Skip to content

Commit

Permalink
Merge pull request #41 from cuteday/spectral
Browse files Browse the repository at this point in the history
Implement spectral rendering
  • Loading branch information
cuteday authored Nov 1, 2023
2 parents 6bed997 + 6689098 commit 6d3ff20
Show file tree
Hide file tree
Showing 78 changed files with 53,356 additions and 982 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ OPTION (KRR_BUILD_EXAMPLES
OFF
)

OPTION (KRR_RENDER_SPECTRAL
"Build the spectral variant of the renderer (WIP)."
ON
)

###############################################################################
# Build type and C++ compiler setup
###############################################################################
Expand All @@ -42,6 +47,7 @@ if (NOT IS_DIRECTORY "${KRR_RENDER_ROOT}/src/ext/glfw")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_CRT_SECURE_NO_WARNINGS ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_ENABLE_EXTENDED_ALIGNED_STORAGE ")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4819 /wd4068 /wd4828") # chn encoding warning

Expand Down Expand Up @@ -119,6 +125,7 @@ target_compile_options (
"$<$<CXX_COMPILER_ID:MSVC>:$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler >/wd4843>" # double -> float conversion
"$<$<CXX_COMPILER_ID:MSVC>:$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler >/wd26451>" # arithmetic on 4-byte value, then cast to 8-byte
"$<$<CXX_COMPILER_ID:MSVC>:$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler >/wd26495>" # uninitialized member variable
"$<$<CXX_COMPILER_ID:MSVC>:$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcompiler >/D_ENABLE_EXTENDED_ALIGNED_STORAGE>" # conform alignment (c++17)

"$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcudafe --diag_suppress=partial_override>"
"$<$<COMPILE_LANGUAGE:CUDA>:SHELL:-Xcudafe --diag_suppress=virtual_function_decl_hidden>"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [x] GPU path tracing (megakernel/wavefront).
- [x] GPU volumetric rendering (wavefront).
- [x] Spectral or tristimulus (RGB) rendering.
- [x] Post processing passes (e.g. denoising).
- [x] Basic support for scene animation (glTF).

Expand Down
22 changes: 0 additions & 22 deletions common/build/FindOptiX.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@ elseif(NOT OptiX_INSTALL_DIR)
"C:/ProgramData/NVIDIA Corporation/OptiX SDK 7.0.0"
"C:/ProgramData/NVIDIA Corporation/OptiX SDK *"
)
else()
find_path(searched_OptiX_INSTALL_DIR
NAME include/optix.h
PATHS
"/usr/local/NVIDIA-OptiX-SDK-7.5.0-linux64-x86_64"
"/usr/local/NVIDIA-OptiX-SDK-7.4.0-linux64-x86_64"
"/usr/local/NVIDIA-OptiX-SDK-7.3.0-linux64-x86_64"
"/usr/local/NVIDIA-OptiX-SDK-7.2.0-linux64-x86_64"
"/usr/local/NVIDIA-OptiX-SDK-7.1.0-linux64-x86_64"
"/usr/local/NVIDIA-OptiX-SDK-7.0.0-linux64-x86_64"
"/usr/local/NVIDIA-OptiX-SDK-*"
)
endif()
endif()
mark_as_advanced(searched_OptiX_INSTALL_DIR)
Expand All @@ -41,16 +29,6 @@ set(OptiX_ROOT_DIR ${searched_OptiX_INSTALL_DIR} CACHE PATH "Path to OptiX insta
set(OptiX_INSTALL_DIR $ENV{OptiX_INSTALL_DIR} CACHE PATH "Path to OptiX installed location.")
set(OptiX_ROOT_DIR ${searched_OptiX_INSTALL_DIR} CACHE PATH "Path to OptiX installed location.")

# The distribution contains both 32 and 64 bit libraries. Adjust the library
# search path based on the bit-ness of the build. (i.e. 64: bin64, lib64; 32:
# bin, lib). Note that on Mac, the OptiX library is a universal binary, so we
# only need to look in lib and not lib64 for 64 bit builds.
if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT APPLE)
set(bit_dest "64")
else()
set(bit_dest "")
endif()

