diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..6f4b3ce6 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,73 @@ +{ + "files.associations": { + "vector": "cpp", + "array": "cpp", + "fstream": "cpp", + "functional": "cpp", + "iomanip": "cpp", + "list": "cpp", + "string": "cpp", + "string_view": "cpp", + "type_traits": "cpp", + "xstring": "cpp", + "xtree": "cpp", + "xutility": "cpp", + "algorithm": "cpp", + "atomic": "cpp", + "chrono": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "initializer_list": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "iterator": "cpp", + "limits": "cpp", + "locale": "cpp", + "map": "cpp", + "memory": "cpp", + "mutex": "cpp", + "new": "cpp", + "ostream": "cpp", + "ratio": "cpp", + "set": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "system_error": "cpp", + "xthread": "cpp", + "thread": "cpp", + "tuple": "cpp", + "typeinfo": "cpp", + "unordered_map": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xhash": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocbuf": "cpp", + "xlocinfo": "cpp", + "xlocmes": "cpp", + "xlocmon": "cpp", + "xlocnum": "cpp", + "xloctime": "cpp", + "xmemory": "cpp", + "xmemory0": "cpp", + "xstddef": "cpp", + "xtr1common": "cpp", + "forward_list": "cpp", + "cctype": "cpp", + "clocale": "cpp", + "numeric": "cpp", + "valarray": "cpp" + } +} \ No newline at end of file diff --git a/README.md b/README.md index 110697ce..90875575 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,164 @@ CUDA Path Tracer **University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 3** -* (TODO) YOUR NAME HERE -* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab) +* Yichao Wang + * [LinkedIn](https://www.linkedin.com/in/wangyic/) +* Tested on: Windows 10 Home 64-bit (10.0, Build 18363) + * Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz (8 CPUs) + * GeForce GTX 1060 6.1 -### (TODO: Your README) +## Demo -*DO NOT* leave the README to the last minute! It is a crucial part of the -project, and we will not be able to grade you without a good README. +### Excalibur +![](img/demo1.png) +Rendered using [fate.txt](scenes/fate.txt) + +Excalibur model credit to [Terallis](https://sketchfab.com/3d-models/excalibur-fatestay-night-c7cb6fb3ada742e894f891ad18be1db4) + +Rock model credit to [BenDelamore](https://sketchfab.com/3d-models/a-rock-c49139dbab5e4c498c225b56cca30466) + +### Torii + +![](img/demo2.png) +Rendered using [torii.txt](scenes/torii.txt) + +Torii model credit to [kazukisakamoto +](https://sketchfab.com/3d-models/torii-7c4d9c93946a41a2a73468b74436705b) + +## Genshin Impact + +![](img/demo3.png) + +Rendered using [genshin2.txt](scenes/genshin2.txt) + +Environment stone credit to [SamanthaMB](https://sketchfab.com/3d-models/textured-game-environment-3a3c90194d35484f9b18b192c95341bf) + +Wolf's Gravestone model credit to [Le Ryan](https://sketchfab.com/3d-models/genshin-impact-wolfs-gravestone-e71a938e02904b14abace9ce10078711) + +![](img/demo4.png) + +Genshin Impact model credit to [José Castanheira](https://sketchfab.com/3d-models/genshin-impact-scene-fa345e2979544abe94fa05acf1d62f4b) +## Description + +A CUDA-based path tracer. + +Feature Implemented: + +* Visual + * Shading kernel with BSDF evaluation (diffuse, reflect & refract) + * Stochastic Sampled Antialiasing + * Stratified Cosine hemisphere sampling + * Texture mapping and Bump mapping +* Mesh + * GLTF mesh loading using tinygltf +* Performance + * Path termination using streaming compaction + * Material sorting + * First bounce caching + + +## Feature Analysis + + +## Visual + +### Shading Kernel with BSDF Evaluation (diffuse, reflect & refract) + +|Diffuse | Reflect | +|-|-| +|![](img/diffuse.png) |![](img/reflect.png) | + +#### Refract +![](img/refract.png) + +|Reflect + Refract | Diffuse + Reflect + Refract | +|-|-| +|![](img/rr.png) |![](img/rrd.png) | + +I implmented BSDF evaluation for different materials (diffuse, reflect & refract). For diffuse material, the ray is scattered using [cosine hemisphere sampling](#stratified-cosine-hemisphere-sampling) +. For reflect material, the ray is scattered using ```glm::reflect```. For refract material, the ray is scattered using Schlick's approximation. For multiple effect on one surface, I choose them based on probability defined in scene file. Before scattering rays, I [sorted material](#material-sorting) for better CUDA performance. + +### Stochastic Sampled Antialiasing + +|Without Antialiasing| With Antialiasing| +|--|--| +|![](img/uaa.png) |![](img/aa.png) | + +The path tracer runs multiple iterations (the images in [demo](#demo) uses 5000 iterations) to converge the final image. I implemented Stochastic sampled antialiasing by jittering the rays when generating them at first bounce of each iteration. Thus, the final pixels will average its surrounding color. From above comparision, we can see antialiasing has a better result. However, the antialiasing sacrifice the performance of [first bounce caching](#first-bounce-caching) because they should be different at each interation. + +### Stratified Cosine Hemisphere Sampling + +|Random|Stratified| +|----|----| +|![](img/strat2.png)|![](img/strat1.png) + +I implemented stratified cosine hemisphere sampling to make render result better than purely random sampling. A stratified sampling is a combination of uniform and random that is samples a random point in each grid. The stratified sampling is implmented using [Peter Shirley's method](https://pdfslide.net/documents/a-low-distortion-map-between-disk-and-square.html). +The above results shows uses two cosine hemisphere sampling to generate next path's direction. We can see that random sampling has some unexpected white pixel while stratified sampling is fine. + +### Texture mapping and Bump mapping + +|Texture (296 ms/iter)|Texture + Bump (303 ms/iter) | Procedural Texture (300 ms/iter)| +|-|---|---| +| ![](img/texture.png)|![](img/texturebump.png) |![](img/pc.png) + +The path tracer support for texture and bump mapping based on [GLTF mesh](#mesh). From above result, we can see there are more details for the mesh with bump mapping. I also implemented a simple procedural texture for performance comparision but it seems there is not much difference. + +## Mesh + +### GLTF Mesh Loading and Rendering with Bounding Volume Intersection Culling + +The path tracer support simple GLTF mesh loading by using [tinygltf](https://github.com/syoyo/tinygltf/). I parsed each mesh into triangles and perform ```glm::intersectRayTriangle``` on each triangles. I also performed a simple bounding volume intersection culling so that triangles will be checked only if a ray intersect the mesh's bounding box. For furture improvement, we should use hierarchical spatial data structures such as Octree for the culling. + +GLTF format is very comprehensive and complicate. The current path tracer only support simple mesh loading with basic color texture and normal map texture. There are a lot more features to explore! + +## Performance + +The following performance analysis uses [cornell.txt](scenes/cornell.txt). + +### cornell.txt render result: + +![](img/cornell.png) + +### Path termination using streaming compaction + +I implemented streaming compaction by using ```thrust::partition``` to separate the paths based on their living status so that the path tracer will continue with only the alive path. Thus, it will save us recourses by running fewer CUDA threads as the depth increase. In addition, we can also end the path tracer earlier if no thread is alive. + +Plot1: +![](img/stream1.png) + +From Plot1, for open scene such as cornell.txt, we can see that the number of remaining thread become smaller as the ray bounces and finally become 0 once it reaches maximum depth. This is because the ray will terminate if it hit a light or it hit nothing. However, in a closed scene, the ray always has an intersection and it will only terminate when it hit light. Thus, the streaming paction is not useful for a closed scene because it only affects terminated rays. For reference, the average time cost for open scene is about 47 ms while it's about 96 ms for closed scene. + +Plot2: +![](img/stream2.png) + +From Plot2, we can see that the performance increases as max depth increases when using streaming compaction. The time cost linearly increases when turning off streaming compaction. When max depth is 8, the cost for streaming compaction is more. This is because partition path also takes time. + +### Material sorting + +After computing the intersections, we want to sort them based on their materials before shading because we are using one big shading kernel. We can access the contigous memory with sorted intersections when perform shading. This improves performance because we decrease divergence in each warp. For reference, at max depth 8, the average time cost for material sorting is about 47 ms while it is about 60 ms without the sorting. + +### First bounce caching + +The path tracer runs multiple iterations to converge the final result. Without antialiasing, the first bounces at each iteration is the same. So I cached first bounce intersections at first iteration so that we do not need to generate them again at the first bounce for the other iteration. For reference, at max depth 8, the average time cost for first bounce caching is about 45 ms while it is about 47 ms without the caching. + + +## Blooper + + +![](img/blooper1.png) + +failed path trace +![](img/blooper2.png) + +![](img/blooper3.png) + +failed refract material +![](img/blooper4.png) + +![](img/blooper5.png) + +![](img/blooper6.png) + +failed bsdf sampling +![](img/blooper7.png) diff --git a/external/include/json.hpp b/external/include/json.hpp new file mode 100644 index 00000000..665d0f04 --- /dev/null +++ b/external/include/json.hpp @@ -0,0 +1,20406 @@ +/* + __ _____ _____ _____ + __| | __| | | | JSON for Modern C++ +| | |__ | | | | | | version 3.5.0 +|_____|_____|_____|_|___| https://github.com/nlohmann/json + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2013-2018 Niels Lohmann . + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#ifndef NLOHMANN_JSON_HPP +#define NLOHMANN_JSON_HPP + +#define NLOHMANN_JSON_VERSION_MAJOR 3 +#define NLOHMANN_JSON_VERSION_MINOR 5 +#define NLOHMANN_JSON_VERSION_PATCH 0 + +#include // all_of, find, for_each +#include // assert +#include // and, not, or +#include // nullptr_t, ptrdiff_t, size_t +#include // hash, less +#include // initializer_list +#include // istream, ostream +#include // random_access_iterator_tag +#include // accumulate +#include // string, stoi, to_string +#include // declval, forward, move, pair, swap + +// #include +#ifndef NLOHMANN_JSON_FWD_HPP +#define NLOHMANN_JSON_FWD_HPP + +#include // int64_t, uint64_t +#include // map +#include // allocator +#include // string +#include // vector + +/*! +@brief namespace for Niels Lohmann +@see https://github.com/nlohmann +@since version 1.0.0 +*/ +namespace nlohmann +{ +/*! +@brief default JSONSerializer template argument + +This serializer ignores the template arguments and uses ADL +([argument-dependent lookup](https://en.cppreference.com/w/cpp/language/adl)) +for serialization. +*/ +template +struct adl_serializer; + +template class ObjectType = + std::map, + template class ArrayType = std::vector, + class StringType = std::string, class BooleanType = bool, + class NumberIntegerType = std::int64_t, + class NumberUnsignedType = std::uint64_t, + class NumberFloatType = double, + template class AllocatorType = std::allocator, + template class JSONSerializer = + adl_serializer> +class basic_json; + +/*! +@brief JSON Pointer + +A JSON pointer defines a string syntax for identifying a specific value +within a JSON document. It can be used with functions `at` and +`operator[]`. Furthermore, JSON pointers are the base for JSON patches. + +@sa [RFC 6901](https://tools.ietf.org/html/rfc6901) + +@since version 2.0.0 +*/ +template +class json_pointer; + +/*! +@brief default JSON class + +This type is the default specialization of the @ref basic_json class which +uses the standard template types. + +@since version 1.0.0 +*/ +using json = basic_json<>; +} // namespace nlohmann + +#endif + +// #include + + +// This file contains all internal macro definitions +// You MUST include macro_unscope.hpp at the end of json.hpp to undef all of them + +// exclude unsupported compilers +#if !defined(JSON_SKIP_UNSUPPORTED_COMPILER_CHECK) + #if defined(__clang__) + #if (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__) < 30400 + #error "unsupported Clang version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #elif defined(__GNUC__) && !(defined(__ICC) || defined(__INTEL_COMPILER)) + #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) < 40800 + #error "unsupported GCC version - see https://github.com/nlohmann/json#supported-compilers" + #endif + #endif +#endif + +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + +// disable documentation warnings on clang +#if defined(__clang__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wdocumentation" +#endif + +// allow for portable deprecation warnings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #define JSON_DEPRECATED __attribute__((deprecated)) +#elif defined(_MSC_VER) + #define JSON_DEPRECATED __declspec(deprecated) +#else + #define JSON_DEPRECATED +#endif + +// allow to disable exceptions +#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION) + #define JSON_THROW(exception) throw exception + #define JSON_TRY try + #define JSON_CATCH(exception) catch(exception) + #define JSON_INTERNAL_CATCH(exception) catch(exception) +#else + #define JSON_THROW(exception) std::abort() + #define JSON_TRY if(true) + #define JSON_CATCH(exception) if(false) + #define JSON_INTERNAL_CATCH(exception) if(false) +#endif + +// override exception macros +#if defined(JSON_THROW_USER) + #undef JSON_THROW + #define JSON_THROW JSON_THROW_USER +#endif +#if defined(JSON_TRY_USER) + #undef JSON_TRY + #define JSON_TRY JSON_TRY_USER +#endif +#if defined(JSON_CATCH_USER) + #undef JSON_CATCH + #define JSON_CATCH JSON_CATCH_USER + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_CATCH_USER +#endif +#if defined(JSON_INTERNAL_CATCH_USER) + #undef JSON_INTERNAL_CATCH + #define JSON_INTERNAL_CATCH JSON_INTERNAL_CATCH_USER +#endif + +// manual branch prediction +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #define JSON_LIKELY(x) __builtin_expect(!!(x), 1) + #define JSON_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else + #define JSON_LIKELY(x) x + #define JSON_UNLIKELY(x) x +#endif + +// C++ language standard detection +#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 +#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 +#endif + +/*! +@brief macro to briefly define a mapping between an enum and JSON +@def NLOHMANN_JSON_SERIALIZE_ENUM +@since version 3.4.0 +*/ +#define NLOHMANN_JSON_SERIALIZE_ENUM(ENUM_TYPE, ...) \ + template \ + inline void to_json(BasicJsonType& j, const ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [e](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.first == e; \ + }); \ + j = ((it != std::end(m)) ? it : std::begin(m))->second; \ + } \ + template \ + inline void from_json(const BasicJsonType& j, ENUM_TYPE& e) \ + { \ + static_assert(std::is_enum::value, #ENUM_TYPE " must be an enum!"); \ + static const std::pair m[] = __VA_ARGS__; \ + auto it = std::find_if(std::begin(m), std::end(m), \ + [j](const std::pair& ej_pair) -> bool \ + { \ + return ej_pair.second == j; \ + }); \ + e = ((it != std::end(m)) ? it : std::begin(m))->first; \ + } + +// Ugly macros to avoid uglier copy-paste when specializing basic_json. They +// may be removed in the future once the class is split. + +#define NLOHMANN_BASIC_JSON_TPL_DECLARATION \ + template class ObjectType, \ + template class ArrayType, \ + class StringType, class BooleanType, class NumberIntegerType, \ + class NumberUnsignedType, class NumberFloatType, \ + template class AllocatorType, \ + template class JSONSerializer> + +#define NLOHMANN_BASIC_JSON_TPL \ + basic_json + +// #include + + +#include // not +#include // size_t +#include // conditional, enable_if, false_type, integral_constant, is_constructible, is_integral, is_same, remove_cv, remove_reference, true_type + +namespace nlohmann +{ +namespace detail +{ +// alias templates to reduce boilerplate +template +using enable_if_t = typename std::enable_if::type; + +template +using uncvref_t = typename std::remove_cv::type>::type; + +// implementation of C++14 index_sequence and affiliates +// source: https://stackoverflow.com/a/32223343 +template +struct index_sequence +{ + using type = index_sequence; + using value_type = std::size_t; + static constexpr std::size_t size() noexcept + { + return sizeof...(Ints); + } +}; + +template +struct merge_and_renumber; + +template +struct merge_and_renumber, index_sequence> + : index_sequence < I1..., (sizeof...(I1) + I2)... > {}; + +template +struct make_index_sequence + : merge_and_renumber < typename make_index_sequence < N / 2 >::type, + typename make_index_sequence < N - N / 2 >::type > {}; + +template<> struct make_index_sequence<0> : index_sequence<> {}; +template<> struct make_index_sequence<1> : index_sequence<0> {}; + +template +using index_sequence_for = make_index_sequence; + +// dispatch utility (taken from ranges-v3) +template struct priority_tag : priority_tag < N - 1 > {}; +template<> struct priority_tag<0> {}; + +// taken from ranges-v3 +template +struct static_const +{ + static constexpr T value{}; +}; + +template +constexpr T static_const::value; +} // namespace detail +} // namespace nlohmann + +// #include + + +#include // not +#include // numeric_limits +#include // false_type, is_constructible, is_integral, is_same, true_type +#include // declval + +// #include + +// #include + + +#include // random_access_iterator_tag + +// #include + + +namespace nlohmann +{ +namespace detail +{ +template struct make_void +{ + using type = void; +}; +template using void_t = typename make_void::type; +} // namespace detail +} // namespace nlohmann + +// #include + + +namespace nlohmann +{ +namespace detail +{ +template +struct iterator_types {}; + +template +struct iterator_types < + It, + void_t> +{ + using difference_type = typename It::difference_type; + using value_type = typename It::value_type; + using pointer = typename It::pointer; + using reference = typename It::reference; + using iterator_category = typename It::iterator_category; +}; + +// This is required as some compilers implement std::iterator_traits in a way that +// doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341. +template +struct iterator_traits +{ +}; + +template +struct iterator_traits < T, enable_if_t < !std::is_pointer::value >> + : iterator_types +{ +}; + +template +struct iterator_traits::value>> +{ + using iterator_category = std::random_access_iterator_tag; + using value_type = T; + using difference_type = ptrdiff_t; + using pointer = T*; + using reference = T&; +}; +} +} + +// #include + +// #include + + +#include + +// #include + + +// http://en.cppreference.com/w/cpp/experimental/is_detected +namespace nlohmann +{ +namespace detail +{ +struct nonesuch +{ + nonesuch() = delete; + ~nonesuch() = delete; + nonesuch(nonesuch const&) = delete; + void operator=(nonesuch const&) = delete; +}; + +template class Op, + class... Args> +struct detector +{ + using value_t = std::false_type; + using type = Default; +}; + +template class Op, class... Args> +struct detector>, Op, Args...> +{ + using value_t = std::true_type; + using type = Op; +}; + +template