├── .clang-format ├── .editorconfig ├── .gitignore ├── .gitmodules ├── CMakeLists.txt ├── CONTRIBUTING ├── LICENSE ├── PACKAGE-LICENSES └── dlib-LICENSE.md ├── README.md ├── barymap_vk.cpp ├── barymap_vk.hpp ├── baryset.cpp ├── baryset.hpp ├── cgltf.cpp ├── common.h ├── common_barymap.h ├── common_mesh.h ├── common_micromesh.h ├── common_micromesh_compressed.h ├── common_micromesh_compressed_rt.h ├── common_micromesh_uncompressed.h ├── config.h ├── dlib_url.h ├── doc └── screenshot.jpg ├── drast_compressed_lod_flat_mesh.comp.glsl ├── drast_compressed_lod_flat_task.comp.glsl ├── drast_compressed_lod_flatsplit_task.comp.glsl ├── drast_shade_compressed.frag.glsl ├── drast_shade_compressed.vert.glsl ├── drast_shade_uncompressed.frag.glsl ├── drast_shade_uncompressed.vert.glsl ├── drast_uncompressed_lod_flat_mesh.comp.glsl ├── drast_uncompressed_lod_flat_task.comp.glsl ├── drast_uncompressed_lod_flatsplit_task.comp.glsl ├── drast_utils.glsl ├── draw_compressed_basic.frag.glsl ├── draw_compressed_basic.mesh.glsl ├── draw_compressed_basic.task.glsl ├── draw_compressed_lod.frag.glsl ├── draw_compressed_lod.mesh.glsl ├── draw_compressed_lod.task.glsl ├── draw_culling.glsl ├── draw_shading.glsl ├── draw_shell.frag.glsl ├── draw_shell.vert.glsl ├── draw_standard.frag.glsl ├── draw_standard.vert.glsl ├── draw_uncompressed_basic.frag.glsl ├── draw_uncompressed_basic.mesh.glsl ├── draw_uncompressed_basic.task.glsl ├── draw_uncompressed_lod.frag.glsl ├── draw_uncompressed_lod.mesh.glsl ├── draw_uncompressed_lod.task.glsl ├── dray_blit.frag.glsl ├── dray_blit.vert.glsl ├── dray_trace.rchit.glsl ├── dray_trace.rgen.glsl ├── dray_trace.rmiss.glsl ├── hbao.h ├── hbao_blur.comp.glsl ├── hbao_blur.glsl ├── hbao_blur_apply.comp.glsl ├── hbao_calc.comp.glsl ├── hbao_deinterleave.comp.glsl ├── hbao_depthlinearize.comp.glsl ├── hbao_pass.cpp ├── hbao_pass.hpp ├── hbao_reinterleave.comp.glsl ├── hbao_viewnormal.comp.glsl ├── meshset.cpp ├── meshset.hpp ├── meshset_gltf.cpp ├── meshset_gltf.hpp ├── meshset_utils.cpp ├── meshset_utils.hpp ├── meshset_vk.cpp ├── meshset_vk.hpp ├── microdisplacement.cpp ├── micromesh_binpack.glsl ├── micromesh_binpack_decl.glsl ├── micromesh_binpack_flat.glsl ├── micromesh_binpack_flat_decl.h ├── micromesh_binpack_flatsplit.glsl ├── micromesh_compressed_rt_vk.cpp ├── micromesh_compressed_rt_vk.hpp ├── micromesh_compressed_vk.cpp ├── micromesh_compressed_vk.hpp ├── micromesh_culling_compressed.glsl ├── micromesh_culling_uncompressed.glsl ├── micromesh_decoder.glsl ├── micromesh_decoder_api.glsl ├── micromesh_decoder_basetri.glsl ├── micromesh_decoder_basetri_vk.cpp ├── micromesh_decoder_basetri_vk.hpp ├── micromesh_decoder_config.glsl ├── micromesh_decoder_microtri.glsl ├── micromesh_decoder_microtri_eval.glsl ├── micromesh_decoder_microtri_vk.cpp ├── micromesh_decoder_microtri_vk.hpp ├── micromesh_decoder_utils_vk.cpp ├── micromesh_decoder_utils_vk.hpp ├── micromesh_loader.glsl ├── micromesh_nopack_flatsplit.glsl ├── micromesh_uncompressed_vk.cpp ├── micromesh_uncompressed_vk.hpp ├── micromesh_utils.glsl ├── nvhiz-update.comp.glsl ├── nvhiz_vk.cpp ├── nvhiz_vk.hpp ├── octant_encoding.h ├── parallel_work.hpp ├── renderer_compressed_rt_vk.cpp ├── renderer_compressed_vk.cpp ├── renderer_uncompressed_vk.cpp ├── renderer_vk.cpp ├── renderer_vk.hpp ├── resources_base_vk.hpp ├── resources_vk.cpp ├── resources_vk.hpp ├── scene.cpp ├── scene.hpp ├── scene_vk.cpp ├── scene_vk.hpp ├── stbimage.cpp ├── vk_nv_micromesh.h ├── vk_nv_micromesh_prototypes.c └── vk_nv_micromesh_prototypes.h /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: LLVM 2 | AccessModifierOffset: '-2' 3 | AlignAfterOpenBracket: Align 4 | AlignConsecutiveAssignments: 'true' 5 | AlignConsecutiveDeclarations: 'true' 6 | AlignOperands: 'true' 7 | AlignTrailingComments: 'true' 8 | AllowAllParametersOfDeclarationOnNextLine: 'false' 9 | AllowShortBlocksOnASingleLine: 'false' 10 | AllowShortCaseLabelsOnASingleLine: 'false' 11 | AllowShortFunctionsOnASingleLine: Inline 12 | AllowShortIfStatementsOnASingleLine: 'false' 13 | AllowShortLoopsOnASingleLine: 'false' 14 | AlwaysBreakAfterReturnType: None 15 | AlwaysBreakBeforeMultilineStrings: 'true' 16 | AlwaysBreakTemplateDeclarations: 'true' 17 | BinPackArguments: 'true' 18 | BinPackParameters: 'false' 19 | ExperimentalAutoDetectBinPacking: 'false' 20 | BreakBeforeBinaryOperators: NonAssignment 21 | BreakBeforeBraces: Custom 22 | BreakBeforeTernaryOperators: 'false' 23 | BreakConstructorInitializersBeforeComma: 'true' 24 | ColumnLimit: '120' 25 | ConstructorInitializerAllOnOneLineOrOnePerLine: 'false' 26 | Cpp11BracedListStyle: 'true' 27 | IndentCaseLabels: 'true' 28 | IndentWidth: '2' 29 | KeepEmptyLinesAtTheStartOfBlocks: 'true' 30 | Language: Cpp 31 | MaxEmptyLinesToKeep: '2' 32 | NamespaceIndentation: None 33 | ObjCSpaceBeforeProtocolList: 'true' 34 | PointerAlignment: Left 35 | SpaceAfterCStyleCast: 'false' 36 | SpaceBeforeAssignmentOperators: 'true' 37 | SpaceBeforeParens: Never 38 | SpaceInEmptyParentheses: 'false' 39 | SpacesBeforeTrailingComments: '2' 40 | SpacesInAngles: 'false' 41 | SpacesInCStyleCastParentheses: 'false' 42 | SpacesInParentheses: 'false' 43 | SpacesInSquareBrackets: 'false' 44 | Standard: Cpp11 45 | TabWidth: '2' 46 | UseTab: Never 47 | SortIncludes: 'false' 48 | ReflowComments: 'false' 49 | BraceWrapping: { 50 | AfterClass: 'true' 51 | AfterControlStatement: 'true' 52 | AfterEnum: 'true' 53 | AfterFunction: 'true' 54 | AfterNamespace: 'false' 55 | AfterStruct: 'true' 56 | AfterUnion: 'true' 57 | BeforeCatch: 'true' 58 | BeforeElse: 'true' 59 | IndentBraces: 'false' 60 | } 61 | PenaltyExcessCharacter: 1 62 | PenaltyBreakBeforeFirstCallParameter: 40 63 | PenaltyBreakFirstLessLess: 1 64 | PenaltyBreakComment: 30 65 | PenaltyBreakString: 30 66 | PenaltyReturnTypeOnItsOwnLine: 9999 67 | BreakStringLiterals: false 68 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # This is the top-most editor config file 2 | root = true 3 | 4 | # Default to 2 space indentation for C/C++ files 5 | [*.{c,cpp,h,hpp,inl}] 6 | indent_size = 2 7 | indent_style = space 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ############################# 2 | # generic 3 | ############################# 4 | 5 | *.bak 6 | 7 | ############################# 8 | # spirv/sass 9 | ############################# 10 | 11 | *.spv 12 | *.spva 13 | *.sass 14 | *.sassbin 15 | 16 | ############################# 17 | #specific to the project 18 | ############################# 19 | 20 | cmake_built 21 | cmake_build 22 | build 23 | _install 24 | bin_x64 25 | downloaded_resources -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "external/cgltf"] 2 | path = external/cgltf 3 | url = https://github.com/pixeljetstream/cgltf.git 4 | branch = nvidia/micromap 5 | [submodule "external/bary_core"] 6 | path = external/bary_core 7 | url = https://github.com/NVIDIAGameWorks/Displacement-MicroMap-BaryFile.git 8 | branch = main 9 | [submodule "nvpro_core"] 10 | path = nvpro_core 11 | url = https://github.com/nvpro-samples/nvpro_core.git 12 | branch = master 13 | -------------------------------------------------------------------------------- /CONTRIBUTING: -------------------------------------------------------------------------------- 1 | https://developercertificate.org/ 2 | 3 | Developer Certificate of Origin 4 | Version 1.1 5 | 6 | Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 7 | 8 | Everyone is permitted to copy and distribute verbatim copies of this 9 | license document, but changing it is not allowed. 10 | 11 | 12 | Developer's Certificate of Origin 1.1 13 | 14 | By making a contribution to this project, I certify that: 15 | 16 | (a) The contribution was created in whole or in part by me and I 17 | have the right to submit it under the open source license 18 | indicated in the file; or 19 | 20 | (b) The contribution is based upon previous work that, to the best 21 | of my knowledge, is covered under an appropriate open source 22 | license and I have the right under that license to submit that 23 | work with modifications, whether created in whole or in part 24 | by me, under the same open source license (unless I am 25 | permitted to submit under a different license), as indicated 26 | in the file; or 27 | 28 | (c) The contribution was provided directly to me by some other 29 | person who certified (a), (b) or (c) and I have not modified 30 | it. 31 | 32 | (d) I understand and agree that this project and the contribution 33 | are public and that a record of the contribution (including all 34 | personal information I submit with it, including my sign-off) is 35 | maintained indefinitely and may be redistributed consistent with 36 | this project or the open source license(s) involved. -------------------------------------------------------------------------------- /PACKAGE-LICENSES/dlib-LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (C) 2003 Davis E. King (davis@dlib.net) 2 | Boost Software License - Version 1.0 - August 17th, 2003 3 | 4 | Permission is hereby granted, free of charge, to any person or organization 5 | obtaining a copy of the software and accompanying documentation covered by 6 | this license (the "Software") to use, reproduce, display, distribute, 7 | execute, and transmit the Software, and to prepare derivative works of the 8 | Software, and to permit third-parties to whom the Software is furnished to 9 | do so, all subject to the following: 10 | 11 | The copyright notices in the Software and this entire statement, including 12 | the above license grant, this restriction and the following disclaimer, 13 | must be included in all copies of the Software, in whole or in part, and 14 | all derivative works of the Software, unless such copies or derivative 15 | works are solely in the form of machine-executable object code generated by 16 | a source language processor. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 21 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 22 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 23 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24 | DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /barymap_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "resources_base_vk.hpp" 23 | #include 24 | 25 | namespace microdisp { 26 | 27 | struct BaryLevelsMapVK 28 | { 29 | struct Level 30 | { 31 | size_t coordsOffset = 0; 32 | size_t headersOffset = 0; 33 | size_t dataOffset = 0; 34 | 35 | size_t firstHeader = 0; 36 | size_t headersCount = 0; 37 | size_t firstData = 0; 38 | size_t dataCount = 0; 39 | }; 40 | 41 | RBuffer binding; 42 | RBuffer data; 43 | 44 | std::vector levels; 45 | 46 | const Level& getLevel(uint32_t subdivLevel, uint32_t topoBits, uint32_t maxLevelCount) const 47 | { 48 | return levels[subdivLevel + topoBits * maxLevelCount]; 49 | } 50 | 51 | void init(ResourcesVK& res, const baryutils::BaryLevelsMap& bmap); 52 | void deinit(ResourcesVK& res); 53 | }; 54 | 55 | } // namespace microdisp 56 | -------------------------------------------------------------------------------- /baryset.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "meshset.hpp" 23 | #include 24 | 25 | #include 26 | 27 | struct BaryDisplacementAttribute 28 | { 29 | // displacement 30 | // The BARY representation of uncompressed data 31 | std::unique_ptr uncompressed = nullptr; 32 | 33 | // The BARY representation of compressed data 34 | std::unique_ptr compressed = nullptr; 35 | std::unique_ptr compressedMisc = nullptr; 36 | }; 37 | 38 | enum ShadingAttributeBit : uint32_t 39 | { 40 | SHADING_ATTRIBUTE_NORMAL_BIT = 1 41 | }; 42 | 43 | struct BaryShadingAttribute 44 | { 45 | // this is a bit of a hack to allow the baking tool and viewer 46 | // to use per-micro vertex shading attributes 47 | std::unique_ptr attribute = nullptr; 48 | 49 | uint32_t attributeFlags = 0; 50 | // which displacement this shading attribute is used with 51 | uint32_t displacementID = MeshSetID::INVALID; 52 | }; 53 | 54 | // Scenes can have multiple sources of displacement information. This class 55 | // stores all those sources, as well as information about how each mesh indexes 56 | // into each source. To render a mesh set with displacement, you need both a 57 | // MeshSet ("base mesh and mapping information") and 58 | // a BaryAttributesSet ("set of displacements") 59 | struct BaryAttributesSet 60 | { 61 | // We have one of these for each source of displacement (e.g. .bary file). 62 | // These are indexed using each Mesh's displacementId member. 63 | 64 | std::vector displacements; 65 | std::vector shadings; 66 | 67 | // shading attribute may not exist 68 | const BaryShadingAttribute* getShading(size_t idx) const { return shadings.empty() || idx == MeshSetID::INVALID ? nullptr : &shadings[idx]; } 69 | BaryShadingAttribute* getShading(size_t idx) { return shadings.empty() || idx == MeshSetID::INVALID ? nullptr : &shadings[idx]; } 70 | 71 | const BaryShadingAttribute* getDisplacementShading(uint32_t dispID, ShadingAttributeBit attributeBit) const; 72 | 73 | // returns MeshSetID::INVALID on fail otherwise slot in `displacements` 74 | uint32_t loadDisplacement(const char* baryFilePathCStr, baryutils::BaryFile& bfile, baryutils::BaryFileOpenOptions* fileOpenOptions = nullptr); 75 | 76 | // returns MeshSetID::INVALID on fail otherwise slot in `shading attributes` 77 | uint32_t loadAttribute(const char* baryFilePathCStr, baryutils::BaryFile& bfile, baryutils::BaryFileOpenOptions* fileOpenOptions = nullptr); 78 | 79 | 80 | 81 | // these depend on updateStats() called 82 | baryutils::BaryStats compressedStats; 83 | baryutils::BaryStats uncompressedStats; 84 | baryutils::BaryStats shadingStats; 85 | 86 | uint64_t compressedMipByteSize = 0; 87 | 88 | // whenever the contents of displacements change, call this 89 | // function 90 | void updateStats(); 91 | 92 | // these require updateStats to be called before 93 | baryutils::BaryLevelsMap makeBaryLevelsMapShading() const 94 | { 95 | return baryutils::BaryLevelsMap(shadingStats.valueOrder, shadingStats.maxSubdivLevel); 96 | } 97 | baryutils::BaryLevelsMap makeBaryLevelsMapUncompressed() const 98 | { 99 | return baryutils::BaryLevelsMap(uncompressedStats.valueOrder, uncompressedStats.maxSubdivLevel); 100 | } 101 | baryutils::BaryLevelsMap makeBaryLevelsMapCompressed() const 102 | { 103 | return baryutils::BaryLevelsMap(compressedStats.valueOrder, compressedStats.maxSubdivLevel); 104 | } 105 | 106 | double computeShellVolume(const MeshSet& baseMeshSet, bool perferDirectionBounds, bool preferUncompressed, uint32_t numThreads = 0) const; 107 | 108 | void fillUniformDirectionBounds(MeshSet& meshSet) const; 109 | 110 | bool supportsCompressedMips() const; 111 | 112 | }; -------------------------------------------------------------------------------- /cgltf.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #define CSF_IMPLEMENTATION 21 | #define CSF_SUPPORT_GLTF2 1 22 | #define CSF_SUPPORT_FILEMAPPING 1 23 | 24 | #include 25 | 26 | #define CGLTF_IMPLEMENTATION 27 | #define CGLTF_WRITE_IMPLEMENTATION 28 | #include 29 | -------------------------------------------------------------------------------- /common_barymap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // this file is included by C++ and GLSL 21 | 22 | #ifndef _COMMON_BARYMAP_H_ 23 | #define _COMMON_BARYMAP_H_ 24 | 25 | ////////////////////////////////////////////////////////////////////////// 26 | 27 | #ifdef __cplusplus 28 | namespace microdisp { 29 | using namespace glm; 30 | #endif 31 | 32 | 33 | //////////////////////////////////////////////////////////////////// 34 | // BaryMapData 35 | 36 | // meshlet configuration for pregenerated meshlets for each level 37 | #define MAX_BARYMAP_VERTICES 64 38 | #define MAX_BARYMAP_PRIMITIVES 64 39 | 40 | // Limit the current app to a sane upper bound. 41 | // Raising this can have consequences in number of bits required for offsets 42 | // in task shader output in dynamic lod scenario. 43 | // But also the data structure required for precalculated uv coords, 44 | // as well as other upper bounds. 45 | #define MAX_BARYMAP_LEVELS 8 46 | // 3 edge bits, up to 8 permutations 47 | #define MAX_BARYMAP_TOPOS 8 48 | 49 | //////////////////////////////////////////////////////////////////// 50 | // BaryMapMeshlet 51 | // 52 | // pre-computed meshlets, we render a single base-triangle 53 | // as sequence of these. 54 | 55 | struct BaryMapMeshlet 56 | { 57 | uint16_t numVertices; 58 | uint16_t numPrimitives; 59 | uint16_t offsetPrims; 60 | uint16_t offsetVertices; 61 | }; 62 | #ifndef __cplusplus 63 | layout(buffer_reference, buffer_reference_align = 4, scalar) restrict readonly buffer BaryMapMeshlet_in 64 | { 65 | BaryMapMeshlet d[]; 66 | }; 67 | #endif 68 | 69 | //////////////////////////////////////////////////////////////////// 70 | // BaryMapLevel 71 | // 72 | // pre-computed data for each subdivision level 73 | 74 | struct BaryMapLevel 75 | { 76 | BUFFER_REF(uints_in) coords; // micro vertex barycentric coords 77 | BUFFER_REF(BaryMapMeshlet_in) meshletHeaders; // micro triangles meshlet headers 78 | BUFFER_REF(uints_in) meshletData; // micro triangles meshlet data 79 | uint meshletCount; 80 | uint triangleCount; 81 | }; 82 | #ifndef __cplusplus 83 | layout(buffer_reference, buffer_reference_align = 16, scalar) restrict readonly buffer BaryMapLevel_in 84 | { 85 | BaryMapLevel d[]; 86 | }; 87 | #endif 88 | 89 | //////////////////////////////////////////////////////////////////// 90 | // BaryMapData 91 | // 92 | // contains multiple BaryMapLevels and accessors for uniform or 93 | // divergent access. 94 | 95 | struct BaryMapData 96 | { 97 | // barycentric lookup maps 98 | BaryMapLevel levelsUni[MAX_BARYMAP_LEVELS * MAX_BARYMAP_TOPOS]; // subgroup-uniform access 99 | BUFFER_REF(BaryMapLevel_in) levels; // divergent access (points to same data as above) 100 | }; 101 | 102 | 103 | 104 | #ifdef __cplusplus 105 | } 106 | #endif 107 | #endif 108 | 109 | -------------------------------------------------------------------------------- /common_mesh.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // this file is included by C++ and GLSL 21 | 22 | #ifndef _COMMON_MESH_H_ 23 | #define _COMMON_MESH_H_ 24 | 25 | ////////////////////////////////////////////////////////////////////////// 26 | 27 | #ifdef __cplusplus 28 | namespace microdisp { 29 | using namespace glm; 30 | #endif 31 | 32 | /////////////////////////////////////////////////////////////////// 33 | // InstanceData 34 | 35 | struct InstanceData 36 | { 37 | mat4 worldMatrix; 38 | mat4 worldMatrixI; 39 | 40 | vec4 color; 41 | uvec3 _pad; 42 | uint normalMapID; 43 | 44 | uint firstVertex; 45 | uint firstIndex; 46 | uint lodSubdiv; 47 | float lodRange; 48 | 49 | vec4 bboxMin; 50 | vec4 bboxMax; 51 | vec4 lodSphere; 52 | }; 53 | #ifndef __cplusplus 54 | layout(buffer_reference, buffer_reference_align = 16, scalar) restrict readonly buffer InstanceDatas_in 55 | { 56 | InstanceData d[]; 57 | }; 58 | #endif 59 | 60 | /////////////////////////////////////////////////////////////////// 61 | // MeshData 62 | 63 | struct MeshData 64 | { 65 | BUFFER_REF(uints_in) indices; 66 | BUFFER_REF(vec3s_in) positions; 67 | BUFFER_REF(uints_in) normals; 68 | BUFFER_REF(uints_in) tangents; 69 | BUFFER_REF(uints_in) bitangents; 70 | BUFFER_REF(vec2s_in) tex0s; 71 | BUFFER_REF(f16vec4s_in) dispDirections; 72 | BUFFER_REF(boundsVec2s_in) dispDirectionBounds; 73 | BUFFER_REF(uint8s_in) dispDecimateFlags; 74 | BUFFER_REF(InstanceDatas_in) instances; 75 | }; 76 | 77 | 78 | /////////////////////////////////////////////////////////////////// 79 | 80 | struct DrawPushData 81 | { 82 | uint firstVertex; 83 | uint firstIndex; 84 | uint instanceID; 85 | uint triangleMax; 86 | uint shellDir; 87 | float shellMin; 88 | float shellMax; 89 | }; 90 | 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif 95 | #endif -------------------------------------------------------------------------------- /common_micromesh.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // this file is included by C++ and GLSL 21 | 22 | #ifndef _COMMON_MICROMESH_H_ 23 | #define _COMMON_MICROMESH_H_ 24 | 25 | #define MICRO_GROUP_SIZE SUBGROUP_SIZE 26 | #define MICRO_TRI_PER_TASK SUBGROUP_SIZE 27 | 28 | // maximum subdiv overall 29 | #define MICRO_MAX_SUBDIV 5 30 | #define MICRO_MAX_LEVELS (MICRO_MAX_SUBDIV+1) 31 | #define MICRO_MAX_TRIANGLES (1u << (MICRO_MAX_SUBDIV*2)) 32 | 33 | // following influence the workgroup size for the respective 34 | // compute shaders used in compute rasterization. 35 | // 36 | // how many groups in the mesh shading phase 37 | // can be changed via shader-reload 38 | #define MICRO_FLAT_MESH_GROUPS 2 39 | 40 | // how many groups in the task shading phase 41 | // cannot be changed at runtime 42 | #define MICRO_FLAT_TASK_GROUPS 2 43 | #define MICRO_FLAT_SPLIT_TASK_GROUPS 1 44 | 45 | #endif -------------------------------------------------------------------------------- /common_micromesh_compressed_rt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // this file is included by C++ and GLSL 21 | 22 | #ifndef _COMMON_MICROMESH_COMPRESSED_RT_H_ 23 | #define _COMMON_MICROMESH_COMPRESSED_RT_H_ 24 | 25 | #include "common_micromesh.h" 26 | 27 | struct MicromeshRtAttrTri 28 | { 29 | uint32_t firstValue; 30 | uint32_t subdivLevel; 31 | }; 32 | 33 | #ifndef __cplusplus 34 | layout(buffer_reference, buffer_reference_align = 8, scalar) restrict readonly buffer MicromeshRtAttrTris_in 35 | { 36 | MicromeshRtAttrTri d[]; 37 | }; 38 | #endif 39 | 40 | struct MicromeshRtAttributes 41 | { 42 | // subdivLevel,firstValue 43 | BUFFER_REF(MicromeshRtAttrTris_in) attrTriangles; 44 | BUFFER_REF(uints_in) attrNormals; 45 | }; 46 | 47 | struct MicromeshRtData 48 | { 49 | BUFFER_REF(uints_in) umajor2bmap[MICRO_MAX_LEVELS]; 50 | }; 51 | 52 | #endif -------------------------------------------------------------------------------- /common_micromesh_uncompressed.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // this file is included by C++ and GLSL 21 | 22 | #ifndef _COMMON_MICROMESH_UNCOMPRESSED_H_ 23 | #define _COMMON_MICROMESH_UNCOMPRESSED_H_ 24 | 25 | #include "common_micromesh.h" 26 | 27 | ////////////////////////////////////////////////////////////////////////// 28 | 29 | // Data structures to render uncompressed barycentric micromesh displacements. 30 | // May contain per-micro-vertex attributes 31 | 32 | #ifdef __cplusplus 33 | namespace microdisp { 34 | using namespace glm; 35 | #endif 36 | 37 | // binding information for descriptor set 38 | 39 | #define DRAWUNCOMPRESSED_UBO_VIEW 0 40 | #define DRAWUNCOMPRESSED_SSBO_STATS 1 41 | #define DRAWUNCOMPRESSED_UBO_MESH 2 42 | #define DRAWUNCOMPRESSED_UBO_MAP 3 43 | #define DRAWUNCOMPRESSED_UBO_UNCOMPRESSED 4 44 | #define DRAWUNCOMPRESSED_UBO_SCRATCH 5 45 | #define DRAWUNCOMPRESSED_TEX_HIZ 6 46 | #define DRAWUNCOMPRESSED_IMG_ATOMIC 7 47 | 48 | 49 | #ifndef __cplusplus 50 | 51 | // these are set via RendererVK::getShaderPrepend() 52 | #ifndef UNCOMPRESSED_UMAJOR 53 | #define UNCOMPRESSED_UMAJOR 1 54 | #endif 55 | 56 | #ifndef UNCOMPRESSED_DISPLACEMENT_BITS 57 | #define UNCOMPRESSED_DISPLACEMENT_BITS 32 58 | #endif 59 | 60 | // setup types and decode macros 61 | #if UNCOMPRESSED_DISPLACEMENT_BITS == 32 62 | #define dbits_in floats_in 63 | #define dbitsminmax_in floats_in 64 | #define dbitsUnpack(v) (v) 65 | #elif UNCOMPRESSED_DISPLACEMENT_BITS == 16 66 | #define dbits_in uint16s_in 67 | #define dbitsminmax_in uint16s_in 68 | #define dbitsUnpack(v) (float((v)) / float(0xFFFF)) 69 | #elif UNCOMPRESSED_DISPLACEMENT_BITS == 11 70 | #define dbits_in uint16s_in 71 | #define dbitsminmax_in uint16s_in 72 | #define dbitsUnpack(v) (float((v)) / float(0x7FF)) 73 | #elif UNCOMPRESSED_DISPLACEMENT_BITS == -11 74 | #define dbits_in uints_in 75 | #define dbitsminmax_in uint16s_in 76 | #define dbitsUnpack(v) (float((v)) / float(0x7FF)) 77 | #elif UNCOMPRESSED_DISPLACEMENT_BITS == 8 78 | #define dbits_in uint8s_in 79 | #define dbitsminmax_in uint8s_in 80 | #define dbitsUnpack(v) (float((v)) / float(0xFF)) 81 | #else 82 | #error "invalid UNCOMPRESSED_DISPLACEMENT_BITS" 83 | #endif 84 | 85 | float loadUncompressedDisplacement(dbits_in distances, uint firstValue, uint idx) 86 | { 87 | #if UNCOMPRESSED_DISPLACEMENT_BITS == -11 88 | uint base = ((idx * 11) / 32) + firstValue; 89 | uint rest = ((idx * 11) % 32); 90 | uint rawLo = distances.d[base]; 91 | uint rawHi = distances.d[base + 1]; // must be safe to overfetch 92 | uint64_t raw64 = packUint2x32(uvec2(rawLo,rawHi)); 93 | uint value = uint(raw64 >> rest) & 0x7FF; 94 | return dbitsUnpack(value); 95 | #else 96 | return dbitsUnpack(distances.d[firstValue + idx]); 97 | #endif 98 | } 99 | 100 | #endif 101 | 102 | 103 | //////////////////////////////////////////////////////////////////// 104 | // BaryFlatTriangle 105 | // 106 | // flattened information for easier rendering 107 | 108 | struct MicromeshUncBaseTri 109 | { 110 | uint16_t subdivLevel; 111 | uint16_t topoBits; 112 | uint firstValue; 113 | uint firstShadingValue; 114 | uint meshletCount; 115 | }; 116 | #ifndef __cplusplus 117 | layout(buffer_reference, buffer_reference_align = 16, scalar) restrict readonly buffer MicromeshUncBaseTri_in 118 | { 119 | MicromeshUncBaseTri d[]; 120 | }; 121 | #endif 122 | 123 | //////////////////////////////////////////////////////////////////// 124 | // BaryMeshData 125 | // 126 | // primary container holding pointers to all data required 127 | // to render an uncompressed bary displaced mesh. 128 | 129 | struct MicromeshUncData 130 | { 131 | // quantized distances 132 | BUFFER_REF(dbits_in) distancesBits; 133 | BUFFER_REF(dbitsminmax_in) triangleBitsMinMax; 134 | 135 | // flattened triangles (resolved indirection from mesh tri to micromap tri) 136 | BUFFER_REF(MicromeshUncBaseTri_in) basetriangles; 137 | BUFFER_REF(vec4s_in) basespheres; 138 | 139 | // per micro-vertex attributes 140 | BUFFER_REF(uints_in) attrNormals; 141 | }; 142 | 143 | /////////////////////////////////////////////////////////////////// 144 | // per-draw info 145 | 146 | struct DrawMicromeshUncPushData 147 | { 148 | uint firstVertex; 149 | uint firstTriangle; 150 | 151 | uint instanceID; 152 | uint triangleMax; 153 | vec2 scale_bias; 154 | 155 | uint64_t binding; 156 | }; 157 | 158 | #ifndef __cplusplus 159 | layout(buffer_reference, buffer_reference_align = 16, scalar) restrict readonly buffer DrawMicromeshUncompressedPushData_in 160 | { 161 | DrawMicromeshUncPushData d[]; 162 | }; 163 | #endif 164 | 165 | ////////////////////////////////////////////////////////////////////////// 166 | 167 | struct MicromeshUncScratchData 168 | { 169 | BUFFER_REF(uints_inout) atomicCounter; 170 | BUFFER_REF(DrawMicromeshUncompressedPushData_in) instancePushDatas; 171 | BUFFER_REF(uints_in) scratchData; 172 | 173 | uint maxCount; // always power of 2 174 | uint maxMask; 175 | }; 176 | 177 | 178 | #ifdef __cplusplus 179 | } 180 | #endif 181 | 182 | #endif 183 | -------------------------------------------------------------------------------- /config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | ////////////////////////////////////////////////////////////////////////// 21 | // this file is included by C++ and GLSL 22 | 23 | #ifndef _CONFIG_H_ 24 | #define _CONFIG_H_ 25 | 26 | ///////////////////////////////////////////////////// 27 | 28 | #define API_SUPPORTED_SETUP_ONLY 1 29 | 30 | // warning raising this beyond 5 has consequences on storage bits 31 | // and needs manual changes in code 32 | #define MAX_BASE_SUBDIV 5 33 | 34 | #define BOUNDS_AS_FP32 1 35 | 36 | #define ATOMIC_LAYERS 2 37 | 38 | // must not change 39 | #define SUBGROUP_SIZE 32 40 | 41 | // Different surface visualization modes. 42 | #define SURFACEVIS_SHADING 0 // Default shading. 43 | #define SURFACEVIS_ANISOTROPY 1 // gl_PrimitiveID is not used; additional pervertexNV attributes; batlow coloring. 44 | #define SURFACEVIS_BASETRI 2 // gl_PrimitiveID holds base triangle index; colorizePrimitive coloring. 45 | #define SURFACEVIS_MICROTRI 3 // gl_PrimitiveID holds unique index per microtriangle; colorizePrimitive coloring. 0 for standard renderer. 46 | #define SURFACEVIS_LOCALTRI 4 // gl_PrimitiveID holds local index of meshlet triangle; colorizePrimitive coloring. 0 for standard renderer. 47 | #define SURFACEVIS_FORMAT 5 // gl_PrimitiveID holds index of encoding format used; colorizePrimitive coloring. 0 for non-umesh renderers. 48 | #define SURFACEVIS_LODBIAS 6 // gl_PrimitiveID holds lod bias. custom hue2rgb coloring; valid for umesh-lod-renderers only. 49 | #define SURFACEVIS_VALUERANGE 7 // gl_PrimitiveID holds effective base triangle range compared to mesh value range. 50 | #define SURFACEVIS_BASESUBDIV 8 // gl_PrimitiveID holds base triangle subdiv level 51 | #define SURFACEVIS_LODSUBDIV 9 // gl_PrimitiveID holds post lod base triangle subdiv level 52 | 53 | #define CLEAR_COLOR 0.1, 0.13, 0.15,0 54 | 55 | /////////////////////////////////////////////////// 56 | #if defined(__cplusplus) 57 | 58 | #include "glm/detail/type_half.hpp" 59 | #include 60 | 61 | enum ModelType 62 | { 63 | MODEL_LO, 64 | MODEL_DISPLACED, 65 | NUM_MODELTYPES, 66 | MODEL_SHELL, 67 | }; 68 | 69 | // few more status prints 70 | extern bool g_verbose; 71 | // allow enabling raytracing extension for micromesh 72 | // if true then codepaths assume native extension exists and rely on it 73 | // if false we still do some fake setup work but the image will be the basemesh alone 74 | extern bool g_enableMicromeshRTExtensions; 75 | // number of default processing threads 76 | extern uint32_t g_numThreads; 77 | 78 | class float16_t 79 | { 80 | private: 81 | glm::detail::hdata h = 0; 82 | 83 | public: 84 | float16_t() {} 85 | float16_t(float f) { h = glm::detail::toFloat16(f); } 86 | 87 | operator float() const { return glm::detail::toFloat32(h); } 88 | }; 89 | 90 | struct f16vec2 91 | { 92 | float16_t x; 93 | float16_t y; 94 | }; 95 | 96 | struct f16vec4 97 | { 98 | float16_t x; 99 | float16_t y; 100 | float16_t z; 101 | float16_t w; 102 | }; 103 | 104 | struct u16vec2 105 | { 106 | uint16_t x; 107 | uint16_t y; 108 | }; 109 | struct u16vec4 110 | { 111 | uint16_t x; 112 | uint16_t y; 113 | uint16_t z; 114 | uint16_t w; 115 | 116 | uint16_t& operator[](size_t i) { return (&x)[i]; } 117 | }; 118 | struct u8vec2 119 | { 120 | uint8_t x; 121 | uint8_t y; 122 | 123 | uint8_t& operator[](size_t i) { return (&x)[i]; } 124 | }; 125 | struct u8vec4 126 | { 127 | uint8_t x; 128 | uint8_t y; 129 | uint8_t z; 130 | uint8_t w; 131 | 132 | uint8_t& operator[](size_t i) { return (&x)[i]; } 133 | }; 134 | #else 135 | 136 | uint encodeMinMaxFp32(float val) { 137 | uint bits = floatBitsToUint(val); 138 | bits ^= (int(bits) >> 31) | 0x80000000u; 139 | return bits; 140 | } 141 | 142 | float decodeMinMaxFp32(uint bits) { 143 | bits ^= ~(int(bits) >> 31) | 0x80000000u; 144 | return uintBitsToFloat(bits); 145 | } 146 | 147 | const float FLT_MAX = 3.402823466e+38F; 148 | const float FLT_EPSILON = 1.192092896e-07F; 149 | 150 | #endif 151 | 152 | #endif 153 | 154 | 155 | -------------------------------------------------------------------------------- /dlib_url.h: -------------------------------------------------------------------------------- 1 | // http://dlib.net/dlib/server/server_http.cpp.html 2 | // http://dlib.net/license.html 3 | // Copyright (C) 2003 Davis E. King (davis@dlib.net) 4 | // License: Boost Software License 5 | // Boost Software License - Version 1.0 - August 17th, 2003 6 | // 7 | // Permission is hereby granted, free of charge, to any person or organization 8 | // obtaining a copy of the software and accompanying documentation covered by 9 | // this license (the "Software") to use, reproduce, display, distribute, 10 | // execute, and transmit the Software, and to prepare derivative works of the 11 | // Software, and to permit third-parties to whom the Software is furnished to 12 | // do so, all subject to the following: 13 | // The copyright notices in the Software and this entire statement, including 14 | // the above license grant, this restriction and the following disclaimer, 15 | // must be included in all copies of the Software, in whole or in part, and 16 | // all derivative works of the Software, unless such copies or derivative 17 | // works are solely in the form of machine-executable object code generated by 18 | // a source language processor. 19 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | // DEALINGS IN THE SOFTWARE. 26 | // 27 | 28 | // Include headers from server_http.h: 29 | #include 30 | #include 31 | 32 | namespace dlib { 33 | inline unsigned char to_hex(unsigned char x) 34 | { 35 | return x + (x > 9 ? ('A' - 10) : '0'); 36 | } 37 | 38 | const inline std::string urlencode(const std::string& s) 39 | { 40 | std::ostringstream os; 41 | 42 | for(std::string::const_iterator ci = s.begin(); ci != s.end(); ++ci) 43 | { 44 | if((*ci >= 'a' && *ci <= 'z') || (*ci >= 'A' && *ci <= 'Z') || (*ci >= '0' && *ci <= '9')) 45 | { // allowed 46 | os << *ci; 47 | } 48 | else if(*ci == ' ') 49 | { 50 | os << '+'; 51 | } 52 | else 53 | { 54 | os << '%' << to_hex(*ci >> 4) << to_hex(*ci % 16); 55 | } 56 | } 57 | 58 | return os.str(); 59 | } 60 | 61 | inline unsigned char from_hex(unsigned char ch) 62 | { 63 | if(ch <= '9' && ch >= '0') 64 | ch -= '0'; 65 | else if(ch <= 'f' && ch >= 'a') 66 | ch -= 'a' - 10; 67 | else if(ch <= 'F' && ch >= 'A') 68 | ch -= 'A' - 10; 69 | else 70 | ch = 0; 71 | return ch; 72 | } 73 | 74 | const inline std::string urldecode(const std::string& str) 75 | { 76 | using namespace std; 77 | string result; 78 | string::size_type i; 79 | for(i = 0; i < str.size(); ++i) 80 | { 81 | if(str[i] == '+') 82 | { 83 | result += ' '; 84 | } 85 | else if(str[i] == '%' && str.size() > i + 2) 86 | { 87 | const unsigned char ch1 = from_hex(str[i + 1]); 88 | const unsigned char ch2 = from_hex(str[i + 2]); 89 | const unsigned char ch = (ch1 << 4) | ch2; 90 | result += ch; 91 | i += 2; 92 | } 93 | else 94 | { 95 | result += str[i]; 96 | } 97 | } 98 | return result; 99 | } 100 | } // namespace dlib 101 | -------------------------------------------------------------------------------- /doc/screenshot.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nvpro-samples/vk_displacement_micromaps/ea5918b4d3bc3a73c91056871ffdcd3c2583399c/doc/screenshot.jpg -------------------------------------------------------------------------------- /drast_compressed_lod_flat_task.comp.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : enable 24 | 25 | #extension GL_EXT_buffer_reference2 : enable 26 | #extension GL_EXT_scalar_block_layout : enable 27 | #extension GL_EXT_shader_8bit_storage : enable 28 | #extension GL_EXT_shader_16bit_storage : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 32 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 33 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 34 | #extension GL_EXT_shader_atomic_int64 : enable 35 | #extension GL_EXT_shader_image_int64 : enable 36 | 37 | #extension GL_KHR_shader_subgroup_basic : require 38 | #extension GL_KHR_shader_subgroup_ballot : require 39 | #extension GL_KHR_shader_subgroup_vote : require 40 | #extension GL_KHR_shader_subgroup_arithmetic : require 41 | #extension GL_KHR_shader_subgroup_shuffle : require 42 | #extension GL_KHR_shader_subgroup_shuffle_relative : require 43 | 44 | #extension GL_NV_shader_subgroup_partitioned : require 45 | 46 | #include "common.h" 47 | #include "common_mesh.h" 48 | #include "common_micromesh_compressed.h" 49 | #include "micromesh_binpack_flat_decl.h" 50 | 51 | layout(buffer_reference, buffer_reference_align = 4) restrict buffer writeonly MicroBinPackFlats_out { 52 | MicroBinPackFlat d[]; 53 | }; 54 | 55 | //////////////////////////////////////////////////////////////// 56 | // BINDINGS 57 | 58 | layout(scalar, binding = DRAWCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 59 | SceneData scene; 60 | SceneData sceneLast; 61 | }; 62 | layout(scalar, binding = DRAWCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 63 | ShaderStats stats; 64 | }; 65 | layout(scalar, binding = DRAWCOMPRESSED_UBO_COMPRESSED) uniform microBuffer { 66 | MicromeshData microdata; 67 | }; 68 | layout(scalar, binding = DRAWCOMPRESSED_UBO_MESH) uniform meshBuffer { 69 | MeshData mesh; 70 | }; 71 | 72 | layout(scalar, binding = DRAWCOMPRESSED_UBO_SCRATCH) uniform scratchBuffer { 73 | MicromeshScratchData scratch; 74 | }; 75 | 76 | #if USE_OCCLUSION_CULLING 77 | #define SUPPORTS_HIZ 1 78 | layout(binding = DRAWCOMPRESSED_TEX_HIZ) uniform sampler2D texHizFar; 79 | #endif 80 | 81 | layout(push_constant) uniform pushDraw { 82 | DrawMicromeshPushData push; 83 | }; 84 | 85 | ////////////////////////////////////////////////////////////////////////// 86 | 87 | #if IS_CONCAT_TASK 88 | 89 | layout(local_size_x=SUBGROUP_SIZE) in; 90 | 91 | void main() 92 | { 93 | #if MICRO_FLAT_MESH_GROUPS > 1 94 | if (gl_LocalInvocationIndex == 0) { 95 | uint outCount = min(scratch.atomicCounter.d[0], scratch.maxCount); 96 | uint alignCount = (outCount + MICRO_FLAT_MESH_GROUPS - 1) / MICRO_FLAT_MESH_GROUPS; 97 | 98 | scratch.atomicCounter.d[0] = alignCount; 99 | 100 | // append some tail dummy data 101 | MicroBinPackFlats_out outData = MicroBinPackFlats_out(scratch.scratchData); 102 | 103 | MicroBinPackFlat flatPackDummy; 104 | flatPackDummy.pack = MICRO_BIN_INVALID_SUBDIV; 105 | flatPackDummy.partOrMask = 0; 106 | 107 | for (uint i = 0; i < MICRO_FLAT_MESH_GROUPS; i++) { 108 | outData.d[i + outCount] = flatPackDummy; 109 | } 110 | } 111 | #endif 112 | } 113 | 114 | #else 115 | 116 | layout(local_size_x=MICRO_GROUP_SIZE, local_size_y=MICRO_FLAT_TASK_GROUPS) in; 117 | 118 | ////////////////////////////////////////////////////////////////////////// 119 | 120 | #include "draw_culling.glsl" 121 | #include "micromesh_utils.glsl" 122 | #include "micromesh_culling_compressed.glsl" 123 | 124 | #define MICROBINPACK_USE_MESHLETCOUNT 0 125 | #define MICROBINPACK_OUT MicroBinPackFlats_out(scratch.scratchData).d 126 | #define MICROBINPACK_OUT_ATOM scratch.atomicCounter.d[0] 127 | #define MICROBINPACK_OUT_MAX scratch.maxCount 128 | 129 | #include "micromesh_binpack_flat.glsl" 130 | 131 | ////////////////////////////////////////////////////////////////////////// 132 | 133 | void processTasks(uint baseID) 134 | { 135 | uint laneID = gl_SubgroupInvocationID; 136 | 137 | uint microMax = push.microMax; 138 | 139 | //microMax = 16; 140 | //if (gl_WorkGroupID.x > 0) return; 141 | 142 | bool valid = baseID + laneID <= microMax; 143 | uint relativeID = valid ? laneID : 0; 144 | uint microID = baseID + relativeID; 145 | 146 | #if MICRO_FLAT_TASK_GROUPS > 1 147 | if (baseID > push.microMax) return; 148 | #endif 149 | 150 | #if USE_LOD 151 | uint targetSubdiv = cullAndLodMicroBaseTriangle(microID, valid); 152 | #else 153 | MicromeshBaseTri microBaseTri = microdata.basetriangles.d[microID]; 154 | uint targetSubdiv = micromesh_getBaseSubdiv(microBaseTri); 155 | #endif 156 | 157 | MicroBinPackFlat_subgroupPack(baseID, relativeID, targetSubdiv, 0, valid, push.instanceID); 158 | } 159 | 160 | void main() 161 | { 162 | //////////////////////// 163 | // "task-shading" phase 164 | 165 | #if MICRO_FLAT_TASK_GROUPS > 1 166 | uint baseID = ((gl_WorkGroupID.x * MICRO_FLAT_TASK_GROUPS) + gl_LocalInvocationID.y) * MICRO_TRI_PER_TASK; 167 | #else 168 | uint baseID = gl_WorkGroupID.x * MICRO_TRI_PER_TASK; 169 | #endif 170 | 171 | processTasks(baseID); 172 | } 173 | 174 | #endif -------------------------------------------------------------------------------- /drast_shade_compressed.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_buffer_reference2 : enable 26 | #extension GL_EXT_scalar_block_layout : enable 27 | #extension GL_EXT_shader_8bit_storage : enable 28 | #extension GL_EXT_shader_16bit_storage : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 32 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 33 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 34 | #extension GL_EXT_shader_atomic_int64 : enable 35 | #extension GL_EXT_shader_image_int64 : enable 36 | 37 | #extension GL_KHR_shader_subgroup_basic : require 38 | #extension GL_KHR_shader_subgroup_ballot : require 39 | #extension GL_KHR_shader_subgroup_vote : require 40 | #extension GL_KHR_shader_subgroup_arithmetic : require 41 | #extension GL_KHR_shader_subgroup_shuffle : require 42 | #extension GL_KHR_shader_subgroup_shuffle_relative : require 43 | 44 | #include "common.h" 45 | #include "common_mesh.h" 46 | #include "common_barymap.h" 47 | #include "common_micromesh_uncompressed.h" 48 | #include "common_micromesh_compressed.h" 49 | 50 | //////////////////////////////////////// 51 | 52 | layout(scalar, binding = DRAWCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 53 | SceneData scene; 54 | SceneData sceneLast; 55 | }; 56 | layout(scalar, binding = DRAWCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 57 | ShaderStats stats; 58 | }; 59 | layout(scalar, binding = DRAWCOMPRESSED_UBO_COMPRESSED) uniform microBuffer { 60 | MicromeshData microdata; 61 | }; 62 | layout(scalar, binding = DRAWCOMPRESSED_UBO_MESH) uniform meshBuffer { 63 | MeshData mesh; 64 | }; 65 | layout(push_constant) uniform pushDraw { 66 | DrawMicromeshPushData push; 67 | }; 68 | layout(binding=DRAWCOMPRESSED_IMG_ATOMIC, r64ui) uniform u64image2DArray imgVisBuffer; 69 | 70 | //////////////////////////////////////// 71 | 72 | #include "draw_shading.glsl" 73 | 74 | //////////////////////////////////////// 75 | 76 | layout(location=0, index=0) out vec4 out_Color; 77 | 78 | //////////////////////////////////////// 79 | 80 | void main() 81 | { 82 | ivec2 coord = ivec2(gl_FragCoord.xy); 83 | uint64_t pixel64 = imageLoad(imgVisBuffer, ivec3(coord,0)).x; 84 | uvec2 pixel = unpackUint2x32(pixel64); 85 | 86 | uint clearDepth = 0xFFFFFFFFu; 87 | 88 | vec4 color = colorizePrimitive(int(pixel.x)); 89 | 90 | out_Color = pixel.y == clearDepth ? vec4(CLEAR_COLOR) : color; 91 | gl_FragDepth = pixel.y == clearDepth ? 1.0f : uintBitsToFloat(pixel.y); 92 | } 93 | -------------------------------------------------------------------------------- /drast_shade_compressed.vert.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | void main() 23 | { 24 | vec4 pos = vec4( 25 | (float( gl_VertexIndex &1)) * 4.0 - 1.0, 26 | (float((gl_VertexIndex>>1)&1)) * 4.0 - 1.0, 27 | 0.5, 1.0); 28 | 29 | gl_Position = pos; 30 | } -------------------------------------------------------------------------------- /drast_shade_uncompressed.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_buffer_reference2 : enable 26 | #extension GL_EXT_scalar_block_layout : enable 27 | #extension GL_EXT_shader_8bit_storage : enable 28 | #extension GL_EXT_shader_16bit_storage : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 32 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 33 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 34 | #extension GL_EXT_shader_atomic_int64 : enable 35 | #extension GL_EXT_shader_image_int64 : enable 36 | 37 | #extension GL_KHR_shader_subgroup_basic : require 38 | #extension GL_KHR_shader_subgroup_ballot : require 39 | #extension GL_KHR_shader_subgroup_vote : require 40 | #extension GL_KHR_shader_subgroup_arithmetic : require 41 | #extension GL_KHR_shader_subgroup_shuffle : require 42 | #extension GL_KHR_shader_subgroup_shuffle_relative : require 43 | 44 | #include "common.h" 45 | #include "common_mesh.h" 46 | #include "common_barymap.h" 47 | #include "common_micromesh_uncompressed.h" 48 | 49 | //////////////////////////////////////// 50 | 51 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 52 | SceneData scene; 53 | SceneData sceneLast; 54 | }; 55 | layout(scalar, binding = DRAWUNCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 56 | ShaderStats stats; 57 | }; 58 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_MESH) uniform meshBuffer { 59 | MeshData mesh; 60 | }; 61 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_UNCOMPRESSED) uniform baryBuffer { 62 | MicromeshUncData bdata; 63 | }; 64 | 65 | layout(push_constant) uniform pushDraw { 66 | DrawMicromeshUncPushData push; 67 | }; 68 | 69 | layout(binding=DRAWUNCOMPRESSED_IMG_ATOMIC, r64ui) uniform u64image2DArray imgVisBuffer; 70 | 71 | 72 | //////////////////////////////////////// 73 | 74 | #include "draw_shading.glsl" 75 | 76 | //////////////////////////////////////// 77 | 78 | layout(location=0, index=0) out vec4 out_Color; 79 | 80 | //////////////////////////////////////// 81 | 82 | void main() 83 | { 84 | ivec2 coord = ivec2(gl_FragCoord.xy); 85 | uint64_t pixel64 = imageLoad(imgVisBuffer, ivec3(coord,0)).x; 86 | uvec2 pixel = unpackUint2x32(pixel64); 87 | 88 | uint clearDepth = 0xFFFFFFFFu; 89 | 90 | vec4 color = colorizePrimitive(int(pixel.x)); 91 | 92 | out_Color = pixel.y == clearDepth ? vec4(CLEAR_COLOR) : color; 93 | gl_FragDepth = pixel.y == clearDepth ? 1.0f : uintBitsToFloat(pixel.y); 94 | } 95 | -------------------------------------------------------------------------------- /drast_shade_uncompressed.vert.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | void main() 23 | { 24 | vec4 pos = vec4( 25 | (float( gl_VertexIndex &1)) * 4.0 - 1.0, 26 | (float((gl_VertexIndex>>1)&1)) * 4.0 - 1.0, 27 | 0.5, 1.0); 28 | 29 | gl_Position = pos; 30 | } -------------------------------------------------------------------------------- /drast_utils.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // basic software rasterization logic 21 | // requires GL_EXT_shader_image_int64 22 | 23 | void outputPixel(vec2 fragCoord, uint payload, float depth) 24 | { 25 | ivec2 coord = ivec2(fragCoord.xy); 26 | uvec2 pixel = uvec2(payload, floatBitsToUint(depth)); 27 | 28 | imageAtomicMin(imgVisBuffer, ivec3(coord,0), packUint2x32(pixel)); 29 | #if ATOMIC_LAYERS > 1 && 0 30 | // for micro-meshes we may want to use two atomics to have more payload 31 | // e.g. avoid deocding for barycentrics, this acts as perf investigation proxy 32 | imageAtomicMin(imgVisBuffer, ivec3(coord,1), packUint2x32(pixel)); 33 | #endif 34 | } 35 | 36 | float edgeFunction(vec2 a, vec2 b, vec2 c, float winding) 37 | { 38 | return ((c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x)) * winding; 39 | } 40 | 41 | void rasterTriangle(vec2 pixel, uint basetri, uint microtri, uvec3 indices, RasterVertex a, RasterVertex b, RasterVertex c, float triArea, float winding) 42 | { 43 | float baryA = edgeFunction(b.xy, c.xy, pixel, winding); 44 | float baryB = edgeFunction(c.xy, a.xy, pixel, winding); 45 | float baryC = edgeFunction(a.xy, b.xy, pixel, winding); 46 | 47 | if (baryA >= 0 && baryB >= 0 && baryC >= 0){ 48 | baryA /= triArea; 49 | baryB /= triArea; 50 | baryC /= triArea; 51 | 52 | float depth = a.z * baryA + 53 | b.z * baryB + 54 | c.z * baryC; 55 | 56 | outputPixel(pixel, (basetri << 10) | (microtri), depth); 57 | } 58 | } -------------------------------------------------------------------------------- /draw_compressed_basic.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_scalar_block_layout : enable 26 | #extension GL_EXT_shader_16bit_storage : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 32 | #extension GL_EXT_shader_atomic_int64 : enable 33 | #extension GL_EXT_buffer_reference2 : enable 34 | #extension GL_EXT_nonuniform_qualifier : require 35 | 36 | #extension GL_NV_fragment_shader_barycentric : enable 37 | 38 | #include "common.h" 39 | #include "common_mesh.h" 40 | #include "common_barymap.h" 41 | #include "common_micromesh_uncompressed.h" 42 | #include "common_micromesh_compressed.h" 43 | 44 | //////////////////////////////////////////////////////////////// 45 | // BINDINGS 46 | 47 | layout(scalar, binding = DRAWCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 48 | SceneData scene; 49 | SceneData sceneLast; 50 | }; 51 | layout(scalar, binding = DRAWCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 52 | ShaderStats stats; 53 | }; 54 | layout(scalar, binding = DRAWCOMPRESSED_UBO_MESH) uniform meshBuffer { 55 | MeshData mesh; 56 | }; 57 | layout(scalar, binding = DRAWCOMPRESSED_UBO_COMPRESSED) uniform microBuffer { 58 | MicromeshData microdata; 59 | }; 60 | #if USE_TEXTURE_NORMALS 61 | layout(binding = 0, set=DSET_TEXTURES) uniform sampler2D tex2Ds[]; 62 | #endif 63 | 64 | layout(push_constant) uniform pushDraw { 65 | DrawMicromeshPushData push; 66 | }; 67 | 68 | //////////////////////////////////////////////////////////////// 69 | // INPUT 70 | 71 | #if SURFACEVIS == SURFACEVIS_SHADING 72 | layout(location = 0) in Interpolants 73 | { 74 | vec3 wPos; 75 | vec3 bary; 76 | flat uint tri; 77 | } 78 | IN; 79 | #endif 80 | 81 | #if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) \ 82 | || (SURFACEVIS == SURFACEVIS_ANISOTROPY) 83 | layout(location = 3) in pervertexNV PerVertex 84 | { 85 | #if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) 86 | uint vidx; 87 | #endif 88 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 89 | vec3 vwPos; // Copy of wPos; aliasing with wPos is unfortunately invalid. 90 | #endif 91 | } 92 | INvtx[3]; 93 | #endif 94 | 95 | //////////////////////////////////////////////////////////////// 96 | // OUTPUT 97 | 98 | layout(location=0,index=0) out vec4 out_Color; 99 | 100 | //////////////////////////////////////////////////////////////// 101 | 102 | #include "draw_shading.glsl" 103 | 104 | void main() 105 | { 106 | #if SURFACEVIS == SURFACEVIS_NONE 107 | { 108 | uint tri = IN.tri; 109 | uvec3 triIndices = uvec3( mesh.indices.d[tri * 3 + 0], 110 | mesh.indices.d[tri * 3 + 1], 111 | mesh.indices.d[tri * 3 + 2]) + push.firstVertex; 112 | 113 | // Generate vertices 114 | mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix; 115 | mat4 worldMatrixIT = transpose(inverse(worldMatrix)); 116 | 117 | vec3 wPos = IN.wPos; 118 | vec2 tex = getInterpolatedArray(mesh.tex0s.d, triIndices, IN.bary); 119 | 120 | #if USE_FACET_SHADING 121 | vec3 wNormal = -cross(dFdx(IN.wPos), dFdy(IN.wPos)); 122 | #elif USE_TEXTURE_NORMALS 123 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.normals.d, triIndices, IN.bary); 124 | vec3 wTangent = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.tangents.d, triIndices, IN.bary); 125 | vec3 wBitangent = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.bitangents.d, triIndices, IN.bary); 126 | #elif USE_MICROVERTEX_NORMALS 127 | uvec3 baryIndices = uvec3(INvtx[0].vidx, INvtx[1].vidx, INvtx[2].vidx); 128 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(microdata.attrNormals.d, baryIndices, vec3(gl_BaryCoordNV)); 129 | #else 130 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.normals.d, triIndices, IN.bary); 131 | #endif 132 | 133 | out_Color = shading(push.instanceID, gl_PrimitiveID, wPos, tex, wNormal 134 | #if USE_TEXTURE_NORMALS 135 | , wTangent, wBitangent 136 | #endif 137 | ); 138 | } 139 | #else // Surface debug visualization 140 | 141 | out_Color = surfaceVisShading( 142 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 143 | INvtx[0].vwPos, INvtx[1].vwPos, INvtx[2].vwPos, 144 | #endif 145 | gl_PrimitiveID); 146 | 147 | #endif 148 | } -------------------------------------------------------------------------------- /draw_compressed_basic.task.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_NV_mesh_shader : enable 26 | 27 | #extension GL_EXT_buffer_reference2 : enable 28 | #extension GL_EXT_scalar_block_layout : enable 29 | #extension GL_EXT_shader_8bit_storage : enable 30 | #extension GL_EXT_shader_16bit_storage : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 32 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 33 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 34 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 35 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 36 | #extension GL_EXT_shader_atomic_int64 : enable 37 | 38 | #extension GL_KHR_shader_subgroup_basic : require 39 | #extension GL_KHR_shader_subgroup_ballot : require 40 | #extension GL_KHR_shader_subgroup_vote : require 41 | #extension GL_KHR_shader_subgroup_arithmetic : require 42 | 43 | #include "common.h" 44 | #include "common_mesh.h" 45 | #include "common_barymap.h" 46 | #include "common_micromesh_uncompressed.h" 47 | #include "common_micromesh_compressed.h" 48 | 49 | ////////////////////////////////////////////////////////////////////////// 50 | 51 | layout(local_size_x=MICRO_GROUP_SIZE) in; 52 | 53 | //////////////////////////////////////////////////////////////// 54 | // BINDINGS 55 | 56 | layout(scalar, binding = DRAWCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 57 | SceneData scene; 58 | SceneData sceneLast; 59 | }; 60 | layout(scalar, binding = DRAWCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 61 | ShaderStats stats; 62 | }; 63 | layout(scalar, binding = DRAWCOMPRESSED_UBO_COMPRESSED) uniform microBuffer { 64 | MicromeshData microdata; 65 | }; 66 | layout(scalar, binding = DRAWCOMPRESSED_UBO_MESH) uniform meshBuffer { 67 | MeshData mesh; 68 | }; 69 | layout(push_constant) uniform pushDraw { 70 | DrawMicromeshPushData push; 71 | }; 72 | 73 | ////////////////////////////////////////////////////////////////////////// 74 | // OUTPUT 75 | 76 | taskNV out Task 77 | { 78 | uint baseID; 79 | uint16_t prefix[MICRO_TRI_PER_TASK]; 80 | } OUT; 81 | 82 | 83 | ////////////////////////////////////////////////////////////////////////// 84 | 85 | #include "micromesh_utils.glsl" 86 | 87 | ////////////////////////////////////////////////////////////////////////// 88 | 89 | // This shader does not do any packing of micromeshes with low subdivision 90 | // into a single mesh-shader invocation. This yields less performance as we 91 | // unterutilize the hardware this way (an entire mesh workgroup may generate 92 | // only a single triangle). 93 | // Look at the draw_micromesh_lod shaders which use a more complex setup 94 | // that does this. 95 | 96 | void main() 97 | { 98 | uint baseID = gl_WorkGroupID.x * MICRO_TRI_PER_TASK; 99 | uint laneID = gl_SubgroupInvocationID; 100 | 101 | OUT.baseID = baseID; 102 | 103 | uint microID = baseID + laneID; 104 | 105 | MicromeshBaseTri microBaseTri = microdata.basetriangles.d[min(microID, push.microMax)]; 106 | uint microSubdiv = micromesh_getBaseSubdiv(microBaseTri); 107 | 108 | uint partMicroMeshlets = subdiv_getNumMeshlets(microSubdiv); 109 | uint microMax = push.microMax; 110 | 111 | bool valid = true; 112 | 113 | // debugging 114 | //valid = microID == 0; 115 | //partMicroMeshlets = 1; 116 | 117 | uint meshletCount = microID <= microMax && valid ? partMicroMeshlets : 0; 118 | 119 | uint prefix = subgroupExclusiveAdd(meshletCount); 120 | OUT.prefix[laneID] = uint16_t(prefix); 121 | 122 | if (laneID == MICRO_TRI_PER_TASK-1) 123 | { 124 | gl_TaskCountNV = prefix + meshletCount; 125 | #if USE_STATS 126 | atomicAdd(stats.meshlets, prefix + meshletCount); 127 | #endif 128 | } 129 | } -------------------------------------------------------------------------------- /draw_compressed_lod.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_scalar_block_layout : enable 26 | #extension GL_EXT_shader_16bit_storage : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 32 | #extension GL_EXT_shader_atomic_int64 : enable 33 | #extension GL_EXT_buffer_reference2 : enable 34 | #extension GL_EXT_nonuniform_qualifier : require 35 | 36 | #extension GL_NV_fragment_shader_barycentric : enable 37 | 38 | #include "common.h" 39 | #include "common_mesh.h" 40 | #include "common_barymap.h" 41 | #include "common_micromesh_uncompressed.h" 42 | #include "common_micromesh_compressed.h" 43 | 44 | //////////////////////////////////////////////////////////////// 45 | // BINDINGS 46 | 47 | layout(scalar, binding = DRAWCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 48 | SceneData scene; 49 | SceneData sceneLast; 50 | }; 51 | layout(scalar, binding = DRAWCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 52 | ShaderStats stats; 53 | }; 54 | layout(scalar, binding = DRAWCOMPRESSED_UBO_MESH) uniform meshBuffer { 55 | MeshData mesh; 56 | }; 57 | layout(scalar, binding = DRAWCOMPRESSED_UBO_COMPRESSED) uniform microBuffer { 58 | MicromeshData microdata; 59 | }; 60 | #if USE_TEXTURE_NORMALS 61 | layout(binding = 0, set=DSET_TEXTURES) uniform sampler2D tex2Ds[]; 62 | #endif 63 | 64 | layout(push_constant) uniform pushDraw { 65 | DrawMicromeshPushData push; 66 | }; 67 | 68 | //////////////////////////////////////////////////////////////// 69 | // INPUT 70 | 71 | #if SURFACEVIS == SURFACEVIS_SHADING 72 | layout(location = 0) in Interpolants 73 | { 74 | vec3 wPos; 75 | vec3 bary; 76 | flat uint tri; 77 | } 78 | IN; 79 | #endif 80 | 81 | #if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) \ 82 | || (SURFACEVIS == SURFACEVIS_ANISOTROPY) 83 | layout(location = 3) in pervertexNV PerVertex 84 | { 85 | #if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) 86 | uint vidx; 87 | #endif 88 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 89 | vec3 vwPos; // Copy of wPos; aliasing with wPos is unfortunately invalid. 90 | #endif 91 | } 92 | INvtx[3]; 93 | #endif 94 | 95 | //////////////////////////////////////////////////////////////// 96 | // OUTPUT 97 | 98 | layout(location=0,index=0) out vec4 out_Color; 99 | 100 | //////////////////////////////////////////////////////////////// 101 | 102 | #include "draw_shading.glsl" 103 | 104 | void main() 105 | { 106 | #if SURFACEVIS == SURFACEVIS_SHADING 107 | { 108 | uint tri = IN.tri; 109 | uvec3 triIndices = uvec3( mesh.indices.d[tri * 3 + 0], 110 | mesh.indices.d[tri * 3 + 1], 111 | mesh.indices.d[tri * 3 + 2]) + push.firstVertex; 112 | 113 | // Generate vertices 114 | mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix; 115 | mat4 worldMatrixIT = transpose(inverse(worldMatrix)); 116 | 117 | vec3 wPos = IN.wPos; 118 | vec2 tex = getInterpolatedArray(mesh.tex0s.d, triIndices, IN.bary); 119 | 120 | #if USE_FACET_SHADING 121 | vec3 wNormal = -cross(dFdx(IN.wPos), dFdy(IN.wPos)); 122 | #elif USE_TEXTURE_NORMALS 123 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.normals.d, triIndices, IN.bary); 124 | vec3 wTangent = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.tangents.d, triIndices, IN.bary); 125 | vec3 wBitangent = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.bitangents.d, triIndices, IN.bary); 126 | #elif USE_MICROVERTEX_NORMALS 127 | uvec3 baryIndices = uvec3(INvtx[0].vidx, INvtx[1].vidx, INvtx[2].vidx); 128 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(microdata.attrNormals.d, baryIndices, vec3(gl_BaryCoordNV)); 129 | #else 130 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.normals.d, triIndices, IN.bary); 131 | #endif 132 | 133 | out_Color = shading(push.instanceID, gl_PrimitiveID, wPos, tex, wNormal 134 | #if USE_TEXTURE_NORMALS 135 | , wTangent, wBitangent 136 | #endif 137 | ); 138 | } 139 | #else // Surface debug visualization 140 | 141 | out_Color = surfaceVisShading( 142 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 143 | INvtx[0].vwPos, INvtx[1].vwPos, INvtx[2].vwPos, 144 | #endif 145 | gl_PrimitiveID); 146 | 147 | #endif 148 | } -------------------------------------------------------------------------------- /draw_compressed_lod.task.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | #version 460 20 | 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #extension GL_NV_mesh_shader : enable 25 | 26 | #extension GL_EXT_buffer_reference2 : enable 27 | #extension GL_EXT_scalar_block_layout : enable 28 | #extension GL_EXT_shader_8bit_storage : enable 29 | #extension GL_EXT_shader_16bit_storage : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 32 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 33 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 34 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 35 | #extension GL_EXT_shader_atomic_int64 : enable 36 | 37 | #extension GL_KHR_shader_subgroup_basic : require 38 | #extension GL_KHR_shader_subgroup_ballot : require 39 | #extension GL_KHR_shader_subgroup_vote : require 40 | #extension GL_KHR_shader_subgroup_shuffle : require 41 | #extension GL_KHR_shader_subgroup_arithmetic : require 42 | #extension GL_NV_shader_subgroup_partitioned : require 43 | 44 | #include "common.h" 45 | #include "common_mesh.h" 46 | #include "common_barymap.h" 47 | #include "common_micromesh_uncompressed.h" 48 | #include "common_micromesh_compressed.h" 49 | #include "micromesh_binpack_decl.glsl" 50 | 51 | ////////////////////////////////////////////////////////////////////////// 52 | 53 | layout(local_size_x=MICRO_GROUP_SIZE) in; 54 | 55 | //////////////////////////////////////////////////////////////// 56 | // BINDINGS 57 | 58 | layout(scalar, binding = DRAWCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 59 | SceneData scene; 60 | SceneData sceneLast; 61 | }; 62 | layout(scalar, binding = DRAWCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 63 | ShaderStats stats; 64 | }; 65 | layout(scalar, binding = DRAWCOMPRESSED_UBO_COMPRESSED) uniform microBuffer { 66 | MicromeshData microdata; 67 | }; 68 | layout(scalar, binding = DRAWCOMPRESSED_UBO_MESH) uniform meshBuffer { 69 | MeshData mesh; 70 | }; 71 | #if USE_OCCLUSION_CULLING 72 | #define SUPPORTS_HIZ 1 73 | layout(binding = DRAWCOMPRESSED_TEX_HIZ) uniform sampler2D texHizFar; 74 | #endif 75 | 76 | layout(push_constant) uniform pushDraw { 77 | DrawMicromeshPushData push; 78 | }; 79 | 80 | 81 | ////////////////////////////////////////////////////////////////////////// 82 | // OUTPUT 83 | 84 | taskNV out Task 85 | { 86 | MicroBinPack binpack; 87 | } OUT; 88 | 89 | //////////////////////////////////////////////////////////////////////////////// 90 | 91 | #include "draw_culling.glsl" 92 | #include "micromesh_utils.glsl" 93 | #include "micromesh_culling_compressed.glsl" 94 | 95 | // MicroBinPack functions need to reference target variables directly via macros 96 | #define MICROBINPACK_USE_MESHLETCOUNT 0 97 | #define MICROBINPACK_OUT OUT.binpack 98 | #define MICROBINPACK_OUT_COUNT gl_TaskCountNV 99 | #include "micromesh_binpack.glsl" 100 | 101 | //////////////////////////////////////////////////////////////////////////////// 102 | 103 | void main() 104 | { 105 | uint baseID = gl_WorkGroupID.x * MICRO_TRI_PER_TASK; 106 | uint laneID = gl_SubgroupInvocationID; 107 | 108 | uint microMax = push.microMax; 109 | 110 | //if (baseID != 0) return; 111 | //microMax = 16; 112 | 113 | bool valid = baseID + laneID <= microMax; 114 | uint relativeID = valid ? laneID : 0; 115 | uint microID = baseID + relativeID; 116 | 117 | uint targetSubdiv = cullAndLodMicroBaseTriangle(microID, valid); 118 | 119 | MicroBinPack_subgroupPack(baseID, relativeID, targetSubdiv, 0, valid); 120 | } -------------------------------------------------------------------------------- /draw_shell.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_scalar_block_layout : enable 26 | #extension GL_EXT_shader_16bit_storage : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 32 | #extension GL_EXT_shader_atomic_int64 : enable 33 | #extension GL_EXT_buffer_reference2 : enable 34 | 35 | #extension GL_NV_fragment_shader_barycentric : enable 36 | 37 | #include "common.h" 38 | #include "common_mesh.h" 39 | 40 | //////////////////////////////////////////////////////////////// 41 | 42 | layout(scalar, binding = DRAWSTD_UBO_VIEW) uniform sceneBuffer { 43 | SceneData scene; 44 | SceneData sceneLast; 45 | }; 46 | layout(scalar, binding = DRAWSTD_SSBO_STATS) coherent buffer statsBuffer { 47 | ShaderStats stats; 48 | }; 49 | layout(scalar, binding = DRAWSTD_UBO_MESH) uniform meshBuffer { 50 | MeshData mesh; 51 | }; 52 | 53 | 54 | layout(push_constant) uniform pushDraw { 55 | DrawPushData push; 56 | }; 57 | 58 | //////////////////////////////////////////////////////////////// 59 | 60 | layout(location=0) in Interpolants { 61 | float shell; 62 | } IN; 63 | 64 | 65 | //////////////////////////////////////////////////////////////// 66 | 67 | layout(location=0,index=0) out vec4 out_Color; 68 | 69 | //////////////////////////////////////////////////////////////// 70 | 71 | void main() 72 | { 73 | out_Color = mix(vec4(0,1,1,1),vec4(1,1,0,1),IN.shell); 74 | } -------------------------------------------------------------------------------- /draw_shell.vert.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_scalar_block_layout : enable 26 | #extension GL_EXT_shader_16bit_storage : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 32 | #extension GL_EXT_shader_atomic_int64 : enable 33 | #extension GL_EXT_buffer_reference2 : enable 34 | 35 | #extension GL_NV_fragment_shader_barycentric : enable 36 | 37 | #include "common.h" 38 | #include "common_mesh.h" 39 | 40 | //////////////////////////////////////////////////////////////// 41 | 42 | layout(scalar, binding = DRAWSTD_UBO_VIEW) uniform sceneBuffer { 43 | SceneData scene; 44 | SceneData sceneLast; 45 | }; 46 | layout(scalar, binding = DRAWSTD_SSBO_STATS) coherent buffer statsBuffer { 47 | ShaderStats stats; 48 | }; 49 | layout(scalar, binding = DRAWSTD_UBO_MESH) uniform meshBuffer { 50 | MeshData mesh; 51 | }; 52 | 53 | 54 | layout(push_constant) uniform pushDraw { 55 | DrawPushData push; 56 | }; 57 | 58 | //////////////////////////////////////////////////////////////// 59 | 60 | layout(location=0) out Interpolants { 61 | float shell; 62 | } OUT; 63 | 64 | //////////////////////////////////////////////////////////////// 65 | 66 | // to avoid multiple shaders the shell rendering is done as follows 67 | // - we render triangles with polygon mode lines 68 | // - object is rendered in two passes with the same shader, but in different modes 69 | // - first, the object is drawn as triangles with indexbuffer and instancing twice 70 | // gl_InstanceIndex 0 is the min shell, 1 the max shell 71 | // - second, the object is drawn as triangles, no indexbuffer, one per vertex 72 | // this is the pass that generates the lines along direction vectors 73 | // we create one triangle per direction vector where 1 vertex is atop 74 | // and two at bottom. We nudge the bottom vertex by one pixel 75 | 76 | 77 | void main() 78 | { 79 | uint vertexID; 80 | float shell; 81 | float nudge = 0; 82 | 83 | if (push.shellDir != 0) 84 | { 85 | vertexID = push.firstVertex + (gl_VertexIndex/3); 86 | shell = float(gl_VertexIndex & 1); 87 | // * 2 because of -1,1 clipspace 88 | nudge = (gl_VertexIndex % 3) == 2 ? 2.0/float(scene.viewportf.x) : 0.0f; 89 | } 90 | else 91 | { 92 | vertexID = push.firstVertex + gl_VertexIndex; 93 | shell = float(gl_InstanceIndex); 94 | } 95 | 96 | vec3 oPos = mesh.positions.d[vertexID]; 97 | f16vec3 oDir = mesh.dispDirections.d[vertexID].xyz; 98 | 99 | #if USE_DIRECTION_BOUNDS 100 | boundsVec2 bounds0 = mesh.dispDirectionBounds.d[vertexID]; 101 | 102 | oPos = oPos + oDir * bounds0.x; 103 | oDir = oDir * float16_t(bounds0.y); 104 | #endif 105 | 106 | mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix; 107 | 108 | float disp = mix(push.shellMin, push.shellMax, shell); 109 | oPos += oDir * disp; 110 | 111 | vec4 wPos = worldMatrix * vec4(oPos,1); 112 | 113 | OUT.shell = shell; 114 | gl_Position = scene.viewProjMatrix * wPos; 115 | // in the shellDir case we have 3 vertices per triangle (one triangle per line) 116 | // vertex 0 is bottom, vertex 1 is tip, vertex 2 is bottom again 117 | // we nudge vertex 2 by one pixel 118 | // * hPos.w to get nudge from screenspace into clipspace 119 | gl_Position.x += nudge * gl_Position.w; 120 | } -------------------------------------------------------------------------------- /draw_standard.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_scalar_block_layout : enable 26 | #extension GL_EXT_shader_16bit_storage : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 32 | #extension GL_EXT_shader_atomic_int64 : enable 33 | #extension GL_EXT_buffer_reference2 : enable 34 | #extension GL_EXT_nonuniform_qualifier : require 35 | 36 | #extension GL_NV_fragment_shader_barycentric : enable 37 | 38 | #include "common.h" 39 | #include "common_mesh.h" 40 | 41 | //////////////////////////////////////////////////////////////// 42 | 43 | layout(scalar, binding = DRAWSTD_UBO_VIEW) uniform sceneBuffer { 44 | SceneData scene; 45 | SceneData sceneLast; 46 | }; 47 | layout(scalar, binding = DRAWSTD_SSBO_STATS) coherent buffer statsBuffer { 48 | ShaderStats stats; 49 | }; 50 | layout(scalar, binding = DRAWSTD_UBO_MESH) uniform meshBuffer { 51 | MeshData mesh; 52 | }; 53 | #if USE_TEXTURE_NORMALS 54 | layout(binding = 0, set=DSET_TEXTURES) uniform sampler2D tex2Ds[]; 55 | #endif 56 | 57 | layout(push_constant) uniform pushDraw { 58 | DrawPushData push; 59 | }; 60 | 61 | //////////////////////////////////////////////////////////////// 62 | 63 | layout(location=0) in Interpolants { 64 | vec3 wPos; 65 | vec3 wNormal; 66 | vec2 tex; 67 | #if USE_TEXTURE_NORMALS 68 | vec3 wTangent; 69 | vec3 wBitangent; 70 | #endif 71 | } IN; 72 | 73 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 74 | layout(location=5) in pervertexNV PerVertex { 75 | vec3 vwPos; 76 | } INvtx[3]; 77 | #endif 78 | 79 | //////////////////////////////////////////////////////////////// 80 | 81 | layout(location=0,index=0) out vec4 out_Color; 82 | 83 | //////////////////////////////////////////////////////////////// 84 | 85 | #include "draw_shading.glsl" 86 | 87 | void main() 88 | { 89 | #if SURFACEVIS == SURFACEVIS_SHADING 90 | #if USE_FACET_SHADING 91 | vec3 wNormal = -cross(dFdx(IN.wPos), dFdy(IN.wPos)); 92 | #else 93 | vec3 wNormal = IN.wNormal; 94 | #endif 95 | 96 | out_Color = shading(push.instanceID, gl_PrimitiveID, IN.wPos, IN.tex, wNormal 97 | #if USE_TEXTURE_NORMALS 98 | ,IN.wTangent ,IN.wBitangent 99 | #endif 100 | ); 101 | #else // Surface debug visualization 102 | out_Color = surfaceVisShading( 103 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 104 | INvtx[0].vwPos, INvtx[1].vwPos, INvtx[2].vwPos, 105 | #endif 106 | gl_PrimitiveID); 107 | #endif 108 | } -------------------------------------------------------------------------------- /draw_standard.vert.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_scalar_block_layout : enable 26 | #extension GL_EXT_shader_16bit_storage : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 32 | #extension GL_EXT_shader_atomic_int64 : enable 33 | #extension GL_EXT_buffer_reference2 : enable 34 | 35 | #extension GL_NV_fragment_shader_barycentric : enable 36 | 37 | #include "common.h" 38 | #include "common_mesh.h" 39 | 40 | //////////////////////////////////////////////////////////////// 41 | 42 | layout(scalar, binding = DRAWSTD_UBO_VIEW) uniform sceneBuffer { 43 | SceneData scene; 44 | SceneData sceneLast; 45 | }; 46 | layout(scalar, binding = DRAWSTD_SSBO_STATS) coherent buffer statsBuffer { 47 | ShaderStats stats; 48 | }; 49 | layout(scalar, binding = DRAWSTD_UBO_MESH) uniform meshBuffer { 50 | MeshData mesh; 51 | }; 52 | 53 | 54 | layout(push_constant) uniform pushDraw { 55 | DrawPushData push; 56 | }; 57 | 58 | //////////////////////////////////////////////////////////////// 59 | 60 | layout(location=0) out Interpolants { 61 | vec3 wPos; 62 | vec3 wNormal; 63 | vec2 tex; 64 | #if USE_TEXTURE_NORMALS 65 | vec3 wTangent; 66 | vec3 wBitangent; 67 | #endif 68 | } OUT; 69 | 70 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 71 | layout(location=5) out PerVertex { 72 | vec3 vwPos; // Copy of wPos; aliasing with wPos is not permitted 73 | } OUTvtx; 74 | #endif 75 | 76 | //////////////////////////////////////////////////////////////// 77 | 78 | void main() 79 | { 80 | mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix; 81 | mat4 worldMatrixIT = transpose(inverse(worldMatrix)); 82 | vec4 wPos = worldMatrix * vec4(mesh.positions.d[push.firstVertex + gl_VertexIndex],1); 83 | OUT.wPos = wPos.xyz; 84 | OUT.wNormal = mat3(worldMatrixIT) * unpackUvec(mesh.normals.d[push.firstVertex + gl_VertexIndex]); 85 | OUT.tex = mesh.tex0s.d[push.firstVertex + gl_VertexIndex]; 86 | #if USE_TEXTURE_NORMALS 87 | OUT.wTangent = mat3(worldMatrixIT) * unpackUvec(mesh.tangents.d[push.firstVertex + gl_VertexIndex]); 88 | OUT.wBitangent = mat3(worldMatrixIT) * unpackUvec(mesh.bitangents.d[push.firstVertex + gl_VertexIndex]); 89 | #endif 90 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 91 | OUTvtx.vwPos = wPos.xyz; 92 | #endif 93 | gl_Position = scene.viewProjMatrix * wPos; 94 | } -------------------------------------------------------------------------------- /draw_uncompressed_basic.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | #version 460 20 | 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #extension GL_EXT_scalar_block_layout : enable 25 | #extension GL_EXT_shader_16bit_storage : enable 26 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 31 | #extension GL_EXT_shader_atomic_int64 : enable 32 | #extension GL_EXT_buffer_reference2 : enable 33 | #extension GL_EXT_nonuniform_qualifier : require 34 | 35 | #extension GL_NV_fragment_shader_barycentric : enable 36 | #extension GL_NV_mesh_shader : enable 37 | 38 | #include "common.h" 39 | #include "common_mesh.h" 40 | #include "common_barymap.h" 41 | #include "common_micromesh_uncompressed.h" 42 | 43 | //////////////////////////////////////////////////////////////// 44 | 45 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 46 | SceneData scene; 47 | SceneData sceneLast; 48 | }; 49 | layout(scalar, binding = DRAWUNCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 50 | ShaderStats stats; 51 | }; 52 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_MESH) uniform meshBuffer { 53 | MeshData mesh; 54 | }; 55 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_UNCOMPRESSED) uniform baryBuffer { 56 | MicromeshUncData microdata; 57 | }; 58 | #if USE_TEXTURE_NORMALS 59 | layout(binding = 0, set=DSET_TEXTURES) uniform sampler2D tex2Ds[]; 60 | #endif 61 | 62 | layout(push_constant) uniform pushDraw { 63 | DrawMicromeshUncPushData push; 64 | }; 65 | 66 | //////////////////////////////////////////////////////////////// 67 | 68 | #if SURFACEVIS == SURFACEVIS_SHADING 69 | layout(location = 0) in Interpolants 70 | { 71 | vec3 wPos; 72 | vec3 bary; 73 | flat uint tri; 74 | } 75 | IN; 76 | #endif 77 | 78 | #if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) \ 79 | || (SURFACEVIS == SURFACEVIS_ANISOTROPY) 80 | layout(location = 3) in pervertexNV PerVertex 81 | { 82 | #if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) 83 | uint vidx; 84 | #endif 85 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 86 | vec3 vwPos; // Copy of wPos; aliasing with wPos is unfortunately invalid. 87 | #endif 88 | } 89 | INvtx[3]; 90 | #endif 91 | 92 | //////////////////////////////////////////////////////////////// 93 | 94 | layout(location=0,index=0) out vec4 out_Color; 95 | 96 | //////////////////////////////////////////////////////////////// 97 | 98 | #include "draw_shading.glsl" 99 | 100 | void main() 101 | { 102 | #if SURFACEVIS == SURFACEVIS_SHADING 103 | { 104 | uint tri = IN.tri; 105 | uvec3 triIndices = uvec3( mesh.indices.d[tri * 3 + 0], 106 | mesh.indices.d[tri * 3 + 1], 107 | mesh.indices.d[tri * 3 + 2]) + push.firstVertex; 108 | 109 | // Generate vertices 110 | mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix; 111 | mat4 worldMatrixIT = transpose(inverse(worldMatrix)); 112 | 113 | vec3 wPos = IN.wPos; 114 | vec2 tex = getInterpolatedArray(mesh.tex0s.d, triIndices, IN.bary); 115 | 116 | #if USE_FACET_SHADING == 1 117 | vec3 wNormal = -cross(dFdx(IN.wPos), dFdy(IN.wPos)); 118 | #elif USE_TEXTURE_NORMALS 119 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.normals.d, triIndices, IN.bary); 120 | vec3 wTangent = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.tangents.d, triIndices, IN.bary); 121 | vec3 wBitangent = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.bitangents.d, triIndices, IN.bary); 122 | #elif USE_MICROVERTEX_NORMALS 123 | uvec3 baryIndices = uvec3(INvtx[0].vidx, INvtx[1].vidx, INvtx[2].vidx); 124 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(microdata.attrNormals.d, baryIndices, vec3(gl_BaryCoordNV)); 125 | #else 126 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.normals.d, triIndices, IN.bary); 127 | #endif 128 | 129 | out_Color = shading(push.instanceID, gl_PrimitiveID, wPos, tex, wNormal 130 | #if USE_TEXTURE_NORMALS 131 | , wTangent, wBitangent 132 | #endif 133 | ); 134 | } 135 | #else // Surface debug visualization 136 | out_Color = surfaceVisShading( 137 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 138 | INvtx[0].vwPos, INvtx[1].vwPos, INvtx[2].vwPos, 139 | #endif 140 | gl_PrimitiveID); 141 | 142 | #endif 143 | } -------------------------------------------------------------------------------- /draw_uncompressed_basic.task.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_NV_mesh_shader : enable 26 | 27 | #extension GL_EXT_buffer_reference2 : enable 28 | #extension GL_EXT_scalar_block_layout : enable 29 | #extension GL_EXT_shader_8bit_storage : enable 30 | #extension GL_EXT_shader_16bit_storage : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 32 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 33 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 34 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 35 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 36 | #extension GL_EXT_shader_atomic_int64 : enable 37 | 38 | #extension GL_KHR_shader_subgroup_basic : require 39 | #extension GL_KHR_shader_subgroup_ballot : require 40 | #extension GL_KHR_shader_subgroup_vote : require 41 | #extension GL_KHR_shader_subgroup_arithmetic : require 42 | 43 | #include "common.h" 44 | #include "common_mesh.h" 45 | #include "common_barymap.h" 46 | #include "common_micromesh_uncompressed.h" 47 | 48 | layout(local_size_x=MICRO_TRI_PER_TASK) in; 49 | 50 | //////////////////////////////////////////////////////////////// 51 | 52 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 53 | SceneData scene; 54 | SceneData sceneLast; 55 | }; 56 | layout(scalar, binding = DRAWUNCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 57 | ShaderStats stats; 58 | }; 59 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_MESH) uniform meshBuffer { 60 | MeshData mesh; 61 | }; 62 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_MAP) uniform bmapBuffer { 63 | BaryMapData bmap; 64 | }; 65 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_UNCOMPRESSED) uniform baryBuffer { 66 | MicromeshUncData microdata; 67 | }; 68 | 69 | layout(push_constant) uniform pushDraw { 70 | DrawMicromeshUncPushData push; 71 | }; 72 | 73 | ////////////////////////////////////////////////////////////////////////// 74 | // OUTPUT 75 | 76 | taskNV out Task 77 | { 78 | uint baseID; 79 | uint16_t prefix[MICRO_TRI_PER_TASK]; 80 | } OUT; 81 | 82 | ////////////////////////////////////////////////////////////////////////// 83 | 84 | // This shader does not do any packing of micromeshes with low subdivision 85 | // into a single mesh-shader invocation. This yields less performance as we 86 | // unterutilize the hardware this way (an entire mesh workgroup may generate 87 | // only a single triangle). 88 | // Look at the draw_micromesh_lod shaders which use a more complex setup 89 | // that does this. 90 | 91 | void main() 92 | { 93 | uint baseID = gl_WorkGroupID.x * MICRO_TRI_PER_TASK; 94 | uint laneID = gl_SubgroupInvocationID; 95 | 96 | uint tri = baseID + laneID; 97 | uint triMax = push.triangleMax; 98 | //triMax = 0; 99 | 100 | uint triFlatID = min(tri, triMax); 101 | uint meshletCount = microdata.basetriangles.d[triFlatID].meshletCount; 102 | meshletCount = tri <= triMax ? meshletCount : 0; 103 | 104 | uint prefix = subgroupExclusiveAdd(meshletCount); 105 | OUT.prefix[laneID] = uint16_t(prefix); 106 | 107 | if (laneID == MICRO_GROUP_SIZE-1) 108 | { 109 | gl_TaskCountNV = prefix + meshletCount; 110 | OUT.baseID = baseID; 111 | 112 | #if USE_STATS 113 | atomicAdd(stats.meshlets, prefix + meshletCount); 114 | atomicAdd(stats.debugUI, 1); 115 | #endif 116 | } 117 | } -------------------------------------------------------------------------------- /draw_uncompressed_lod.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_scalar_block_layout : enable 26 | #extension GL_EXT_shader_16bit_storage : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 32 | #extension GL_EXT_shader_atomic_int64 : enable 33 | #extension GL_EXT_buffer_reference2 : enable 34 | #extension GL_EXT_nonuniform_qualifier : require 35 | 36 | #extension GL_NV_fragment_shader_barycentric : enable 37 | #extension GL_NV_mesh_shader : enable 38 | 39 | #include "common.h" 40 | #include "common_mesh.h" 41 | #include "common_barymap.h" 42 | #include "common_micromesh_uncompressed.h" 43 | 44 | //////////////////////////////////////////////////////////////// 45 | 46 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 47 | SceneData scene; 48 | SceneData sceneLast; 49 | }; 50 | layout(scalar, binding = DRAWUNCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 51 | ShaderStats stats; 52 | }; 53 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_MESH) uniform meshBuffer { 54 | MeshData mesh; 55 | }; 56 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_UNCOMPRESSED) uniform baryBuffer { 57 | MicromeshUncData microdata; 58 | }; 59 | #if USE_TEXTURE_NORMALS 60 | layout(binding = 0, set=DSET_TEXTURES) uniform sampler2D tex2Ds[]; 61 | #endif 62 | 63 | layout(push_constant) uniform pushDraw { 64 | DrawMicromeshUncPushData push; 65 | }; 66 | 67 | //////////////////////////////////////////////////////////////// 68 | 69 | #if SURFACEVIS == SURFACEVIS_SHADING 70 | layout(location = 0) in Interpolants 71 | { 72 | vec3 wPos; 73 | vec3 bary; 74 | flat uint tri; 75 | } 76 | IN; 77 | #endif 78 | 79 | #if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) \ 80 | || (SURFACEVIS == SURFACEVIS_ANISOTROPY) 81 | layout(location = 3) in pervertexNV PerVertex 82 | { 83 | #if (SURFACEVIS == SURFACEVIS_SHADING && USE_MICROVERTEX_NORMALS) 84 | uint vidx; 85 | #endif 86 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 87 | vec3 vwPos; // Copy of wPos; aliasing with wPos is unfortunately invalid. 88 | #endif 89 | } 90 | INvtx[3]; 91 | #endif 92 | 93 | //////////////////////////////////////////////////////////////// 94 | 95 | layout(location=0,index=0) out vec4 out_Color; 96 | 97 | //////////////////////////////////////////////////////////////// 98 | 99 | #include "draw_shading.glsl" 100 | 101 | void main() 102 | { 103 | #if SURFACEVIS == SURFACEVIS_SHADING 104 | { 105 | uint tri = IN.tri; 106 | uvec3 triIndices = uvec3( mesh.indices.d[tri * 3 + 0], 107 | mesh.indices.d[tri * 3 + 1], 108 | mesh.indices.d[tri * 3 + 2]) + push.firstVertex; 109 | 110 | // Generate vertices 111 | mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix; 112 | mat4 worldMatrixIT = transpose(inverse(worldMatrix)); 113 | 114 | vec3 wPos = IN.wPos; 115 | vec2 tex = getInterpolatedArray(mesh.tex0s.d, triIndices, IN.bary); 116 | 117 | #if USE_FACET_SHADING 118 | vec3 wNormal = -cross(dFdx(IN.wPos), dFdy(IN.wPos)); 119 | #elif USE_TEXTURE_NORMALS 120 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.normals.d, triIndices, IN.bary); 121 | vec3 wTangent = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.tangents.d, triIndices, IN.bary); 122 | vec3 wBitangent = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.bitangents.d, triIndices, IN.bary); 123 | #elif USE_MICROVERTEX_NORMALS 124 | uvec3 baryIndices = uvec3(INvtx[0].vidx, INvtx[1].vidx, INvtx[2].vidx); 125 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(microdata.attrNormals.d, baryIndices, vec3(gl_BaryCoordNV)); 126 | #else 127 | vec3 wNormal = mat3(worldMatrixIT) * getInterpolatedArrayUvec(mesh.normals.d, triIndices, IN.bary); 128 | #endif 129 | 130 | out_Color = shading(push.instanceID, gl_PrimitiveID, wPos, tex, wNormal 131 | #if USE_TEXTURE_NORMALS 132 | , wTangent, wBitangent 133 | #endif 134 | ); 135 | } 136 | #else // Surface debug visualization 137 | 138 | out_Color = surfaceVisShading( 139 | #if SURFACEVIS == SURFACEVIS_ANISOTROPY 140 | INvtx[0].vwPos, INvtx[1].vwPos, INvtx[2].vwPos, 141 | #endif 142 | gl_PrimitiveID); 143 | 144 | #endif 145 | } -------------------------------------------------------------------------------- /draw_uncompressed_lod.task.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_NV_mesh_shader : enable 26 | 27 | #extension GL_EXT_buffer_reference2 : enable 28 | #extension GL_EXT_scalar_block_layout : enable 29 | #extension GL_EXT_shader_8bit_storage : enable 30 | #extension GL_EXT_shader_16bit_storage : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 32 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 33 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 34 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 35 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 36 | #extension GL_EXT_shader_atomic_int64 : enable 37 | 38 | #extension GL_KHR_shader_subgroup_basic : require 39 | #extension GL_KHR_shader_subgroup_ballot : require 40 | #extension GL_KHR_shader_subgroup_vote : require 41 | #extension GL_KHR_shader_subgroup_arithmetic : require 42 | 43 | #extension GL_KHR_shader_subgroup_shuffle : require 44 | #extension GL_NV_shader_subgroup_partitioned : require 45 | 46 | #include "common.h" 47 | #include "common_mesh.h" 48 | #include "common_barymap.h" 49 | #include "common_micromesh_uncompressed.h" 50 | #include "micromesh_binpack_decl.glsl" 51 | 52 | layout(local_size_x=MICRO_TRI_PER_TASK) in; 53 | 54 | //////////////////////////////////////////////////////////////// 55 | 56 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_VIEW) uniform sceneBuffer { 57 | SceneData scene; 58 | SceneData sceneLast; 59 | }; 60 | layout(scalar, binding = DRAWUNCOMPRESSED_SSBO_STATS) coherent buffer statsBuffer { 61 | ShaderStats stats; 62 | }; 63 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_MESH) uniform meshBuffer { 64 | MeshData mesh; 65 | }; 66 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_MAP) uniform bmapBuffer { 67 | BaryMapData bmap; 68 | }; 69 | layout(scalar, binding = DRAWUNCOMPRESSED_UBO_UNCOMPRESSED) uniform baryBuffer { 70 | MicromeshUncData microdata; 71 | }; 72 | #if USE_OCCLUSION_CULLING 73 | #define SUPPORTS_HIZ 1 74 | layout(binding = DRAWUNCOMPRESSED_TEX_HIZ) uniform sampler2D texHizFar; 75 | #endif 76 | 77 | layout(push_constant) uniform pushDraw { 78 | DrawMicromeshUncPushData push; 79 | }; 80 | 81 | ////////////////////////////////////////////////////////////////////////// 82 | // OUTPUT 83 | 84 | taskNV out Task 85 | { 86 | MicroBinPack binpack; 87 | } OUT; 88 | 89 | 90 | ////////////////////////////////////////////////////////////////////////// 91 | 92 | #include "draw_culling.glsl" 93 | #include "micromesh_culling_uncompressed.glsl" 94 | 95 | // MicroBinPack functions need to reference target variables directly via macros 96 | #define MICROBINPACK_USE_MESHLETCOUNT 1 97 | #define MICROBINPACK_OUT OUT.binpack 98 | #define MICROBINPACK_OUT_COUNT gl_TaskCountNV 99 | #include "micromesh_binpack.glsl" 100 | 101 | ////////////////////////////////////////////////////////////////////////// 102 | 103 | void main() 104 | { 105 | uint baseID = gl_WorkGroupID.x * MICRO_TRI_PER_TASK; 106 | uint laneID = gl_SubgroupInvocationID; 107 | 108 | uint triMax = push.triangleMax; 109 | 110 | //if (baseID != 0) return; 111 | //triMax = 1; 112 | 113 | bool valid = baseID + laneID <= triMax; 114 | uint relativeID = valid ? laneID : 0; 115 | uint triID = baseID + relativeID; 116 | 117 | MicromeshUncBaseTri baseTri = microdata.basetriangles.d[min(triID, triMax)]; 118 | uint meshletCount = baseTri.meshletCount; 119 | uint baseSubdiv = baseTri.subdivLevel; 120 | 121 | uint targetSubdiv = cullAndLodBaseTriangle(triID, baseSubdiv, valid); 122 | 123 | #if 0 124 | uint targetMax = subgroupMax(valid ? targetSubdiv : 0); 125 | targetSubdiv = valid ? targetMax : targetSubdiv; 126 | #endif 127 | 128 | if (valid && targetSubdiv < baseSubdiv) { 129 | meshletCount = bmap.levels.d[targetSubdiv].meshletCount; 130 | } 131 | 132 | MicroBinPack_subgroupPack(baseID, relativeID, targetSubdiv, meshletCount, valid); 133 | 134 | //gl_TaskCountNV *= scene.dbgUint; 135 | } -------------------------------------------------------------------------------- /dray_blit.frag.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | #extension GL_EXT_control_flow_attributes : require 24 | 25 | #extension GL_EXT_buffer_reference2 : enable 26 | #extension GL_EXT_scalar_block_layout : enable 27 | #extension GL_EXT_shader_8bit_storage : enable 28 | #extension GL_EXT_shader_16bit_storage : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 32 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 33 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 34 | #extension GL_EXT_shader_atomic_int64 : enable 35 | #extension GL_EXT_shader_image_int64 : enable 36 | 37 | #extension GL_KHR_shader_subgroup_basic : require 38 | #extension GL_KHR_shader_subgroup_ballot : require 39 | #extension GL_KHR_shader_subgroup_vote : require 40 | #extension GL_KHR_shader_subgroup_arithmetic : require 41 | #extension GL_KHR_shader_subgroup_shuffle : require 42 | #extension GL_KHR_shader_subgroup_shuffle_relative : require 43 | 44 | #include "common.h" 45 | 46 | //////////////////////////////////////// 47 | 48 | layout(scalar, binding = DRAWRAY_UBO_VIEW) uniform sceneBuffer { 49 | SceneData scene; 50 | SceneData sceneLast; 51 | }; 52 | layout(scalar, binding = DRAWRAY_SSBO_STATS) coherent buffer statsBuffer { 53 | ShaderStats stats; 54 | }; 55 | 56 | layout(binding=DRAWRAY_IMG_OUT, r64ui) uniform u64image2DArray imgOutBuffer; 57 | 58 | 59 | //////////////////////////////////////// 60 | 61 | layout(location=0, index=0) out vec4 out_Color; 62 | 63 | //////////////////////////////////////// 64 | 65 | void main() 66 | { 67 | ivec2 coord = ivec2(gl_FragCoord.xy); 68 | uint64_t pixel64 = imageLoad(imgOutBuffer, ivec3(coord,0)).x; 69 | uvec2 pixel = unpackUint2x32(pixel64); 70 | 71 | uint clearDepth = 0xFFFFFFFFu; 72 | 73 | vec4 color = unpackUnorm4x8(pixel.x); 74 | 75 | out_Color = color; 76 | gl_FragDepth = uintBitsToFloat(pixel.y); 77 | } 78 | -------------------------------------------------------------------------------- /dray_blit.vert.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | void main() 23 | { 24 | uint idx = gl_VertexIndex; 25 | vec4 pos = vec4( 26 | (float( idx &1U)) * 4.0 - 1.0, 27 | (float((idx>>1U)&1U)) * 4.0 - 1.0, 28 | 0, 1.0); 29 | gl_Position = pos; 30 | } -------------------------------------------------------------------------------- /dray_trace.rgen.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | #version 460 20 | 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #extension GL_EXT_ray_tracing : require 25 | #extension GL_EXT_scalar_block_layout : enable 26 | #extension GL_EXT_shader_16bit_storage : enable 27 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : enable 28 | #extension GL_EXT_shader_explicit_arithmetic_types_int8 : enable 29 | #extension GL_EXT_shader_explicit_arithmetic_types_int32 : enable 30 | #extension GL_EXT_shader_explicit_arithmetic_types_int16 : enable 31 | #extension GL_EXT_shader_explicit_arithmetic_types_int64 : enable 32 | #extension GL_EXT_buffer_reference2 : enable 33 | #extension GL_EXT_shader_image_int64 : enable 34 | 35 | #include "common.h" 36 | #include "common_mesh.h" 37 | 38 | //////////////////////////////////////////////////////////////// 39 | 40 | layout(scalar, binding = DRAWRAY_UBO_VIEW) uniform sceneBuffer { 41 | SceneData scene; 42 | SceneData sceneLast; 43 | }; 44 | layout(scalar, binding = DRAWRAY_SSBO_STATS) coherent buffer statsBuffer { 45 | ShaderStats stats; 46 | }; 47 | 48 | layout(binding=DRAWRAY_IMG_OUT, r64ui) uniform u64image2DArray imgOutBuffer; 49 | 50 | layout(binding=DRAWRAY_ACC) uniform accelerationStructureEXT accScene; 51 | 52 | //////////////////////////////////////////////////////////////// 53 | 54 | layout(location = 0) rayPayloadEXT uvec2 rayHit; 55 | 56 | void main() 57 | { 58 | ivec2 coord = ivec2(gl_LaunchIDEXT.xy); 59 | vec2 d = (vec2(gl_LaunchIDEXT.xy) + vec2(0.5)) / vec2(gl_LaunchSizeEXT.xy) * 2.0 - 1.0; 60 | 61 | vec3 origin = (scene.viewMatrixI * vec4(0, 0, 0, 1)).xyz; 62 | vec3 target = (scene.projMatrixI * vec4(d.x, d.y, 1, 1)).xyz; 63 | vec3 direction = (scene.viewMatrixI * vec4(normalize(target), 0)).xyz; 64 | 65 | float tMin = scene.nearPlane; 66 | float tMax = scene.farPlane; 67 | 68 | direction = normalize(direction); 69 | 70 | traceRayEXT(accScene, gl_RayFlagsCullBackFacingTrianglesEXT, 71 | 0xff, 72 | 0, 1, // hit offset, hit stride 73 | 0, // miss offset 74 | origin.xyz, tMin, direction.xyz, tMax, 75 | 0 // rayPayloadNV location qualifier 76 | ); 77 | 78 | imageStore(imgOutBuffer, ivec3(coord,0), u64vec4(packUint2x32(rayHit),0,0,0)); 79 | } 80 | 81 | 82 | -------------------------------------------------------------------------------- /dray_trace.rmiss.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | 22 | #extension GL_GOOGLE_include_directive : enable 23 | 24 | #extension GL_EXT_ray_tracing : require 25 | 26 | #include "config.h" 27 | 28 | layout(location = 0) rayPayloadInEXT uvec2 rayHit; 29 | void main() 30 | { 31 | rayHit.x = packUnorm4x8(vec4(CLEAR_COLOR)); 32 | rayHit.y = floatBitsToUint(1.0f); 33 | } 34 | -------------------------------------------------------------------------------- /hbao.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #ifndef NVHBAO_H_ 21 | #define NVHBAO_H_ 22 | 23 | #define NVHBAO_RANDOMTEX_SIZE 4 24 | #define NVHBAO_NUM_DIRECTIONS 8 25 | 26 | #define NVHBAO_MAIN_UBO 0 27 | #define NVHBAO_MAIN_TEX_DEPTH 1 28 | #define NVHBAO_MAIN_TEX_LINDEPTH 2 29 | #define NVHBAO_MAIN_TEX_VIEWNORMAL 3 30 | #define NVHBAO_MAIN_TEX_DEPTHARRAY 4 31 | #define NVHBAO_MAIN_TEX_RESULTARRAY 5 32 | #define NVHBAO_MAIN_TEX_RESULT 6 33 | #define NVHBAO_MAIN_TEX_BLUR 7 34 | #define NVHBAO_MAIN_IMG_LINDEPTH 8 35 | #define NVHBAO_MAIN_IMG_VIEWNORMAL 9 36 | #define NVHBAO_MAIN_IMG_DEPTHARRAY 10 37 | #define NVHBAO_MAIN_IMG_RESULTARRAY 11 38 | #define NVHBAO_MAIN_IMG_RESULT 12 39 | #define NVHBAO_MAIN_IMG_BLUR 13 40 | #define NVHBAO_MAIN_IMG_OUT 14 41 | 42 | #ifndef NVHBAO_BLUR 43 | #define NVHBAO_BLUR 1 44 | #endif 45 | 46 | // 1 is slower 47 | #ifndef NVHBAO_SKIP_INTERPASS 48 | #define NVHBAO_SKIP_INTERPASS 0 49 | #endif 50 | 51 | #ifdef __cplusplus 52 | namespace glsl { 53 | using namespace glm; 54 | #endif 55 | 56 | struct NVHBAOData 57 | { 58 | float RadiusToScreen; // radius 59 | float R2; // 1/radius 60 | float NegInvR2; // radius * radius 61 | float NDotVBias; 62 | 63 | vec2 InvFullResolution; 64 | vec2 InvQuarterResolution; 65 | 66 | ivec2 SourceResolutionScale; 67 | float AOMultiplier; 68 | float PowExponent; 69 | 70 | vec4 projReconstruct; 71 | vec4 projInfo; 72 | int projOrtho; 73 | int _pad0; 74 | ivec2 _pad1; 75 | 76 | ivec2 FullResolution; 77 | ivec2 QuarterResolution; 78 | 79 | mat4 InvProjMatrix; 80 | 81 | vec4 float2Offsets[NVHBAO_RANDOMTEX_SIZE * NVHBAO_RANDOMTEX_SIZE]; 82 | vec4 jitters[NVHBAO_RANDOMTEX_SIZE * NVHBAO_RANDOMTEX_SIZE]; 83 | }; 84 | 85 | // keep all these equal size 86 | struct NVHBAOMainPush 87 | { 88 | int layer; 89 | int _pad0; 90 | ivec2 _pad1; 91 | }; 92 | 93 | struct NVHBAOBlurPush 94 | { 95 | vec2 invResolutionDirection; 96 | float sharpness; 97 | float _pad; 98 | }; 99 | 100 | #ifdef __cplusplus 101 | } 102 | #else 103 | 104 | layout(std140, binding = NVHBAO_MAIN_UBO) uniform controlBuffer 105 | { 106 | NVHBAOData control; 107 | }; 108 | 109 | #ifndef NVHABO_GFX 110 | 111 | layout(local_size_x = 32, local_size_y = 2) in; 112 | 113 | bool setupCoord(inout ivec2 coord, inout vec2 texCoord, ivec2 res, vec2 invRes) 114 | { 115 | ivec2 base = ivec2(gl_WorkGroupID.xy) * 8; 116 | ivec2 subset = ivec2(int(gl_LocalInvocationID.x) & 1, int(gl_LocalInvocationID.x) / 2); 117 | subset += gl_LocalInvocationID.x >= 16 ? ivec2(2, -8) : ivec2(0, 0); 118 | subset += ivec2(gl_LocalInvocationID.y * 4, 0); 119 | 120 | coord = base + subset; 121 | 122 | if(coord.x >= res.x || coord.y >= res.y) 123 | return true; 124 | 125 | texCoord = (vec2(coord) + vec2(0.5)) * invRes; 126 | 127 | return false; 128 | } 129 | 130 | bool setupCoordFull(inout ivec2 coord, inout vec2 texCoord) 131 | { 132 | return setupCoord(coord, texCoord, control.FullResolution, control.InvFullResolution); 133 | } 134 | 135 | bool setupCoordQuarter(inout ivec2 coord, inout vec2 texCoord) 136 | { 137 | return setupCoord(coord, texCoord, control.QuarterResolution, control.InvQuarterResolution); 138 | } 139 | 140 | #endif 141 | 142 | #endif 143 | #endif 144 | -------------------------------------------------------------------------------- /hbao_blur.comp.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #include "hbao.h" 25 | 26 | layout(binding=NVHBAO_MAIN_IMG_BLUR, rg16f) uniform image2D imgBlur; 27 | layout(binding=NVHBAO_MAIN_TEX_RESULT) uniform sampler2D texSource; 28 | 29 | #include "hbao_blur.glsl" 30 | 31 | //------------------------------------------------------------------------- 32 | 33 | void main() 34 | { 35 | ivec2 intCoord; 36 | vec2 texCoord; 37 | 38 | if (setupCoordFull(intCoord, texCoord)) return; 39 | 40 | vec2 res = BlurRun(texCoord); 41 | imageStore(imgBlur, intCoord, vec4(res,0,0)); 42 | } 43 | -------------------------------------------------------------------------------- /hbao_blur.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | layout(push_constant) uniform pushData { 21 | NVHBAOBlurPush blur; 22 | }; 23 | 24 | 25 | const float KERNEL_RADIUS = 3; 26 | 27 | //------------------------------------------------------------------------- 28 | 29 | float BlurFunction(vec2 uv, float r, float center_c, float center_d, inout float w_total) 30 | { 31 | vec2 aoz = texture(texSource, uv).xy; 32 | float c = aoz.x; 33 | float d = aoz.y; 34 | 35 | const float BlurSigma = float(KERNEL_RADIUS) * 0.5; 36 | const float BlurFalloff = 1.0 / (2.0*BlurSigma*BlurSigma); 37 | 38 | float ddiff = (d - center_d) * blur.sharpness; 39 | float w = exp2(-r*r*BlurFalloff - ddiff*ddiff); 40 | w_total += w; 41 | 42 | return c*w; 43 | } 44 | 45 | vec2 BlurRun(vec2 texCoord) 46 | { 47 | vec2 aoz = texture(texSource, texCoord).xy; 48 | float center_c = aoz.x; 49 | float center_d = aoz.y; 50 | 51 | float c_total = center_c; 52 | float w_total = 1.0; 53 | 54 | [[unroll]] 55 | for (float r = 1; r <= KERNEL_RADIUS; ++r) 56 | { 57 | vec2 uv = texCoord + blur.invResolutionDirection * r; 58 | c_total += BlurFunction(uv, r, center_c, center_d, w_total); 59 | } 60 | 61 | [[unroll]] 62 | for (float r = 1; r <= KERNEL_RADIUS; ++r) 63 | { 64 | vec2 uv = texCoord - blur.invResolutionDirection * r; 65 | c_total += BlurFunction(uv, r, center_c, center_d, w_total); 66 | } 67 | 68 | return vec2(c_total/w_total, center_d); 69 | //return vec2(aoz); 70 | } 71 | -------------------------------------------------------------------------------- /hbao_blur_apply.comp.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #include "hbao.h" 25 | 26 | layout(binding=NVHBAO_MAIN_IMG_OUT, rgba16f) uniform image2D imgOut; 27 | layout(binding=NVHBAO_MAIN_TEX_BLUR) uniform sampler2D texSource; 28 | 29 | #include "hbao_blur.glsl" 30 | 31 | //------------------------------------------------------------------------- 32 | 33 | 34 | void main() 35 | { 36 | ivec2 intCoord; 37 | vec2 texCoord; 38 | 39 | if (setupCoordFull(intCoord, texCoord)) return; 40 | 41 | vec2 res = BlurRun(texCoord); 42 | vec4 color = imageLoad(imgOut, intCoord); 43 | imageStore(imgOut, intCoord, color * res.x); 44 | } 45 | -------------------------------------------------------------------------------- /hbao_deinterleave.comp.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #include "hbao.h" 25 | 26 | layout(binding=NVHBAO_MAIN_TEX_LINDEPTH) uniform sampler2D texLinearDepth; 27 | layout(binding=NVHBAO_MAIN_IMG_DEPTHARRAY,r32f) uniform image2DArray imgDepthArray; 28 | 29 | //---------------------------------------------------------------------------------- 30 | 31 | void outputColor(ivec2 intCoord, int layer, float value) 32 | { 33 | imageStore(imgDepthArray, ivec3(intCoord,layer), vec4(value,0,0,0)); 34 | } 35 | 36 | void main() 37 | { 38 | ivec2 intCoord; 39 | vec2 texCoord; 40 | 41 | if (setupCoordQuarter(intCoord, texCoord)) return; 42 | 43 | vec2 uv = vec2(intCoord) * 4.0 + 0.5; 44 | uv *= control.InvFullResolution; 45 | 46 | vec4 S0 = textureGather (texLinearDepth, uv, 0); 47 | vec4 S1 = textureGatherOffset(texLinearDepth, uv, ivec2(2,0), 0); 48 | vec4 S2 = textureGatherOffset(texLinearDepth, uv, ivec2(0,2), 0); 49 | vec4 S3 = textureGatherOffset(texLinearDepth, uv, ivec2(2,2), 0); 50 | 51 | outputColor(intCoord, 0, S0.w); 52 | outputColor(intCoord, 1, S0.z); 53 | outputColor(intCoord, 2, S1.w); 54 | outputColor(intCoord, 3, S1.z); 55 | outputColor(intCoord, 4, S0.x); 56 | outputColor(intCoord, 5, S0.y); 57 | outputColor(intCoord, 6, S1.x); 58 | outputColor(intCoord, 7, S1.y); 59 | 60 | outputColor(intCoord, 0 + 8, S2.w); 61 | outputColor(intCoord, 1 + 8, S2.z); 62 | outputColor(intCoord, 2 + 8, S3.w); 63 | outputColor(intCoord, 3 + 8, S3.z); 64 | outputColor(intCoord, 4 + 8, S2.x); 65 | outputColor(intCoord, 5 + 8, S2.y); 66 | outputColor(intCoord, 6 + 8, S3.x); 67 | outputColor(intCoord, 7 + 8, S3.y); 68 | } 69 | -------------------------------------------------------------------------------- /hbao_depthlinearize.comp.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #include "hbao.h" 25 | 26 | layout(binding=NVHBAO_MAIN_TEX_DEPTH) uniform sampler2D inputTexture; 27 | layout(binding=NVHBAO_MAIN_IMG_LINDEPTH, r32f) uniform image2D imgLinearDepth; 28 | #if NVHBAO_SKIP_INTERPASS 29 | layout(binding=NVHBAO_MAIN_IMG_DEPTHARRAY, r32f) uniform image2DArray imgLinearDepthArray; 30 | #endif 31 | 32 | 33 | float reconstructCSZ(float d, vec4 clipInfo) { 34 | #if 1 35 | vec4 ndc = vec4(0,0,d,1); 36 | vec4 unproj = control.InvProjMatrix * ndc; 37 | return unproj.z / unproj.w; 38 | #else 39 | // clipInfo = z_n * z_f, z_n - z_f, z_f, perspective = 1 : 0 40 | 41 | if (clipInfo[3] != 0) { 42 | return (clipInfo[0] / (clipInfo[1] * d + clipInfo[2])); 43 | } 44 | else { 45 | return (clipInfo[1]+clipInfo[2] - d * clipInfo[1]); 46 | } 47 | #endif 48 | 49 | } 50 | /* 51 | if (in_perspective == 1.0) // perspective 52 | { 53 | ze = (zNear * zFar) / (zFar - zb * (zFar - zNear)); 54 | } 55 | else // orthographic proj 56 | { 57 | ze = zNear + zb * (zFar - zNear); 58 | } 59 | */ 60 | void main() 61 | { 62 | ivec2 intCoord; 63 | vec2 texCoord; 64 | 65 | if (setupCoordFull(intCoord, texCoord)) return; 66 | 67 | float depth = textureLod(inputTexture, texCoord.xy, 0).x; 68 | float linDepth = reconstructCSZ(depth, control.projReconstruct); 69 | imageStore(imgLinearDepth, intCoord, vec4(linDepth,0,0,0)); 70 | #if NVHBAO_SKIP_INTERPASS 71 | ivec2 FullResPos = intCoord; 72 | ivec2 Offset = FullResPos & 3; 73 | int SliceId = Offset.y * 4 + Offset.x; 74 | ivec2 QuarterResPos = FullResPos >> 2; 75 | imageStore(imgLinearDepthArray, ivec3(QuarterResPos, SliceId), vec4(linDepth,0,0,0)); 76 | #endif 77 | } 78 | -------------------------------------------------------------------------------- /hbao_pass.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | 21 | #ifndef HBAOPASS_H__ 22 | #define HBAOPASS_H__ 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | ////////////////////////////////////////////////////////////////////////// 34 | 35 | /// HbaoSystem implements a screen-space 36 | /// ambient occlusion effect using 37 | /// horizon-based ambient occlusion. 38 | /// See https://github.com/nvpro-samples/gl_ssao 39 | /// for more details 40 | 41 | class HbaoPass 42 | { 43 | public: 44 | static const int RANDOM_SIZE = 4; 45 | static const int RANDOM_ELEMENTS = RANDOM_SIZE * RANDOM_SIZE; 46 | 47 | struct Config 48 | { 49 | VkFormat targetFormat; 50 | uint32_t maxFrames; 51 | }; 52 | 53 | void init(VkDevice device, nvvk::ResourceAllocator* allocator, nvvk::ShaderModuleManager* shaderManager, const Config& config); 54 | void reloadShaders(); 55 | void deinit(); 56 | 57 | struct FrameConfig 58 | { 59 | bool blend; 60 | 61 | uint32_t sourceWidthScale; 62 | uint32_t sourceHeightScale; 63 | 64 | uint32_t targetWidth; 65 | uint32_t targetHeight; 66 | 67 | VkDescriptorImageInfo sourceDepth; 68 | VkDescriptorImageInfo targetColor; 69 | }; 70 | 71 | struct FrameIMGs 72 | { 73 | nvvk::Texture depthlinear, viewnormal, result, blur, resultarray, deptharray; 74 | }; 75 | 76 | struct Frame 77 | { 78 | uint32_t slot = ~0u; 79 | 80 | FrameIMGs images; 81 | int width; 82 | int height; 83 | 84 | FrameConfig config; 85 | }; 86 | 87 | bool initFrame(Frame& frame, const FrameConfig& config, VkCommandBuffer cmd); 88 | void deinitFrame(Frame& frame); 89 | 90 | 91 | struct View 92 | { 93 | bool isOrtho; 94 | float nearPlane; 95 | float farPlane; 96 | float halfFovyTan; 97 | glm::mat4 projectionMatrix; 98 | }; 99 | 100 | struct Settings 101 | { 102 | View view; 103 | 104 | float unit2viewspace = 1.0f; 105 | float intensity = 1.0f; 106 | float radius = 1.0f; 107 | float bias = 0.1f; 108 | float blurSharpness = 40.0f; 109 | }; 110 | 111 | // before: must do appropriate barriers for color write access and depth read access 112 | // after: from compute write to whatever output image needs 113 | void cmdCompute(VkCommandBuffer cmd, const Frame& frame, const Settings& settings) const; 114 | 115 | private: 116 | struct Shaders 117 | { 118 | nvvk::ShaderModuleID depth_linearize, viewnormal, blur, blur_apply, deinterleave, calc, reinterleave; 119 | }; 120 | 121 | struct Pipelines 122 | { 123 | VkPipeline depth_linearize = VK_NULL_HANDLE; 124 | VkPipeline viewnormal = VK_NULL_HANDLE; 125 | VkPipeline blur = VK_NULL_HANDLE; 126 | VkPipeline blur_apply = VK_NULL_HANDLE; 127 | VkPipeline deinterleave = VK_NULL_HANDLE; 128 | VkPipeline calc = VK_NULL_HANDLE; 129 | VkPipeline reinterleave = VK_NULL_HANDLE; 130 | }; 131 | 132 | VkDevice m_device; 133 | nvvk::ResourceAllocator* m_allocator; 134 | nvvk::ShaderModuleManager* m_shaderManager; 135 | nvh::TRangeAllocator<1> m_slots; 136 | Config m_config; 137 | 138 | nvvk::DescriptorSetContainer m_setup; 139 | 140 | nvvk::Buffer m_ubo; 141 | VkDescriptorBufferInfo m_uboInfo; 142 | 143 | VkSampler m_linearSampler; 144 | 145 | Shaders m_shaders; 146 | Pipelines m_pipelines; 147 | 148 | glm::vec4 m_hbaoRandom[RANDOM_ELEMENTS]; 149 | 150 | void updatePipelines(); 151 | void updateUbo(VkCommandBuffer cmd, const Frame& frame, const Settings& settings) const; 152 | }; 153 | 154 | #endif -------------------------------------------------------------------------------- /hbao_reinterleave.comp.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #include "hbao.h" 25 | 26 | layout(binding=NVHBAO_MAIN_TEX_RESULTARRAY) uniform sampler2DArray texResultsArray; 27 | #if NVHBAO_BLUR 28 | layout(binding=NVHBAO_MAIN_IMG_RESULT, rg16f) uniform image2D imgResult; 29 | #else 30 | layout(binding=NVHBAO_MAIN_IMG_RESULT, r8) uniform image2D imgResult; 31 | #endif 32 | 33 | //---------------------------------------------------------------------------------- 34 | 35 | void main() { 36 | ivec2 intCoord; 37 | vec2 texCoord; 38 | 39 | if (setupCoordFull(intCoord, texCoord)) return; 40 | 41 | ivec2 FullResPos = intCoord; 42 | ivec2 Offset = FullResPos & 3; 43 | int SliceId = Offset.y * 4 + Offset.x; 44 | ivec2 QuarterResPos = FullResPos >> 2; 45 | 46 | #if NVHBAO_BLUR 47 | imageStore(imgResult, intCoord, vec4(texelFetch( texResultsArray, ivec3(QuarterResPos, SliceId), 0).xy,0,0)); 48 | #else 49 | imageStore(imgResult, intCoord, vec4(texelFetch( texResultsArray, ivec3(QuarterResPos, SliceId), 0).x)); 50 | #endif 51 | } 52 | -------------------------------------------------------------------------------- /hbao_viewnormal.comp.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2018-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2018-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #version 460 21 | #extension GL_GOOGLE_include_directive : enable 22 | #extension GL_EXT_control_flow_attributes : require 23 | 24 | #include "hbao.h" 25 | 26 | layout(binding=NVHBAO_MAIN_TEX_LINDEPTH) uniform sampler2D texLinearDepth; 27 | layout(binding=NVHBAO_MAIN_IMG_VIEWNORMAL,rgba8) uniform image2D imgViewNormal; 28 | 29 | //---------------------------------------------------------------------------------- 30 | 31 | vec3 UVToView(vec2 uv, float eye_z) 32 | { 33 | return vec3((uv * control.projInfo.xy + control.projInfo.zw) * (control.projOrtho != 0 ? 1. : eye_z), eye_z); 34 | } 35 | 36 | vec3 FetchViewPos(vec2 UV) 37 | { 38 | float ViewDepth = textureLod(texLinearDepth,UV,0).x; 39 | return UVToView(UV, ViewDepth); 40 | } 41 | 42 | vec3 MinDiff(vec3 P, vec3 Pr, vec3 Pl) 43 | { 44 | vec3 V1 = Pr - P; 45 | vec3 V2 = P - Pl; 46 | return (dot(V1,V1) < dot(V2,V2)) ? V1 : V2; 47 | } 48 | 49 | vec3 ReconstructNormal(vec2 UV, vec3 P) 50 | { 51 | vec3 Pr = FetchViewPos(UV + vec2(control.InvFullResolution.x, 0)); 52 | vec3 Pl = FetchViewPos(UV + vec2(-control.InvFullResolution.x, 0)); 53 | vec3 Pt = FetchViewPos(UV + vec2(0, control.InvFullResolution.y)); 54 | vec3 Pb = FetchViewPos(UV + vec2(0, -control.InvFullResolution.y)); 55 | return normalize(cross(MinDiff(P, Pr, Pl), MinDiff(P, Pt, Pb))); 56 | } 57 | 58 | //---------------------------------------------------------------------------------- 59 | 60 | void main() { 61 | ivec2 intCoord; 62 | vec2 texCoord; 63 | 64 | if (setupCoordFull(intCoord, texCoord)) return; 65 | 66 | vec3 P = FetchViewPos(texCoord); 67 | vec3 N = ReconstructNormal(texCoord, P); 68 | 69 | imageStore(imgViewNormal, intCoord, vec4(N*0.5 + 0.5,0)); 70 | } 71 | -------------------------------------------------------------------------------- /meshset_gltf.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | // Contains an importer from glTF files to a MeshSet, optionally with displacement. 23 | 24 | #include 25 | #include 26 | #include "baryset.hpp" 27 | 28 | // barySet can be nullptr, then barycentric related data isn't loaded 29 | bool loadGLTF(MeshSet& meshSet, BaryAttributesSet* barySet, const char* filename); 30 | -------------------------------------------------------------------------------- /meshset_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "meshset.hpp" 23 | #include "resources_base_vk.hpp" 24 | #include "vk_nv_micromesh.h" 25 | 26 | namespace microdisp { 27 | struct TriDisplacementVK; 28 | struct MicromeshSetCompressedRayTracedVK; 29 | 30 | struct MeshAttributesVK 31 | { 32 | RBuffer normals; 33 | RBuffer tangents; 34 | RBuffer bitangents; 35 | RBuffer tex0s; 36 | 37 | void init(ResourcesVK& res, const MeshSet& meshSet); 38 | void deinit(ResourcesVK& res); 39 | }; 40 | 41 | class MeshSetVK 42 | { 43 | public: 44 | RBuffer binding; 45 | 46 | RBuffer positions; 47 | RBuffer indices; 48 | RBuffer instances; 49 | 50 | RBuffer displacementDirections; 51 | RBuffer displacementDirectionBounds; 52 | RBuffer displacementEdgeFlags; 53 | 54 | MeshAttributesVK attr; 55 | 56 | struct MeshRT 57 | { 58 | nvvk::AccelKHR blas; 59 | VkAccelerationStructureGeometryKHR geometry; 60 | VkAccelerationStructureBuildGeometryInfoKHR geometryInfo; 61 | VkAccelerationStructureTrianglesDisplacementMicromapNV geometryDisplacement; 62 | VkAccelerationStructureBuildRangeInfoKHR blasRange; 63 | }; 64 | 65 | std::vector rtMeshes; 66 | nvvk::AccelKHR sceneTlas; 67 | VkWriteDescriptorSetAccelerationStructureKHR sceneTlasInfo; 68 | 69 | std::vector materialTextures; 70 | 71 | void init(ResourcesVK& resources, const MeshSet& meshSet); 72 | void initNormalMaps(ResourcesVK& resources, const MeshSet& meshSet); 73 | 74 | // if the meshSet doesn't have the appropriate content, existing resource will be destroyed 75 | // and binding updated to reflect that 76 | void initDisplacementDirections(ResourcesVK& resources, const MeshSet& meshSet); 77 | void initDisplacementBounds(ResourcesVK& resources, const MeshSet& meshSet); 78 | void initDisplacementEdgeFlags(ResourcesVK& resources, const MeshSet& meshSet); 79 | 80 | void initRayTracingGeometry(ResourcesVK& resources, 81 | const MeshSet& meshSet, 82 | const MicromeshSetCompressedRayTracedVK* micromeshSetRT = nullptr); 83 | void initRayTracingScene(ResourcesVK& resources, const MeshSet& meshSet, const MicromeshSetCompressedRayTracedVK* micromeshSetRT = nullptr); 84 | void deinit(ResourcesVK& resources); 85 | void deinitRayTracing(ResourcesVK& resources); 86 | 87 | void updateBinding(ResourcesVK& resources); 88 | void updateInstances(ResourcesVK& resources, const MeshSet& meshSet); 89 | }; 90 | 91 | } // namespace microdisp -------------------------------------------------------------------------------- /micromesh_binpack_decl.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | ////////////////////////////////////////////////////////////////////////// 21 | // MicroBinPack 22 | // 23 | // For dynamic level of detail handling and to improve efficiency for 24 | // micromeshes with low subdivsion levels we allow packing multiple 25 | // decoding jobs in a single subgroup. 26 | // A single decoding job is however targeting one subdivision level 27 | // uniformly, so this struct will be filled in such fashion that 28 | // it bins decoding jobs based on target subdivision level. 29 | // See `micromesh_binpack.glsl` 30 | // 31 | // Typically used as output of task-shader and input for mesh-shader 32 | 33 | struct MicroBinPack 34 | { 35 | // arrays are ordered by bins of micromeshes 36 | // with equal packSize 37 | // within a bin subIDs are ordered ascending 38 | // 39 | // the arrays may contain ranges for subdiv level 7 40 | // which equals to being invalid and these make no 41 | // contributions to meshlet counts 42 | 43 | // pack 44 | // -------------- 45 | // exclusive prefix where each meshlet starts 46 | // for packSize == 1: multiple meshlets may be created 47 | // per input micromesh 48 | // otherwise: packSize many are packed per meshlet 49 | // 50 | // 15 bit : meshlet workgroup offset 51 | // for subdiv limited to 5: 52 | // worst-case is (SUBGROUP_SIZE-1) * 16, all previous threads requiring 16 meshlets 53 | // SUBGROUP_SIZE == 32: 9 bits 54 | // SUBGROUP_SIZE == 64: 10 bits 55 | // for subdiv limited to 7: 56 | // worst-case is (SUBGROUP_SIZE-1) * (highest resolution per base triangle/64) 57 | // SUBGROUP_SIZE == 32: 13 bits and highest res is 16 384 tris 58 | // SUBGROUP_SIZE == 64: 14 bits and highest res is 16 384 tris 59 | // 60 | // 2 bit : pack size (1,4,8,16) (actual) how many micromeshes per meshlet 61 | // 0,1,2,3 (encoded) 62 | // 63 | // info 64 | // ------------- 65 | // 6 bit : microID relative to baseID 66 | // SUBGROUP_SIZE == 32: 5 bits 67 | // SUBGROUP_SIZE == 64: 6 bits 68 | // 69 | // 4 bit : target subdiv level 70 | // needs to be able to store MICRO_BIN_INVALID_SUBDIV 71 | 72 | uint32_t pack_info[SUBGROUP_SIZE]; 73 | uint32_t baseID; 74 | }; 75 | 76 | #define MICRO_BIN_PACK_OFFSET_MASK ((1<<15)-1) 77 | #define MICRO_BIN_PACK_SIZE_MASK ((1<<2)-1) 78 | #define MICRO_BIN_PACK_SIZE_SHIFT 15 79 | #define MICRO_BIN_INFO_SHIFT 17 80 | #define MICRO_BIN_INFO_ID_MASK ((1<<6)-1) 81 | #define MICRO_BIN_INFO_LVL_SHIFT 6 82 | #define MICRO_BIN_INVALID_SUBDIV 15 83 | #define MICRO_BIN_SPLIT_SUBDIV 3 84 | -------------------------------------------------------------------------------- /micromesh_binpack_flat_decl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | ////////////////////////////////////////////////////////////////////////// 20 | // MicroBinPackFlat 21 | // 22 | // Encodes the task performed by one decoding warp. 23 | // One flat packing is either up to 16 packed decoding jobs 24 | // or a single. If single (target subdiv >= 3) then it needs 25 | // to provide which partition within the primitive it is 26 | // decoding. 27 | 28 | struct MicroBinPackFlat 29 | { 30 | uint32_t pack; 31 | // 4: target subdiv level 32 | // if target subdiv >= 3 33 | // 28: final ID 34 | // else (target subdiv < 3) 35 | // 28: base ID 36 | 37 | uint32_t partOrMask; 38 | // if target subdiv >= 3 39 | // 32: partID 40 | // else (target subdiv < 3) 41 | // 32: relative ID bitmask 42 | // bit set for each relative ID active in packing 43 | // only works for SUBGROUP_SIZE == 32, otherwise need 64 bit here and adjust a few more 44 | 45 | uint32_t instanceID; 46 | }; 47 | 48 | #define MICRO_BIN_FLAT_PACK_LVL_MASK ((1<<4)-1) 49 | #define MICRO_BIN_FLAT_PACK_BASE_SHIFT 4 50 | #define MICRO_BIN_SPLIT_SUBDIV 3 51 | #define MICRO_BIN_INVALID_SUBDIV 15 52 | 53 | #if MAX_BASE_SUBDIV != 5 54 | #error "bits " 55 | #endif -------------------------------------------------------------------------------- /micromesh_compressed_rt_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "meshset.hpp" 23 | #include "baryset.hpp" 24 | #include "resources_base_vk.hpp" 25 | #include "vk_nv_micromesh.h" 26 | 27 | namespace microdisp { 28 | 29 | struct MicromeshSetCompressedRayTracedVK 30 | { 31 | RBuffer umajor2bmap; 32 | RBuffer instanceAttributes; 33 | RBuffer binding; 34 | 35 | // each bary::Group is mapped to one VkMicromapEXT 36 | struct Group 37 | { 38 | VkMicromapEXT micromap = nullptr; 39 | // referenced by above 40 | RBuffer micromeshData; 41 | 42 | std::vector usages; 43 | }; 44 | 45 | struct Info 46 | { 47 | std::vector groups; 48 | }; 49 | 50 | // maps 1:1 to BaryAttributesSet 51 | std::vector displacements; 52 | 53 | // maps 1:1 to meshes 54 | struct MeshData 55 | { 56 | RBuffer attrNormals; 57 | RBuffer attrTriangles; 58 | }; 59 | std::vector meshDatas; 60 | 61 | void init(ResourcesVK& res, const MeshSet& meshSet, const BaryAttributesSet& barySet, uint32_t numThreads); 62 | void deinit(ResourcesVK& res); 63 | 64 | // creates buffers & uploads micro vertex attribute normals 65 | void initAttributeNormals(ResourcesVK& res, const MeshSet& meshSet, const BaryAttributesSet& barySet, uint32_t numThreads = 0); 66 | 67 | }; 68 | 69 | } // namespace microdisp 70 | -------------------------------------------------------------------------------- /micromesh_compressed_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include 23 | #include "meshset.hpp" 24 | #include "baryset.hpp" 25 | #include "resources_base_vk.hpp" 26 | 27 | namespace microdisp { 28 | 29 | struct MicromeshCombinedData; 30 | 31 | struct MicromeshSetCompressedVK 32 | { 33 | RBuffer umajor2bmap; 34 | 35 | RBuffer vertices; 36 | RBuffer descends; 37 | RBuffer triangleIndices; 38 | 39 | struct MeshData 40 | { 41 | RBuffer binding; 42 | MicromeshCombinedData* combinedData = nullptr; 43 | 44 | // Either base- or sub-triangle data is used and 45 | // never both. We kept separate variables 46 | // for clarity 47 | 48 | RBuffer baseTriangles; 49 | RBuffer baseSpheres; 50 | 51 | RBuffer subTriangles; 52 | RBuffer subSpheres; 53 | 54 | RBuffer distances; 55 | RBuffer mipDistances; 56 | 57 | RBuffer attrNormals; 58 | RBuffer attrTriangles; 59 | 60 | // just for visualization purposes not for rendering 61 | RBuffer baseTriangleMinMaxs; 62 | 63 | // either sub or base triangle count 64 | uint32_t microTriangleCount; 65 | }; 66 | 67 | std::vector meshDatas; 68 | 69 | bool hasBaseTriangles = false; 70 | bool usedFormats[uint32_t(bary::BlockFormatDispC1::eR11_unorm_lvl5_pack1024) + 1]; 71 | 72 | // creates buffers & uploads typical data that is agnostic of the specific 73 | // rasterization decoder chosen. 74 | // see various `micromesh_decoder_...` files for the full init sequence 75 | 76 | void initBasics(ResourcesVK& res, const MeshSet& meshSet, const BaryAttributesSet& barySet, bool useBaseTriangles, bool useMips); 77 | 78 | // creates buffers & uploads micro vertex attribute normals 79 | void initAttributeNormals(ResourcesVK& res, const MeshSet& meshSet, const BaryAttributesSet& barySet, uint32_t numThreads = 0); 80 | 81 | void deinit(ResourcesVK& res); 82 | 83 | // updates the state of `MeshData::combinedData` to retrieve most buffer addresses and store them 84 | // in the binding buffer 85 | void uploadMeshDatasBinding(nvvk::StagingMemoryManager* staging, VkCommandBuffer cmd); 86 | }; 87 | 88 | } // namespace microdisp -------------------------------------------------------------------------------- /micromesh_culling_compressed.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #define MICRO_LOD_CLAMP 0 21 | 22 | uint cullAndLodMicroBaseTriangle(uint microID, inout bool valid) 23 | { 24 | MicromeshBaseTri microBaseTri = microdata.basetriangles.d[microID]; 25 | 26 | mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix; 27 | 28 | uint baseSubdiv = micromesh_getBaseSubdiv(microBaseTri); 29 | 30 | #if USE_TRI_LOD 31 | uint tri = microID + push.firstTriangle;; 32 | uvec3 triIndices = uvec3( mesh.indices.d[tri * 3 + 0], 33 | mesh.indices.d[tri * 3 + 1], 34 | mesh.indices.d[tri * 3 + 2]) + push.firstVertex; 35 | 36 | vec3 v0 = mesh.positions.d[triIndices.x].xyz; 37 | vec3 v1 = mesh.positions.d[triIndices.y].xyz; 38 | vec3 v2 = mesh.positions.d[triIndices.z].xyz; 39 | 40 | f16vec3 d0 = mesh.dispDirections.d[triIndices.x].xyz; 41 | f16vec3 d1 = mesh.dispDirections.d[triIndices.y].xyz; 42 | f16vec3 d2 = mesh.dispDirections.d[triIndices.z].xyz; 43 | 44 | #if USE_DIRECTION_BOUNDS 45 | // fixme this logic is rather crude 46 | // to be more exact we would need to generate all prismoid 47 | // positions like the CPU code does 48 | 49 | boundsVec2 bounds0 = mesh.dispDirectionBounds.d[triIndices.x]; 50 | boundsVec2 bounds1 = mesh.dispDirectionBounds.d[triIndices.y]; 51 | boundsVec2 bounds2 = mesh.dispDirectionBounds.d[triIndices.z]; 52 | 53 | v0 = v0 + d0 * bounds0.x; 54 | v1 = v1 + d1 * bounds1.x; 55 | v2 = v2 + d2 * bounds2.x; 56 | 57 | d0 = d0 * float16_t(bounds0.y); 58 | d1 = d1 * float16_t(bounds1.y); 59 | d2 = d2 * float16_t(bounds2.y); 60 | 61 | v0 = v0 + d0 * float16_t(0.5); 62 | v1 = v1 + d1 * float16_t(0.5); 63 | v2 = v2 + d2 * float16_t(0.5); 64 | #endif 65 | 66 | // Generate vertices 67 | vec3 va = (worldMatrix * vec4(v0,1)).xyz; 68 | vec3 vb = (worldMatrix * vec4(v1,1)).xyz; 69 | vec3 vc = (worldMatrix * vec4(v2,1)).xyz; 70 | 71 | float maxDirLength = max(max(length(d0), 72 | length(d1)), 73 | length(d2)); 74 | 75 | int cullDistance = micromesh_getCullDist(microBaseTri); 76 | float maxDisplacement = micromesh_getFloatDisplacement(cullDistance, push.scale_bias); 77 | 78 | uint lodSubdiv = baseSubdiv; 79 | vec3 sphereCenter = (va + vb + vc) / 3.0f; 80 | float sphereRadius = max(max(length(va - sphereCenter), length(vb - sphereCenter)), length(vc - sphereCenter)); 81 | sphereRadius = sphereRadius + (maxDisplacement * maxDirLength) * length(mat3(worldMatrix) * vec3(1)); 82 | #else 83 | uint lodSubdiv = baseSubdiv; 84 | vec4 sphere = microdata.basespheres.d[microID]; 85 | vec3 sphereCenter = (worldMatrix * vec4(sphere.xyz,1)).xyz; 86 | float sphereRadius = sphere.w * length(mat3(worldMatrix) * vec3(1)); 87 | #endif 88 | 89 | uint targetSubdiv = MICRO_BIN_INVALID_SUBDIV; 90 | 91 | #if USE_TRI_LOD 92 | valid = valid && cullAndLodTriangle(va, vb, vc, sphereCenter, sphereRadius, lodSubdiv, targetSubdiv); 93 | #else 94 | valid = valid && cullAndLodSphere(sphereCenter, sphereRadius, lodSubdiv, targetSubdiv); 95 | #endif 96 | 97 | if (valid) { 98 | // subdivTarget is now relative to lodSubdiv, 99 | // but we need it relative to baseSubdiv 100 | uint refSubdiv = baseSubdiv; 101 | uint refDelta = refSubdiv; 102 | #if MICRO_LOD_CLAMP 103 | refDelta = min(refDelta, MICRO_LOD_CLAMP); 104 | #endif 105 | targetSubdiv = (refSubdiv - min(refDelta, lodSubdiv - targetSubdiv)); 106 | } 107 | 108 | return targetSubdiv; 109 | } -------------------------------------------------------------------------------- /micromesh_culling_uncompressed.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | 21 | ////////////////////////////////////////////////////////////////////////// 22 | 23 | uint cullAndLodBaseTriangle(uint triLocal, uint baseSubdiv, inout bool valid) 24 | { 25 | mat4 worldMatrix = mesh.instances.d[push.instanceID].worldMatrix; 26 | 27 | uint tri = triLocal + push.firstTriangle; 28 | 29 | #if USE_TRI_LOD 30 | uvec3 triIndices = uvec3( mesh.indices.d[tri * 3 + 0], 31 | mesh.indices.d[tri * 3 + 1], 32 | mesh.indices.d[tri * 3 + 2]) + push.firstVertex; 33 | 34 | // Generate vertices 35 | vec3 va = (worldMatrix * vec4(mesh.positions.d[triIndices.x].xyz,1)).xyz; 36 | vec3 vb = (worldMatrix * vec4(mesh.positions.d[triIndices.y].xyz,1)).xyz; 37 | vec3 vc = (worldMatrix * vec4(mesh.positions.d[triIndices.z].xyz,1)).xyz; 38 | 39 | float maxDisplacement = dbitsUnpack(microdata.triangleBitsMinMax.d[triLocal * 2 + 1]); 40 | maxDisplacement = maxDisplacement * push.scale_bias.x + push.scale_bias.y; 41 | 42 | uint lodSubdiv = baseSubdiv; 43 | vec3 sphereCenter = (va + vb + vc) / 3.0f; 44 | float sphereRadius = max(max(length(va - sphereCenter), length(vb - sphereCenter)), length(vc - sphereCenter)); 45 | sphereRadius = sphereRadius + (maxDisplacement) * length(mat3(worldMatrix) * vec3(1)); 46 | #else 47 | uint lodSubdiv = baseSubdiv; 48 | vec4 sphere = microdata.basespheres.d[triLocal]; 49 | vec3 sphereCenter = (worldMatrix * vec4(sphere.xyz,1)).xyz; 50 | float sphereRadius = sphere.w * length(mat3(worldMatrix) * vec3(1)); 51 | #endif 52 | 53 | uint targetSubdiv = MICRO_BIN_INVALID_SUBDIV; 54 | 55 | #if USE_TRI_LOD 56 | valid = valid && cullAndLodTriangle(va, vb, vc, sphereCenter, sphereRadius, lodSubdiv, targetSubdiv); 57 | #else 58 | valid = valid && cullAndLodSphere(sphereCenter, sphereRadius, lodSubdiv, targetSubdiv); 59 | #endif 60 | 61 | return targetSubdiv; 62 | } -------------------------------------------------------------------------------- /micromesh_decoder_api.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | /////////////////////////////////////////////////////////////// 21 | // public api 22 | 23 | // High-level api intended to be used by mesh or compute shaders. 24 | // Different SubgroupMicromeshDecoder implementations are provided 25 | // in their respective glsl files. 26 | // - `micromesh_decoder_basetri.glsl` 27 | // - `micromesh_decoder_microtri.glsl` 28 | // 29 | // Ideally mesh/compute shaders only use the functions provided 30 | // here. None of the lower-level details. 31 | 32 | // how many threads are used for each part, partSubdiv [0,3] 33 | uint smicrodec_getThreadCount(uint partSubdiv); 34 | 35 | // sets up the decoder state 36 | // must be called in subgroup-uniform branch 37 | // 38 | // arguments can be divergent in subgroup, but must 39 | // be uniform for threads with equal `cfg.packID`. 40 | void smicrodec_subgroupInit(inout SubgroupMicromeshDecoder sdec, 41 | MicroDecoderConfig cfg, 42 | MicromeshBaseTri microBaseTri, 43 | uint firstMicro, 44 | uint firstTriangle, 45 | uint firstData, 46 | uint firstMipData); 47 | 48 | uint smicrodec_getNumTriangles(inout SubgroupMicromeshDecoder sdec); 49 | uint smicrodec_getNumVertices (inout SubgroupMicromeshDecoder sdec); 50 | uint smicrodec_getMeshTriangle(inout SubgroupMicromeshDecoder sdec); 51 | uint smicrodec_getBaseSubdiv (inout SubgroupMicromeshDecoder sdec); 52 | uint smicrodec_getMicroSubdiv (inout SubgroupMicromeshDecoder sdec); 53 | uint smicrodec_getFormatIdx (inout SubgroupMicromeshDecoder sdec); 54 | 55 | // the decoding process of vertices and triangles may need multiple iterations 56 | // within the subgroup (known at compile-time) 57 | uint smicrodec_getIterationCount(); 58 | 59 | // retrieve the triangle information for the part being decoded 60 | struct MicroDecodedTriangle 61 | { 62 | bool valid; // this thread has work to do 63 | uint localIndex; // local index relative to current part 64 | uint outIndex; // output index across parts in all decoders within subgroup 65 | 66 | uvec3 indices; // is adjusted for vertex output index 67 | }; 68 | 69 | MicroDecodedTriangle smicrodec_getTriangle (inout SubgroupMicromeshDecoder sdec, uint iterationIndex); 70 | 71 | // retrieve the vertex information for the part being decoded 72 | // must be called in subgroup-uniform branch 73 | struct MicroDecodedVertex 74 | { 75 | bool valid; // this thread has work to do 76 | uint localIndex; // local index relative to current part 77 | uint outIndex; // output index across parts in all decoders within subgroup 78 | 79 | ivec2 uv; // relative to base triangle 80 | #if MICRO_DECODER == MICRO_DECODER_MICROTRI_THREAD && MICRO_MTRI_USE_INTRINSIC 81 | uint blasBasePrimitiveID; // base triangle within blas, used with intrinsic 82 | #else 83 | int displacement; // raw displacement 84 | #endif 85 | vec3 bary; // relative to base triangle 86 | }; 87 | 88 | MicroDecodedVertex smicrodec_subgroupGetVertex (inout SubgroupMicromeshDecoder sdec, uint iterationIndex); 89 | 90 | vec4 smicrodec_getVertexPos(inout MicroDecodedVertex decoded, 91 | vec3 v0, vec3 v1, vec3 v2, f16vec3 d0, f16vec3 d1, f16vec3 d2, 92 | mat4 worldMatrix, 93 | uint instanceID, 94 | dispVec2 scale_bias) 95 | { 96 | #if MICRO_DECODER == MICRO_DECODER_MICROTRI_THREAD && MICRO_MTRI_USE_INTRINSIC 97 | // in this sample the blas is constructed with just one geometry 98 | int geometryIndex = 0; 99 | return worldMatrix * vec4(fetchMicroTriangleVertexPositionNV(sceneTlas, int(instanceID), geometryIndex, int(decoded.blasBasePrimitiveID), decoded.uv), 1.0); 100 | #else 101 | float dispDistance = micromesh_getFloatDisplacement(decoded.displacement, scale_bias); 102 | 103 | // Compute interpolation 104 | vec3 dispDirection = getInterpolated(d0, d1, d2, decoded.bary); 105 | #if USE_NORMALIZED_DIR 106 | dispDirection = normalize(dispDirection); 107 | #endif 108 | 109 | vec3 pos = getInterpolated(v0, v1, v2, decoded.bary) + vec3(dispDirection) * dispDistance; 110 | return worldMatrix * vec4(pos, 1.0); 111 | #endif 112 | } -------------------------------------------------------------------------------- /micromesh_decoder_basetri_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "micromesh_compressed_vk.hpp" 23 | 24 | namespace microdisp { 25 | 26 | class MicroSplitParts; 27 | 28 | class MicromeshBaseTriangleDecoderVK 29 | { 30 | public: 31 | MicromeshBaseTriangleDecoderVK(MicromeshSetCompressedVK& microSet) 32 | : m_micro(microSet) 33 | { 34 | } 35 | 36 | bool init(ResourcesVK& res, const MeshSet& meshSet, const BaryAttributesSet& barySet, bool withAttributes, uint32_t numThreads); 37 | 38 | private: 39 | MicromeshSetCompressedVK& m_micro; 40 | 41 | void uploadMicroBaseTriangles(nvvk::StagingMemoryManager* staging, 42 | VkCommandBuffer cmd, 43 | const MeshSet& meshSet, 44 | const BaryAttributesSet& barySet, 45 | uint32_t meshID, 46 | uint32_t numThreads); 47 | 48 | void uploadVertices(nvvk::StagingMemoryManager* staging, VkCommandBuffer cmd, const MicroSplitParts& splits); 49 | }; 50 | 51 | 52 | } // namespace microdisp 53 | -------------------------------------------------------------------------------- /micromesh_decoder_config.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | /////////////////////////////////////////////////////////////// 21 | // public api 22 | 23 | // Interface that describes key configuration of which 24 | // portion of triangles and at what level of detail 25 | // are to be decoded. 26 | 27 | struct MicroDecoderConfig { 28 | // baseTriID or subTriID 29 | // depending on decoder type 30 | uint microID; 31 | 32 | // a micromesh may need multiple parts to be decoded, each part has a maximum of 33 | // MICRO_PART_MAX_PRIMITIVES triangles (64) and 34 | // MICRO_PART_MAX_VERTICES vertices (45) 35 | uint partID; 36 | // subdivision resolution of the part being decoded [0,3] 37 | uint partSubdiv; 38 | 39 | // target subdivision [0, microSubdiv] 40 | uint targetSubdiv; 41 | 42 | // When multiple decoder states are packed within a subgroup 43 | // packID is the unique identifier for each such state. 44 | // Each pack will use packThreads many threads, and packThreadID 45 | // specifies which thread is calling the function. 46 | // packThreads * packID + packThreadID < SUBGROUP_SIZE 47 | 48 | uint packID; 49 | uint packThreads; 50 | uint packThreadID; 51 | 52 | // decoder could be invalid 53 | // if subgroup contains some that are not 54 | // supposed to participate 55 | bool valid; 56 | }; 57 | -------------------------------------------------------------------------------- /micromesh_decoder_microtri_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "micromesh_compressed_vk.hpp" 23 | 24 | namespace microdisp { 25 | 26 | class MicroSplitParts; 27 | 28 | class MicromeshMicroTriangleDecoderVK 29 | { 30 | public: 31 | MicromeshMicroTriangleDecoderVK(MicromeshSetCompressedVK& microSet) 32 | : m_micro(microSet) 33 | { 34 | } 35 | 36 | // all must have BARY_BASE_FLAG_UNIFORM_SUBPRIMITIVE_BLOCK_FORMATS_BIT set 37 | bool init(ResourcesVK& res, const MeshSet& meshSet, const BaryAttributesSet& barySet, bool withAttributes, bool useIntrinsic, uint32_t numThreads); 38 | 39 | private: 40 | MicromeshSetCompressedVK& m_micro; 41 | 42 | void uploadMicroBaseTriangles(nvvk::StagingMemoryManager* staging, 43 | VkCommandBuffer cmd, 44 | const MeshSet& meshSet, 45 | const BaryAttributesSet& barySet, 46 | uint32_t meshID, 47 | uint32_t numThreads); 48 | 49 | void uploadDescends(nvvk::StagingMemoryManager* staging, VkCommandBuffer cmd, const MicroSplitParts& splits); 50 | 51 | void uploadVertices(nvvk::StagingMemoryManager* staging, VkCommandBuffer cmd, const MicroSplitParts& splits); 52 | }; 53 | 54 | 55 | } // namespace microdisp 56 | -------------------------------------------------------------------------------- /micromesh_loader.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | ///////////////////////////////////////////////////// 21 | 22 | // Data loading was separated to ease integration with other renderers/apis 23 | 24 | // Flat renderers need to pull per-mesh data from local pointers 25 | // hence the ability to change the variable via macro here. 26 | // Otherwise microdata is provided as a per-draw bound SSBO. 27 | #ifndef mesh_microdata 28 | #define mesh_microdata microdata 29 | #endif 30 | 31 | uint microdata_loadDistance(uint idx) 32 | { 33 | return mesh_microdata.distances.d[idx]; 34 | } 35 | 36 | uvec2 microdata_loadDistance2(uint idx) 37 | { 38 | uvec2s_in distances64 = uvec2s_in(mesh_microdata.distances); 39 | return distances64.d[idx]; 40 | } 41 | 42 | uint microdata_loadMipDistance(uint idx) 43 | { 44 | return mesh_microdata.mipDistances.d[idx]; 45 | } 46 | 47 | #ifndef decoder_microdata 48 | #define decoder_microdata microdata 49 | #endif 50 | 51 | uint microdata_loadFormatInfo(uint formatIdx, uint decodeSubdiv) 52 | { 53 | return uint(decoder_microdata.formats.d[formatIdx].width_start[decodeSubdiv]); 54 | } 55 | 56 | #if MICRO_DECODER == MICRO_DECODER_BASETRI_MIP_SHUFFLE 57 | MicromeshBTriVertex 58 | #elif MICRO_DECODER == MICRO_DECODER_MICROTRI_THREAD 59 | MicromeshMTriVertex 60 | #else 61 | MicromeshSTriVertex 62 | #endif 63 | microdata_loadMicromeshVertex(uint idx) 64 | { 65 | return decoder_microdata.vertices.d[idx]; 66 | } 67 | #if MICRO_DECODER == MICRO_DECODER_BASETRI_MIP_SHUFFLE 68 | MicromeshBTriDescend 69 | #elif MICRO_DECODER == MICRO_DECODER_MICROTRI_THREAD 70 | MicromeshMTriDescend 71 | #else 72 | MicromeshSTriDescend 73 | #endif 74 | microdata_loadMicromeshDescend(uint idx) 75 | { 76 | return decoder_microdata.descendInfos.d[idx]; 77 | } 78 | 79 | uint microdata_loadTriangleIndices(uint idx) 80 | { 81 | return decoder_microdata.triangleIndices.d[idx]; 82 | } -------------------------------------------------------------------------------- /micromesh_nopack_flatsplit.glsl: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | // MicroBinPackFlatSplit without bin packing!! 21 | // ------------------------------------------- 22 | // 23 | // Do NOT USE, only here to show perf benefits of binpacking. 24 | // 25 | // Variant of the `micromesh_binpack_flat.glsl` which uses the "flat" encoding 26 | // only for >= MICRO_BIN_SPLIT_SUBDIV and otherwise passes smaller triangles to MICROBINPACK_PROCESS 27 | // one at a time per subgroup. No binning is performed. 28 | // 29 | // passing the MicroBinPack as in/out struct 30 | // breaks things because of shading language semantics (copy in/out struct), 31 | // we need to reference the storage directly (could be task input/output or shared memory or global memory) 32 | // 33 | // user must specify the variable we modify by macro 34 | // All require: 35 | // 36 | // MICROBINPACK_USE_MESHLETCOUNT 0/1 37 | // 1: the numbers of meshlets is provided explictly 38 | // 0: the numbers of meshlets is derived from targetSubdiv 39 | // 40 | // MicroBinPackFlatSplit_subgroupPack requires 41 | // - MICROBINPACK_OUT and MICROBINPACK_OUT_ATOM and MICROBINPACK_OUT_MAX and MICROBINPACK_PROCESS 42 | 43 | // these functions are illustrated in the rasterization pdf document 44 | 45 | #if defined (MICROBINPACK_OUT) && defined(MICROBINPACK_OUT_ATOM) && defined(MICROBINPACK_OUT_MAX) && defined(MICROBINPACK_PROCESS) 46 | 47 | void MicroBinPackFlatSplit_subgroupPack( 48 | uint baseID, 49 | uint relativeID, 50 | uint targetSubdiv, 51 | uint targetMeshletCount, 52 | bool valid, 53 | uint instanceID 54 | ) 55 | { 56 | uint laneID = gl_SubgroupInvocationID; 57 | 58 | if (subgroupAll(!valid)){ 59 | return; 60 | } 61 | 62 | // NO BINPACKING 63 | // ---------------- 64 | 65 | // bin packs based on targetlevel 66 | // level 3,4,5 use 1 or more meshlets and can go in same bin 67 | uint packSubdiv = min(MICRO_BIN_SPLIT_SUBDIV, targetSubdiv); 68 | 69 | bool isCompact = packSubdiv < MICRO_BIN_SPLIT_SUBDIV; 70 | bool isFlat = !isCompact && valid; 71 | 72 | // # meshlet outputs 73 | // ------------ 74 | // figure out how many meshlets we output to flat 75 | 76 | uint meshletCount = 0; 77 | if (isFlat) { 78 | #if MICROBINPACK_USE_MESHLETCOUNT 79 | meshletCount = targetMeshletCount; 80 | #else 81 | // packSubdiv == 3, needs 1 or more meshlets due to splitting 82 | uint splitSubdiv = uint(max(int(targetSubdiv) - MICRO_BIN_SPLIT_SUBDIV, 0)); 83 | meshletCount = (1 << (splitSubdiv * 2)); 84 | #endif 85 | } 86 | 87 | // enqueue large into flat output 88 | uint offsetFlat = subgroupInclusiveAdd(meshletCount); 89 | 90 | uint startBin = 0; 91 | if (laneID == SUBGROUP_SIZE-1) 92 | { 93 | startBin = atomicAdd(MICROBINPACK_OUT_ATOM, offsetFlat); 94 | #if USE_STATS 95 | atomicAdd(stats.meshlets, offsetFlat); 96 | #endif 97 | } 98 | 99 | // was inclusiveAdd, need exclusive 100 | offsetFlat -= meshletCount; 101 | startBin = subgroupShuffle(startBin, SUBGROUP_SIZE-1); 102 | 103 | if (isFlat && startBin < MICROBINPACK_OUT_MAX) 104 | { 105 | MicroBinPackFlat flatPack; 106 | flatPack.instanceID = instanceID; 107 | flatPack.pack = targetSubdiv | ((baseID + relativeID) << MICRO_BIN_FLAT_PACK_BASE_SHIFT);\ 108 | 109 | for (uint32_t i = 0; i < meshletCount; i++) 110 | { 111 | flatPack.partOrMask = i; 112 | MICROBINPACK_OUT[offsetFlat + i + startBin] = flatPack; 113 | } 114 | } 115 | 116 | // directly rasterize all compact packs 117 | bool doProcess = (isCompact && valid); 118 | uvec4 processVote = subgroupBallot(doProcess); 119 | uint processCount = subgroupBallotBitCount(processVote); 120 | 121 | if (processCount == 0){ 122 | return; 123 | } 124 | 125 | if (laneID == 0) 126 | { 127 | #if USE_STATS 128 | atomicAdd(stats.meshlets, processCount); 129 | #endif 130 | } 131 | 132 | uint processPrefix = subgroupBallotExclusiveBitCount(processVote); 133 | for (uint p = 0; p < processCount; p++) 134 | { 135 | uvec4 batchVote = subgroupBallot(p == processPrefix); 136 | uint readID = subgroupBallotFindMSB(batchVote); 137 | 138 | uint sub_microID = subgroupShuffle(baseID + relativeID, readID); 139 | uint sub_targetSubdiv = subgroupShuffle(targetSubdiv, readID); 140 | 141 | MicroDecoderConfig cfg; 142 | 143 | cfg.microID = sub_microID; 144 | cfg.partID = 0; // < split subdiv doesn't have parts 145 | 146 | cfg.targetSubdiv = sub_targetSubdiv; 147 | cfg.partSubdiv = sub_targetSubdiv; 148 | 149 | cfg.packID = 0; 150 | cfg.packThreads = SUBGROUP_SIZE; 151 | cfg.packThreadID = laneID; 152 | 153 | cfg.valid = true; 154 | 155 | MICROBINPACK_PROCESS(cfg); 156 | } 157 | } 158 | #endif 159 | -------------------------------------------------------------------------------- /micromesh_uncompressed_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "barymap_vk.hpp" 23 | #include "baryset.hpp" 24 | #include "meshset.hpp" 25 | #include "resources_base_vk.hpp" 26 | 27 | namespace microdisp { 28 | 29 | // This is an analogue to BaryAttributesSet, except it stores its data in Vulkan 30 | // buffers instead of the CPU. Instead of sets of buffers for each BaryMesh, 31 | // BaryMeshSetVK contains buffers which store all BaryAttributes data concatenated. 32 | // This also includes a struct for getting the ranges of data used by each mesh. 33 | struct MicromeshSetUncompressedVK 34 | { 35 | BaryLevelsMapVK baryMap; 36 | 37 | struct MeshData 38 | { 39 | RBuffer binding; // Contains pointers to the start of each buffer. 40 | RBuffer distances; // Array of uint32_ts storing the raw bits of micro-vertex displacement values. 41 | 42 | RBuffer baseTriangles; // Array of MicromeshUncBaseTri. 1 per mesh triangle. 43 | RBuffer baseTriangleSpheres; // Array of vec4s containing information about bounding spheres for renderers. 1 per mesh triangle. 44 | RBuffer baseTrianglesMinMax; // Type of array depends on bary format. 2 per bary triangle. 45 | 46 | RBuffer attrNormals; // per micro-vertex octant encoded normals (optional) 47 | }; 48 | 49 | // 1:1 to meshSet.meshInfos 50 | std::vector meshDatas; 51 | 52 | void init(ResourcesVK& resources, const MeshSet& meshSet, const BaryAttributesSet& barySet, bool withAttributes, uint32_t numThreads); 53 | void deinit(ResourcesVK& resources); 54 | 55 | private: 56 | // Updates the values of `binding` with pointers to the start of each Vulkan buffer. 57 | void uploadBinding(ResourcesVK& resources, VkCommandBuffer cmd, const MeshData& meshData) const; 58 | 59 | void uploadFlatTriangles(ResourcesVK& resources, VkCommandBuffer cmd, const MeshSet& meshSet, const BaryAttributesSet& barySet, uint32_t meshID, uint32_t numThreads); 60 | }; 61 | 62 | } // namespace microdisp 63 | -------------------------------------------------------------------------------- /nvhiz_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #ifndef _NVHIZ_H__ 21 | #define _NVHIZ_H__ 22 | 23 | #include 24 | #include 25 | #include 26 | 27 | 28 | #include 29 | 30 | class NVHizVK 31 | { 32 | private: 33 | enum ProgViewMode 34 | { 35 | PROG_VIEW_MONO, 36 | PROG_VIEW_STEREO, 37 | PROG_VIEW_COUNT, 38 | }; 39 | 40 | enum ProgHizMode 41 | { 42 | PROG_HIZ_FAR, 43 | PROG_HIZ_FAR_AND_NEAR, 44 | PROG_HIZ_FAR_REST, 45 | PROG_HIZ_COUNT, 46 | }; 47 | 48 | public: 49 | static const uint32_t MAX_MIP_LEVELS = 16; 50 | static const uint32_t SHADER_COUNT = (PROG_HIZ_COUNT * PROG_VIEW_COUNT); 51 | 52 | enum BindingSlots 53 | { 54 | // keep in sync with glsl 55 | BINDING_READ_DEPTH, 56 | BINDING_READ_FAR, 57 | BINDING_WRITE_NEAR, 58 | BINDING_WRITE_FAR, 59 | BINDING_COUNT, 60 | }; 61 | 62 | struct TextureInfo 63 | { 64 | // allocation 65 | uint32_t width; 66 | uint32_t height; 67 | uint32_t mipLevels; 68 | VkFormat format; 69 | VkImageAspectFlags aspect; 70 | 71 | // the system may use only a sub-rectangle of the allocated width/height 72 | // you should clamp access to this, when sampling the texture 73 | uint32_t usedWidth; 74 | uint32_t usedHeight; 75 | 76 | // xy scale and zw clamp 77 | // use min(uv*factor.xy,factor.zw) for lookups 78 | void getShaderFactors(float factors[4]) const; 79 | }; 80 | 81 | struct Update 82 | { 83 | // provide texture/views that are not layered 84 | VkImageView sourceImageView; // 2DMS if createInfo.msaaLevel set, otherwise 2D 85 | VkImageView nearImageView; // 2D optional 86 | VkImageView farImageView; // 2D all mips 87 | VkImageView farImageViews[MAX_MIP_LEVELS]; // 2D single mip 88 | 89 | VkDescriptorImageInfo farImageInfo; 90 | VkDescriptorImageInfo nearImageInfo; 91 | 92 | VkImage sourceImage; 93 | VkImage nearImage; // optional 94 | VkImage farImage; 95 | 96 | TextureInfo sourceInfo; 97 | TextureInfo farInfo; 98 | TextureInfo nearInfo; 99 | bool stereo; // textures are layered, and updates layer 0,1 100 | 101 | Update() 102 | { 103 | memset(this,0,sizeof(Update)); 104 | } 105 | }; 106 | 107 | struct DescriptorUpdate 108 | { 109 | VkWriteDescriptorSet writeSets[BINDING_COUNT]; 110 | VkDescriptorImageInfo imageInfos[BINDING_COUNT + MAX_MIP_LEVELS - 1]; 111 | }; 112 | 113 | struct Config 114 | { 115 | int msaaSamples = 0; 116 | bool reversedZ = false; 117 | bool supportsSubGroupShuffle = false; 118 | bool supportsMinmaxFilter = false; 119 | }; 120 | 121 | 122 | void init(VkDevice device, const Config& config, uint32_t descrSetsCount); 123 | 124 | VkSampler getReadFarSampler() const; 125 | const VkDescriptorPoolSize* getDescriptorPoolSizes(uint32_t& count) const; 126 | VkDescriptorSetLayout getDescriptorSetLayout() const; 127 | std::string getShaderDefines(uint32_t shader) const; 128 | void initPipelines(const VkShaderModule modules[SHADER_COUNT]); 129 | 130 | void deinit(); 131 | 132 | void setupUpdateInfos(Update& update, uint32_t width, uint32_t height, VkFormat sourceFormat, VkImageAspectFlags sourceAspect) const; 133 | void setupDescriptorUpdate(DescriptorUpdate& updateWrite, const Update& update, VkDescriptorSet set) const; 134 | 135 | void cmdUpdateHiz(VkCommandBuffer cmd, const Update& update, VkDescriptorSet set) const; 136 | 137 | // optional utility functions 138 | void initUpdateViews(Update& update) const; 139 | void deinitUpdateViews(Update& update) const; 140 | 141 | // if descrSetsCount was non zero 142 | void updateDescriptorSet(const Update& update, uint32_t setIdx) const; 143 | // if descrSetsCount was non zero 144 | void cmdUpdateHiz(VkCommandBuffer cmd, const Update& update, uint32_t setIdx) const 145 | { 146 | cmdUpdateHiz(cmd, update, m_descrSets[setIdx]); 147 | } 148 | 149 | private: 150 | struct InternalConfig : public Config 151 | { 152 | uint32_t hizLevels = 1; 153 | uint32_t hizNearLevel = 0; 154 | uint32_t hizFarLevel = 0; 155 | }; 156 | 157 | static void getShaderIndexConfig(uint32_t index, ProgHizMode& hiz, ProgViewMode& view) 158 | { 159 | hiz = ProgHizMode(index % PROG_HIZ_COUNT); 160 | view = ProgViewMode(index / PROG_HIZ_COUNT); 161 | } 162 | 163 | static uint32_t getShaderIndex(ProgHizMode hiz, ProgViewMode view) { return view * PROG_HIZ_COUNT + hiz; } 164 | 165 | struct PushConstants 166 | { 167 | // keep in sync with glsl 168 | int srcSize[4]; 169 | int writeLod; 170 | int startLod; 171 | int layer; 172 | int _pad0; 173 | int levelActive[4]; 174 | }; 175 | 176 | void deinitPipelines(); 177 | 178 | InternalConfig m_config; 179 | VkDevice m_device = VK_NULL_HANDLE; 180 | VkSampler m_readDepthSampler; 181 | VkSampler m_readFarSampler; 182 | VkSampler m_readNearSampler; 183 | VkPipeline m_pipelines[SHADER_COUNT] = {0}; 184 | VkPipelineLayout m_pipelineLayout = VK_NULL_HANDLE; 185 | VkDescriptorSetLayout m_descrLayout = VK_NULL_HANDLE; 186 | VkDescriptorPoolSize m_poolSizes[2]; 187 | uint32_t m_descrSetsCount = 0; 188 | VkDescriptorPool m_descrPool = VK_NULL_HANDLE; 189 | VkDescriptorSet* m_descrSets = nullptr; 190 | }; 191 | 192 | #endif 193 | -------------------------------------------------------------------------------- /octant_encoding.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #ifndef _OCTANT_ENCODING_H_ 21 | #define _OCTANT_ENCODING_H_ 22 | 23 | #ifdef __cplusplus 24 | namespace glm { 25 | #define OCT_INLINE inline 26 | #define OCT_FLOOR floor 27 | #define OCT_CLAMP clamp 28 | #define OCT_ABS abs 29 | OCT_INLINE uint32_t pack_oct32(vec2 v) 30 | { 31 | union { 32 | int16_t snorm[2]; 33 | uint32_t packed; 34 | }; 35 | snorm[0] = static_cast(clamp(int32_t(std::round(v.x * float(0x7FFF))), -0x7FFF, 0x7FFF)); 36 | snorm[1] = static_cast(clamp(int32_t(std::round(v.y * float(0x7FFF))), -0x7FFF, 0x7FFF)); 37 | return packed; 38 | } 39 | OCT_INLINE vec2 unpack_oct32(uint32_t v) 40 | { 41 | union { 42 | int16_t snorm[2]; 43 | uint32_t packed; 44 | }; 45 | packed = v; 46 | return vec2(float(snorm[0]) / float(0x7FFF), float(snorm[1]) / float(0x7FFF)); 47 | } 48 | #else 49 | #define OCT_INLINE 50 | #define OCT_FLOOR floor 51 | #define OCT_CLAMP clamp 52 | #define OCT_ABS abs 53 | uint pack_oct32(vec2 v) 54 | { 55 | return packSnorm2x16(v); 56 | } 57 | vec2 unpack_oct32(uint v) 58 | { 59 | return unpackSnorm2x16(v); 60 | } 61 | #endif 62 | 63 | // oct functions from http://jcgt.org/published/0003/02/01/paper.pdf 64 | OCT_INLINE vec2 oct_signNotZero(vec2 v) 65 | { 66 | return vec2((v.x >= 0.0f) ? +1.0f : -1.0f, (v.y >= 0.0f) ? +1.0 : -1.0f); 67 | } 68 | OCT_INLINE vec3 oct_to_vec(vec2 e) 69 | { 70 | vec3 v = vec3(e.x, e.y, 1.0f - OCT_ABS(e.x) - OCT_ABS(e.y)); 71 | if(v.z < 0.0f) 72 | { 73 | vec2 os = oct_signNotZero(e); 74 | v.x = (1.0f - OCT_ABS(e.y)) * os.x; 75 | v.y = (1.0f - OCT_ABS(e.x)) * os.y; 76 | } 77 | return normalize(v); 78 | } 79 | 80 | OCT_INLINE vec3 oct32_to_vec(uint32_t v) 81 | { 82 | return oct_to_vec(unpack_oct32(v)); 83 | } 84 | 85 | OCT_INLINE vec2 vec_to_oct(vec3 v) 86 | { 87 | // Project the sphere onto the octahedron, and then onto the xy plane 88 | vec2 p = vec2(v.x, v.y) * (1.0f / (OCT_ABS(v.x) + OCT_ABS(v.y) + OCT_ABS(v.z))); 89 | // Reflect the folds of the lower hemisphere over the diagonals 90 | return (v.z <= 0.0f) ? (vec2(1.0f-OCT_ABS(p.y), 1.0f-OCT_ABS(p.x)) * oct_signNotZero(p)) : p; 91 | } 92 | 93 | OCT_INLINE vec2 vec_to_oct_precise(vec3 v, int bits) { 94 | vec2 s = vec_to_oct(v); // Remap to the square 95 | // Each snorm's max value interpreted as an integer, 96 | // e.g., 127.0 for snorm8 97 | float M = float(1 << ((bits / 2) - 1)) - 1.0f; 98 | // Remap components to snorm(n/2) precision...with floor instead 99 | // of round (see equation 1) 100 | s = OCT_FLOOR(OCT_CLAMP(s, -1.0f, +1.0f) * M) * (1.0f / M); 101 | vec2 bestRepresentation = s; 102 | float highestCosine = dot(oct_to_vec(s), v); 103 | // Test all combinations of floor and ceil and keep the best. 104 | // Note that at +/- 1, this will exit the square... but that 105 | // will be a worse encoding and never win. 106 | for(int i = 0; i <= 1; ++i) 107 | { 108 | for(int j = 0; j <= 1; ++j) 109 | { 110 | // This branch will be evaluated at compile time 111 | if((i != 0) || (j != 0)) 112 | { 113 | // Offset the bit pattern (which is stored in floating 114 | // point!) to effectively change the rounding mode 115 | // (when i or j is 0: floor, when it is one: ceiling) 116 | vec2 candidate = vec2(i, j) * (1 / M) + s; 117 | float cosine = dot(oct_to_vec(candidate), v); 118 | if(cosine > highestCosine) 119 | { 120 | bestRepresentation = candidate; 121 | highestCosine = cosine; 122 | } 123 | } 124 | } 125 | } 126 | return bestRepresentation; 127 | } 128 | 129 | OCT_INLINE uint vec_to_oct32(vec3 v) 130 | { 131 | return pack_oct32(vec_to_oct_precise(v,32)); 132 | } 133 | 134 | #undef OCT_ABS 135 | #undef OCT_FLOOR 136 | #undef OCT_CLAMP 137 | #undef OCT_INLINE 138 | 139 | #ifdef __cplusplus 140 | } 141 | #endif 142 | 143 | #endif -------------------------------------------------------------------------------- /parallel_work.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | // distributes batches of loops over BATCHSIZE items across 30 | // multiple threads. numItems reflects the total number 31 | // of items to process. 32 | // fn (uint64_t itemIndex) 33 | 34 | template 35 | inline void parallel_batches(uint64_t numItems, std::function fn, uint32_t numThreads) 36 | { 37 | if(numThreads == 0 || numItems < numThreads || numItems < BATCHSIZE) 38 | { 39 | for(uint64_t idx = 0; idx < numItems; idx++) 40 | { 41 | fn(idx); 42 | } 43 | } 44 | else 45 | { 46 | std::atomic_uint64_t counter = 0; 47 | 48 | auto worker = [&]() { 49 | uint64_t idx; 50 | while((idx = counter.fetch_add(BATCHSIZE)) < numItems) 51 | { 52 | uint64_t last = std::min(numItems, idx + BATCHSIZE); 53 | for(uint64_t i = idx; i < last; i++) 54 | { 55 | fn(i); 56 | } 57 | } 58 | }; 59 | 60 | std::vector threads(numThreads); 61 | for(uint32_t i = 0; i < numThreads; i++) 62 | { 63 | threads[i] = std::thread(worker); 64 | } 65 | 66 | for(uint32_t i = 0; i < numThreads; i++) 67 | { 68 | threads[i].join(); 69 | } 70 | } 71 | } 72 | 73 | template 74 | inline void parallel_batches(uint64_t numItems, std::function fn, uint32_t numThreads) 75 | { 76 | if(numThreads <= 1 || numItems < numThreads || numItems < BATCHSIZE) 77 | { 78 | for(uint64_t idx = 0; idx < numItems; idx++) 79 | { 80 | fn(idx, 0); 81 | } 82 | } 83 | else 84 | { 85 | std::atomic_uint64_t counter = 0; 86 | 87 | auto worker = [&](uint32_t threadIdx) { 88 | uint64_t idx; 89 | while((idx = counter.fetch_add(BATCHSIZE)) < numItems) 90 | { 91 | uint64_t last = std::min(numItems, idx + BATCHSIZE); 92 | for(uint64_t i = idx; i < last; i++) 93 | { 94 | fn(i, threadIdx); 95 | } 96 | } 97 | }; 98 | 99 | std::vector threads(numThreads); 100 | for(uint32_t i = 0; i < numThreads; i++) 101 | { 102 | threads[i] = std::thread(worker, i); 103 | } 104 | 105 | for(uint32_t i = 0; i < numThreads; i++) 106 | { 107 | threads[i].join(); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /renderer_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #ifndef RENDERER_H__ 21 | #define RENDERER_H__ 22 | 23 | #include "scene_vk.hpp" 24 | #include "resources_vk.hpp" 25 | #include 26 | 27 | namespace microdisp { 28 | 29 | struct ShaderStats; 30 | 31 | class RenderList 32 | { 33 | public: 34 | struct Config 35 | { 36 | }; 37 | 38 | 39 | void setup(SceneVK* scene, ShaderStats* stats, const Config& config); 40 | 41 | Config m_config; 42 | SceneVK* NV_RESTRICT m_scene = nullptr; 43 | ShaderStats* m_stats = nullptr; 44 | }; 45 | 46 | class RendererVK 47 | { 48 | public: 49 | enum DecoderType 50 | { 51 | DECODER_BASETRI_MIP, 52 | DECODER_MICROTRI, 53 | DECODER_MICROTRI_INTRINSIC, 54 | }; 55 | 56 | struct Config 57 | { 58 | bool useNormalMap = false; 59 | bool useMicroVertexNormals = false; 60 | bool useLod = false; 61 | bool useOcclusionHiz = false; 62 | DecoderType decoderType = DECODER_BASETRI_MIP; 63 | uint32_t numThreads = 0; 64 | uint32_t maxVisibleBits = 20; 65 | }; 66 | 67 | class Type 68 | { 69 | public: 70 | Type() { getRegistry().push_back(this); } 71 | 72 | public: 73 | virtual bool isAvailable(const nvvk::Context* context) const = 0; 74 | virtual const char* name() const = 0; 75 | virtual RendererVK* create(ResourcesVK& resources) const = 0; 76 | virtual bool supportsCompressed() const = 0; 77 | virtual uint32_t priority() const { return 0xFF; } 78 | }; 79 | 80 | typedef std::vector Registry; 81 | 82 | static Registry& getRegistry() 83 | { 84 | static Registry s_registry; 85 | return s_registry; 86 | } 87 | 88 | RendererVK(ResourcesVK& resources) 89 | : m_res(resources) 90 | { 91 | } 92 | 93 | ResourcesVK& m_res; 94 | 95 | virtual bool init(RenderList& list, const Config& config) = 0; 96 | virtual void deinit() = 0; 97 | virtual void draw(const FrameConfig& config, nvvk::ProfilerVK& profiler) = 0; 98 | 99 | virtual ~RendererVK() {} 100 | 101 | ////////////////////////////////////////////////////////////////////////// 102 | 103 | enum CmdType 104 | { 105 | CMD_TYPE_LO, 106 | CMD_TYPE_DISPLACED, 107 | CMD_TYPE_NONDISPLACED_LO, 108 | CMD_TYPE_SHELL, 109 | CMD_TYPE_EMPTY, 110 | NUM_CMD_TYPES, 111 | }; 112 | 113 | struct StandardShaderIDs 114 | { 115 | nvvk::ShaderModuleID vertex, fragment, fragment_overlay; 116 | nvvk::ShaderModuleID shellVertex, shellFragment; 117 | }; 118 | 119 | struct Setup 120 | { 121 | VkPipeline pipeline = {nullptr}; 122 | VkPipeline pipelineOverlay = {nullptr}; 123 | VkPipeline pipelineFlat = {nullptr}; 124 | VkPipeline pipelineIndirect = {nullptr}; 125 | nvvk::TDescriptorSetContainer<2> container; 126 | VkShaderStageFlags stageFlags; 127 | }; 128 | 129 | Setup m_standard; 130 | StandardShaderIDs m_standardShaders; 131 | VkCommandPool m_cmdPool = VK_NULL_HANDLE; 132 | 133 | CmdType getCmdType(ModelType modelType) const 134 | { 135 | switch(modelType) 136 | { 137 | case MODEL_LO: 138 | return CMD_TYPE_LO; 139 | case MODEL_DISPLACED: 140 | return CMD_TYPE_DISPLACED; 141 | case MODEL_SHELL: 142 | return CMD_TYPE_SHELL; 143 | default: 144 | return CMD_TYPE_EMPTY; 145 | } 146 | } 147 | 148 | VkPipeline getPipeline(const Setup& setup, bool overlay = false, bool shell = false) const 149 | { 150 | if(shell && setup.pipelineFlat) 151 | { 152 | return setup.pipelineFlat; 153 | } 154 | else if(overlay && setup.pipelineOverlay) 155 | { 156 | return setup.pipelineOverlay; 157 | } 158 | else 159 | { 160 | return setup.pipeline; 161 | } 162 | } 163 | 164 | void initStandardShaders(std::string prepend); 165 | void deinitStandardShaders(); 166 | void initSetupStandard(const SceneVK* scene, const Config& config); 167 | void initTextureSet(Setup& setup, const SceneVK* scene, const Config& config, VkShaderStageFlags shaderStageFlags); 168 | void initCommandPool(); 169 | void destroyPipelines(Setup& setup); 170 | 171 | VkCommandBuffer generateCmdBufferEmpty(); 172 | VkCommandBuffer generateCmdBufferLo(RenderList& list, const Config& config, bool overlay, bool onlyNonDisplaced, bool shell = false); 173 | }; 174 | } // namespace microdisp 175 | 176 | #endif 177 | -------------------------------------------------------------------------------- /resources_base_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | #pragma once 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace microdisp { 34 | 35 | class ResourcesVK; 36 | 37 | typedef nvvk::Texture RTextureR; 38 | 39 | struct RBuffer : nvvk::Buffer 40 | { 41 | VkDescriptorBufferInfo info = {VK_NULL_HANDLE}; 42 | VkDeviceAddress addr = 0; 43 | }; 44 | 45 | struct RTextureRW : nvvk::Texture 46 | { 47 | VkDescriptorImageInfo descriptorImage; 48 | VkExtent3D extent; 49 | }; 50 | 51 | inline void cmdCopyBuffer(VkCommandBuffer cmd, const RBuffer& src, const RBuffer& dst) 52 | { 53 | VkBufferCopy cpy = {src.info.offset, dst.info.offset, src.info.range}; 54 | vkCmdCopyBuffer(cmd, src.buffer, dst.buffer, 1, &cpy); 55 | } 56 | 57 | } // namespace microdisp 58 | -------------------------------------------------------------------------------- /scene.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | 31 | #include "config.h" 32 | #include "meshset_gltf.hpp" 33 | #include "meshset_utils.hpp" 34 | #include "parallel_work.hpp" 35 | #include "scene.hpp" 36 | 37 | namespace { 38 | // Loads a mesh set from a file. When loading a glTF file, can load 39 | // microdisplacement as well. If the filename is empty or loading fails, 40 | // returns a struct will null elements. 41 | bool loadMeshAndBarySet(MeshSet& meshSet, BaryAttributesSet* barySet, const char* filename) 42 | { 43 | if(!filename) 44 | return false; 45 | 46 | size_t len = strlen(filename); 47 | std::string ext = len > 3 ? filename + len - 4 : nullptr; 48 | std::transform(ext.begin(), ext.end(), ext.begin(), [](unsigned char c) { return std::tolower(c); }); 49 | // ext now contains the last four characters of the filename, in lower case. 50 | 51 | if(ext == "gltf" || ext == ".glb") 52 | { 53 | return loadGLTF(meshSet, barySet, filename); 54 | } 55 | else 56 | { 57 | return false; 58 | } 59 | } 60 | } // namespace 61 | 62 | namespace microdisp { 63 | bool Scene::load(const char* filenameLo) 64 | { 65 | { 66 | std::unique_ptr loadedLo = std::make_unique(); 67 | 68 | if(loadMeshAndBarySet(*loadedLo, &barySet, filenameLo)) 69 | { 70 | meshSetLo = std::move(loadedLo); 71 | } 72 | else 73 | { 74 | return false; 75 | } 76 | } 77 | 78 | // Set up lo-res mesh 79 | meshSetLoOrigInstanceCount = meshSetLo->meshInstances.size(); 80 | 81 | if(!meshSetLo->attributes.directionBounds.empty() && !meshSetLo->directionBoundsAreUniform) 82 | { 83 | hasDirectionBounds = true; 84 | } 85 | 86 | { 87 | if(!barySet.displacements.empty()) 88 | { 89 | if(!finalizeLoadedBarySet()) 90 | { 91 | LOGE("%s: Loading worked and at least one mesh had microdisplacement, but finalizeLoadedBaryMesh failed!", __FUNCTION__); 92 | return false; 93 | } 94 | } 95 | } 96 | 97 | finalizeMeshLo(); 98 | 99 | if(!barySet.displacements.empty()) 100 | { 101 | barySetShellVolume = barySet.computeShellVolume(*meshSetLo, true, true, g_numThreads); 102 | nvprintfLevel(LOGLEVEL_INFO, "total shell volume %f\n", barySetShellVolume); 103 | } 104 | 105 | 106 | return true; 107 | } 108 | 109 | 110 | 111 | void Scene::finalizeMeshLo() 112 | { 113 | meshSetLo->setupProcessingGlobals(g_numThreads); 114 | 115 | // we build those on-demand later 116 | meshSetLo->clearDirectionBoundsGlobals(); 117 | 118 | // for baking we want some reference matrix for each mesh 119 | // here we simply pick a meshInstance with a big bbox 120 | meshSetLo->setupLargestInstance(); 121 | // for per-instance lod 122 | meshSetLo->setupEdgeLengths(g_numThreads); 123 | } 124 | 125 | bool Scene::finalizeLoadedBarySet() 126 | { 127 | for(size_t d = 0; d < barySet.displacements.size(); d++) 128 | { 129 | BaryDisplacementAttribute& displacementAttr = barySet.displacements[d]; 130 | 131 | if(displacementAttr.uncompressed) 132 | { 133 | hasUncompressedDisplacement = true; 134 | } 135 | else if(displacementAttr.compressed) 136 | { 137 | hasCompressedDisplacement = true; 138 | } 139 | } 140 | 141 | barySet.fillUniformDirectionBounds(*meshSetLo); 142 | barySet.updateStats(); 143 | 144 | return true; 145 | } 146 | } // namespace microdisp 147 | -------------------------------------------------------------------------------- /scene.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "config.h" 28 | #include "meshset.hpp" 29 | #include "baryset.hpp" 30 | 31 | namespace microdisp { 32 | 33 | class Scene 34 | { 35 | public: 36 | bool hasCompressedDisplacement = false; 37 | bool hasUncompressedDisplacement = false; 38 | bool hasDirectionBounds = false; 39 | 40 | // lo-resolution meshset that gets the displacement applied 41 | std::unique_ptr meshSetLo; 42 | size_t meshSetLoOrigInstanceCount = 0; 43 | 44 | // stores barycentric attributes and displacements for meshLo 45 | // can be loaded from disk or filled via baryBaker 46 | BaryAttributesSet barySet; 47 | double barySetShellVolume = 0; 48 | 49 | // Loads a scene from filename 50 | bool load(const char* filenameLo); 51 | 52 | private: 53 | void finalizeMeshLo(); 54 | // After loading displacement, computes necessary fields of a BaryMesh object, 55 | // based on the lo-res meshes. 56 | // Can set hasCustomBary, hasCustomUncompressed, and hasCustomCompressed. 57 | bool finalizeLoadedBarySet(); 58 | }; 59 | } // namespace microdisp 60 | -------------------------------------------------------------------------------- /scene_vk.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "config.h" 33 | #include "meshset_gltf.hpp" 34 | #include "meshset_utils.hpp" 35 | #include "parallel_work.hpp" 36 | #include "resources_vk.hpp" 37 | #include "scene_vk.hpp" 38 | #include "vk_nv_micromesh.h" 39 | 40 | 41 | namespace microdisp { 42 | 43 | void SceneVK::init(ResourcesVK& res) {} 44 | 45 | void SceneVK::deinit(ResourcesVK& res) 46 | { 47 | meshSetLoVK.deinit(res); 48 | } 49 | 50 | 51 | void SceneVK::updateGrid(ResourcesVK& res, uint32_t copies, uint32_t axis, float spacing, bool gpu) 52 | { 53 | copies = std::max(copies, 1u); 54 | 55 | // Handle the case where only meshLo exists (e.g. when viewing a file with displacement): 56 | glm::vec3 diagonal = meshSetLo->bbox.diagonal() * spacing; 57 | if(spacing < 0) 58 | { 59 | diagonal = glm::vec3(-spacing); 60 | } 61 | 62 | meshSetLo->setupInstanceGrid(meshSetLoOrigInstanceCount, copies, axis, diagonal); 63 | 64 | if(gpu) 65 | { 66 | meshSetLoVK.updateInstances(res, *meshSetLo); 67 | } 68 | } 69 | 70 | void SceneVK::updateLow(ResourcesVK& res) 71 | { 72 | meshSetLoVK.deinit(res); 73 | meshSetLoVK.init(res, *meshSetLo); 74 | meshSetLoVK.initDisplacementDirections(res, *meshSetLo); 75 | meshSetLoVK.initDisplacementEdgeFlags(res, *meshSetLo); 76 | meshSetLoVK.initDisplacementBounds(res, *meshSetLo); 77 | meshSetLoVK.initNormalMaps(res, *meshSetLo); 78 | } 79 | 80 | } // namespace microdisp 81 | -------------------------------------------------------------------------------- /scene_vk.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #pragma once 21 | 22 | #include "scene.hpp" 23 | #include "meshset_vk.hpp" 24 | 25 | namespace microdisp { 26 | 27 | class SceneVK : public Scene 28 | { 29 | public: 30 | // vk resources for lo-res meshset 31 | MeshSetVK meshSetLoVK; 32 | 33 | void init(ResourcesVK& res); 34 | void deinit(ResourcesVK& res); 35 | 36 | // Uploads the lo-res mesh (`meshLo`) to `meshLoVK`. 37 | void updateLow(ResourcesVK& res); 38 | // Updates the grid of instances of the lo-res and hi-res mesh. 39 | void updateGrid(ResourcesVK& res, uint32_t copies, uint32_t axis, float spacing, bool gpu = true); 40 | 41 | private: 42 | void finalizeMeshLo(); 43 | // After loading displacement, computes necessary fields of a BaryMesh object, 44 | // based on the lo-res meshes. 45 | // Can set hasCustomBary, hasCustomUncompressed, and hasCustomCompressed. 46 | bool finalizeLoadedBarySet(); 47 | }; 48 | } // namespace microdisp 49 | -------------------------------------------------------------------------------- /stbimage.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | #define STB_IMAGE_IMPLEMENTATION 21 | #include 22 | 23 | #define STB_IMAGE_IMPLEMENTATION 24 | #define STB_IMAGE_WRITE_IMPLEMENTATION 25 | #include 26 | -------------------------------------------------------------------------------- /vk_nv_micromesh_prototypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION 17 | * SPDX-License-Identifier: Apache-2.0 18 | */ 19 | 20 | ////////////////////////////////////////////////////////////////////////////////////// 21 | ////////////////////////////////////////////////////////////////////////////////////// 22 | // 23 | // WARNING: VK_NV_displacement_micromap is still in beta 24 | // 25 | ////////////////////////////////////////////////////////////////////////////////////// 26 | ////////////////////////////////////////////////////////////////////////////////////// 27 | 28 | #pragma once 29 | 30 | #include "vk_nv_micromesh.h" 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | #ifdef VULKAN_NV_DEFINED_EXT_opacity_micromap 37 | #ifndef VK_NO_PROTOTYPES 38 | void load_VK_EXT_opacity_micromap_prototypes(VkDevice device, PFN_vkGetDeviceProcAddr getDeviceProcAddr); 39 | #else 40 | typedef struct VK_EXT_opacity_micromap_functions 41 | { 42 | PFN_vkCreateMicromapEXT pfn_vkCreateMicromapEXT; 43 | PFN_vkDestroyMicromapEXT pfn_vkDestroyMicromapEXT; 44 | PFN_vkCmdBuildMicromapsEXT pfn_vkCmdBuildMicromapsEXT; 45 | PFN_vkBuildMicromapsEXT pfn_vkBuildMicromapsEXT; 46 | PFN_vkCopyMicromapEXT pfn_vkCopyMicromapEXT; 47 | PFN_vkCopyMicromapToMemoryEXT pfn_vkCopyMicromapToMemoryEXT; 48 | PFN_vkCopyMemoryToMicromapEXT pfn_vkCopyMemoryToMicromapEXT; 49 | PFN_vkWriteMicromapsPropertiesEXT pfn_vkWriteMicromapsPropertiesEXT; 50 | PFN_vkCmdCopyMicromapEXT pfn_vkCmdCopyMicromapEXT; 51 | PFN_vkCmdCopyMicromapToMemoryEXT pfn_vkCmdCopyMicromapToMemoryEXT; 52 | PFN_vkCmdCopyMemoryToMicromapEXT pfn_vkCmdCopyMemoryToMicromapEXT; 53 | PFN_vkCmdWriteMicromapsPropertiesEXT pfn_vkCmdWriteMicromapsPropertiesEXT; 54 | PFN_vkGetDeviceMicromapCompatibilityEXT pfn_vkGetDeviceMicromapCompatibilityEXT; 55 | PFN_vkGetMicromapBuildSizesEXT pfn_vkGetMicromapBuildSizesEXT; 56 | } VK_EXT_opacity_micromap_functions; 57 | 58 | void load_VK_EXT_opacity_micromap_functions(VK_EXT_opacity_micromap_functions* fns, VkDevice device, PFN_vkGetDeviceProcAddr getDeviceProcAddr); 59 | #endif 60 | #else // ^^^ #ifdef VULKAN_NV_DEFINED_EXT_opacity_micromap 61 | // When the Vulkan SDK provides VK_EXT_opacity_micromap, extensions_vk.cpp loads it for us. 62 | inline void load_VK_EXT_opacity_micromap_prototypes(VkDevice device, PFN_vkGetDeviceProcAddr getDeviceProcAddr){}; 63 | #endif // #ifdef VULKAN_NV_DEFINED_EXT_opacity_micromap 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | // there are no extra function prototypes for displacement --------------------------------------------------------------------------------