# Include
find_path(OptiX_INCLUDE
NAMES optix.h
Expand Down
19 changes: 15 additions & 4 deletions common/build/source.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ SET (KRR_SOURCE
${KRR_RENDER_SOURCE_DIR}/render/profiler/profiler.cpp
${KRR_RENDER_SOURCE_DIR}/render/profiler/ui.cpp
${KRR_RENDER_SOURCE_DIR}/render/media.cpp
${KRR_RENDER_SOURCE_DIR}/render/color.cpp
${KRR_RENDER_SOURCE_DIR}/render/spectrum.cpp
${KRR_RENDER_SOURCE_DIR}/scene/assimp.cpp
${KRR_RENDER_SOURCE_DIR}/scene/pbrt.cpp
${KRR_RENDER_SOURCE_DIR}/scene/openvdb.cpp
Expand All @@ -56,6 +58,15 @@ SET (KRR_SOURCE
${KRR_RENDER_SOURCE_DIR}/util/image.cpp
)

SET (KRR_SOURCE
${KRR_SOURCE}
${KRR_RENDER_SOURCE_DIR}/data/rgbspectrum_srgb.cpp
${KRR_RENDER_SOURCE_DIR}/data/rgbspectrum_aces.cpp
${KRR_RENDER_SOURCE_DIR}/data/rgbspectrum_dci_p3.cpp
${KRR_RENDER_SOURCE_DIR}/data/rgbspectrum_rec2020.cpp
${KRR_RENDER_SOURCE_DIR}/data/named_spectrum.cpp
)

SET (KRR_SOURCE_VULKAN
${KRR_RENDER_SOURCE_DIR}/core/vulkan/binding.cpp
${KRR_RENDER_SOURCE_DIR}/core/vulkan/cuvk.cpp
Expand All @@ -71,6 +82,8 @@ SET_SOURCE_FILES_PROPERTIES (
# the symbols that are defined within these .cpp files.
${KRR_RENDER_SOURCE_DIR}/core/light.cpp
${KRR_RENDER_SOURCE_DIR}/render/media.cpp
${KRR_RENDER_SOURCE_DIR}/render/color.cpp
${KRR_RENDER_SOURCE_DIR}/render/spectrum.cpp
${KRR_RENDER_SOURCE_DIR}/render/wavefront/integrator.cpp
${KRR_RENDER_SOURCE_DIR}/render/wavefront/medium.cpp
${KRR_RENDER_SOURCE_DIR}/render/passes/denoise/denoise.cpp
Expand Down Expand Up @@ -108,8 +121,8 @@ INCLUDE_DIRECTORIES (${KRR_INCLUDE_ALL})
INCLUDE (${KRR_RENDER_ROOT}/common/build/CompilePTX.cmake)
# the argument's name must match the extern variable declared in host c++ code
CUDA_COMPILE_EMBED(GBUFFER_PTX ${KRR_SHADER_REL_DIR}render/passes/gbuffer/device.cu krr-gbuffer krr_soa_generated)
CUDA_COMPILE_EMBED(PATHTRACER_PTX ${KRR_SHADER_REL_DIR}render/path/path.cu krr-path krr_soa_generated)
CUDA_COMPILE_EMBED(WAVEFRONT_PTX ${KRR_SHADER_REL_DIR}render/wavefront/wavefront.cu krr-wavefront krr_soa_generated)
CUDA_COMPILE_EMBED(PATHTRACER_PTX ${KRR_SHADER_REL_DIR}render/path/device.cu krr-path krr_soa_generated)
CUDA_COMPILE_EMBED(WAVEFRONT_PTX ${KRR_SHADER_REL_DIR}render/wavefront/device.cu krr-wavefront krr_soa_generated)
CUDA_COMPILE_EMBED(BDPT_PTX ${KRR_SHADER_REL_DIR}render/bdpt/device.cu krr-bdpt krr_soa_generated)

SET(KRR_PTX_FILES
Expand All @@ -127,14 +140,12 @@ SET(KRR_SOURCE
${KRR_SOURCE}
${KRR_RENDER_SOURCE_DIR}/misc/render/ppg/integrator.cpp
${KRR_RENDER_SOURCE_DIR}/misc/render/ppg/treemanip.cpp
${KRR_RENDER_SOURCE_DIR}/misc/render/ppg/medium.cpp
${KRR_RENDER_SOURCE_DIR}/misc/render/zero_guiding/integrator.cpp
${KRR_RENDER_SOURCE_DIR}/misc/render/zero_guiding/medium.cpp
)

SET_SOURCE_FILES_PROPERTIES (
${KRR_RENDER_SOURCE_DIR}/misc/render/ppg/integrator.cpp
${KRR_RENDER_SOURCE_DIR}/misc/render/ppg/medium.cpp
${KRR_RENDER_SOURCE_DIR}/misc/render/zero_guiding/integrator.cpp
${KRR_RENDER_SOURCE_DIR}/misc/render/zero_guiding/medium.cpp
PROPERTIES LANGUAGE CUDA
Expand Down
2 changes: 1 addition & 1 deletion common/configs/example_cbox.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"enable": true,
"name": "ToneMappingPass",
"params": {
"exposure": 0.5,
"exposure": 1,
"operator": "aces"
}
}
Expand Down
76 changes: 76 additions & 0 deletions common/configs/example_megakernel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"model": "common/assets/scenes/cbox/cbox.obj",
"resolution": [
750,
750
],
"passes": [
{
"enable": true,
"name": "MegakernelPathTracer",
"params": {
"nee": true,
"rr": 0.8,
"max_depth": 10
}
},
{
"enable": true,
"name": "AccumulatePass",
"params": {
"spp": 0,
"mode": "accumulate"
}
},
{
"enable": false,
"name": "DenoisePass"
},
{
"enable": true,
"name": "ToneMappingPass",
"params": {
"exposure": 1,
"operator": "aces"
}
}
],
"scene": {
"camera": {
"mData": {
"aspectRatio": 1.0,
"focalDistance": 10.0,
"focalLength": 21.0,
"lensRadius": 0.0,
"pos": [
-0.010055910795927048,
1.0200917720794678,
2.828592300415039
],
"target": [
-0.011140584014356136,
1.0211291313171387,
-0.2837386131286621
],
"up": [
0.0,
1.0,
0.0
]
}
},
"cameraController": {
"mData": {
"pitch": 0.0003333091735839844,
"radius": 3.1123313903808594,
"target": [
-0.011140584014356136,
1.0211291313171387,
-0.2837386131286621
],
"yaw": 0.000348508358001709
}
}
},
"spp": 0
}
2 changes: 1 addition & 1 deletion src/core/config.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define KRR_BUILD_TYPE "${CMAKE_BUILD_TYPE}"

#cmakedefine01 KRR_BUILD_STARLIGHT
#
#cmakedefine01 KRR_RENDER_SPECTRAL

#define KRR_ENABLE_PROFILE 1

Expand Down
15 changes: 12 additions & 3 deletions src/core/device/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include "logger.h"
#include "context.h"
#include "renderpass.h"
#include "render/color.h"
#include "render/spectrum.h"

KRR_NAMESPACE_BEGIN

using namespace gpu;
Context::SharedPtr gpContext;
std::unique_ptr<Context> gpContext;
std::shared_ptr<RenderPassFactory::map_type> RenderPassFactory::map = nullptr;
std::shared_ptr<RenderPassFactory::configured_map_type> RenderPassFactory::configured_map = nullptr;
CUDATrackedMemory CUDATrackedMemory::singleton;
Expand Down Expand Up @@ -45,8 +47,7 @@ void Context::initialize() {
Log(Debug, "Concurrent access of managed memory is not supported.");

CUresult cuRes = cuCtxGetCurrent(&cudaContext);
if (cuRes != CUDA_SUCCESS)
logError("Error querying current context: error code " + cuRes);
if (cuRes != CUDA_SUCCESS) Log(Error, "Error querying current context: error code " + cuRes);

OptixDeviceContextOptions optixContextOptions = {};
//optixContextOptions.validationMode = OPTIX_DEVICE_CONTEXT_VALIDATION_MODE_ALL;
Expand All @@ -58,6 +59,14 @@ void Context::initialize() {
// tracked cuda device memory management
set_default_resource(&CUDATrackedMemory::singleton);
alloc = new Allocator(&CUDATrackedMemory::singleton);

// initialize spectral rendering resources
#if KRR_RENDER_SPECTRAL
spec::init(*alloc);
RGBToSpectrumTable::init(*alloc);
RGBColorSpace::init(*alloc);
CUDA_SYNC_CHECK();
#endif
}

void Context::finalize(){
Expand Down
2 changes: 1 addition & 1 deletion src/core/device/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ class Context{
bool exit{};
};

extern Context::SharedPtr gpContext;
extern std::unique_ptr<Context> gpContext;

KRR_NAMESPACE_END
2 changes: 1 addition & 1 deletion src/core/device/cuda.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <typeindex>

#include "common.h"
#include "taggedptr.h"
#include "device/taggedptr.h"

KRR_NAMESPACE_BEGIN

Expand Down
4 changes: 2 additions & 2 deletions src/core/device/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ void RTScene::uploadSceneMediumData() {
// For a medium, its index in mediumBuffer is the same as medium->getMediumId();
for (auto medium : mScene.lock()->getMedia()) {
if (auto m = std::dynamic_pointer_cast<HomogeneousVolume>(medium)) {
HomogeneousMedium gMedium(m->sigma_a, m->sigma_s, m->Le, m->g);
HomogeneousMedium gMedium(m->sigma_a, m->sigma_s, m->Le, m->g, KRR_DEFAULT_COLORSPACE);
mHomogeneousMedium.push_back(gMedium);
} else if (auto m = std::dynamic_pointer_cast<VDBVolume>(medium)) {
float maxDensity{0};
mNanoVDBMedium.emplace_back(m->getNode()->getGlobalTransform(), m->sigma_a, m->sigma_s,
m->g, std::move(*m->densityGrid));
m->g, std::move(*m->densityGrid), KRR_DEFAULT_COLORSPACE);
mNanoVDBMedium.back().initializeFromHost();
}
}
Expand Down
File renamed without changes.
15 changes: 0 additions & 15 deletions src/core/light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,6 @@ KRR_NAMESPACE_BEGIN

namespace rt {

KRR_DEVICE LightSample DiffuseAreaLight::sampleLi(Vector2f u,
const LightSampleContext &ctx) const {
LightSample ls = {};
ShapeSampleContext shapeCtx = {ctx.p, ctx.n};
ShapeSample ss = shape.sample(u, shapeCtx);
DCHECK(!isnan(ss.pdf));
Interaction &intr = ss.intr;
intr.wo = normalize(ctx.p - intr.p);

ls.intr = intr;
ls.pdf = ss.pdf;
ls.L = L(intr.p, intr.n, intr.uv, intr.wo);
return ls;
}

}

KRR_NAMESPACE_END
Loading

0 comments on commit 6d3ff20

Please sign in to comment.