Skip to content

Commit

Permalink
Implement wavelength-dependent refraction (spectral eta) dielectric m…
Browse files Browse the repository at this point in the history
…aterials, and conductor brdf (#56)

* implement spectral eta for dielectric mats
* implement conductor brdf
* now can override existing materials by specifying desired parameters in scene config
  • Loading branch information
cuteday authored Apr 18, 2024
1 parent 3808e7f commit 9c45393
Show file tree
Hide file tree
Showing 25 changed files with 335 additions and 85 deletions.
9 changes: 7 additions & 2 deletions common/configs/example_cbox.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"model": "common/assets/scenes/cbox/cbox.obj",
"resolution": [
750,
750
Expand Down Expand Up @@ -55,6 +54,12 @@
],
"yaw": 0.000348508358001709
}
}
},
"model": [
{
"model": "common/assets/scenes/cbox/cbox.obj",
"name": "Cornell Box"
}
]
}
}
7 changes: 4 additions & 3 deletions src/core/device/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ void Context::initialize() {

// tracked cuda device memory management
set_default_resource(&CUDATrackedMemory::singleton);
alloc = new Allocator(&CUDATrackedMemory::singleton);
alloc = std::make_unique<Allocator>(&CUDATrackedMemory::singleton);

// initialize spectral rendering resources
#if KRR_RENDER_SPECTRAL
spec::init(*alloc);
#if KRR_RENDER_SPECTRAL
RGBToSpectrumTable::init(*alloc);
RGBColorSpace::init(*alloc);
CUDA_SYNC_CHECK();
Expand All @@ -70,8 +70,9 @@ void Context::initialize() {

void Context::finalize(){
CUDA_SYNC_CHECK();
CUDATrackedMemory::singleton.release();
optixDeviceContextDestroy(optixContext);
delete alloc;
alloc.reset();
cuCtxDestroy(cudaContext);
}

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 @@ -35,7 +35,7 @@ class Context{
cudaDeviceProp deviceProps;
OptixDeviceContext optixContext;
nvrhi::vulkan::IDevice *defaultVkDevice{};
Allocator* alloc;
std::unique_ptr<Allocator> alloc;
// signal bits
bool exit{};
};
Expand Down
2 changes: 2 additions & 0 deletions src/core/device/gpustd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ namespace gpu {
bool do_is_equal(const memory_resource& other) const noexcept {
return this == &other;
}

void do_release() {}
};

static NewDeleteResource* ndr;
Expand Down
14 changes: 8 additions & 6 deletions src/core/device/gpustd.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ namespace gpu {
static constexpr size_t max_align = alignof(std::max_align_t);

public:
virtual ~memory_resource() {};
void* allocate(size_t bytes, size_t alignment = max_align) {
virtual ~memory_resource() {}
void release() { do_release(); }
void *allocate(size_t bytes, size_t alignment = max_align) {
if (bytes == 0)
return nullptr;
return do_allocate(bytes, alignment);
}
void deallocate(void* p, size_t bytes, size_t alignment = max_align) {
void deallocate(void *p, size_t bytes, size_t alignment = max_align) {
if (!p)
return;
return do_deallocate(p, bytes, alignment);
}
bool is_equal(const memory_resource& other) const noexcept {
bool is_equal(const memory_resource &other) const noexcept {
return do_is_equal(other);
}

private:
virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
virtual void do_deallocate(void* p, size_t bytes, size_t alignment) = 0;
virtual void do_release() = 0;
virtual void *do_allocate(size_t bytes, size_t alignment) = 0;
virtual void do_deallocate(void *p, size_t bytes, size_t alignment) = 0;
virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;
};

Expand Down
29 changes: 19 additions & 10 deletions src/core/device/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,28 @@
NAMESPACE_BEGIN(krr)

class CUDAMemory : public gpu::memory_resource {
void *do_allocate(size_t size, size_t alignment){
void *do_allocate(size_t size, size_t alignment) override {
void *ptr;
CUDA_CHECK(cudaMallocManaged(&ptr, size));
CHECK_EQ(0, intptr_t(ptr) % alignment);
return ptr;
}

void do_deallocate(void *p, size_t bytes, size_t alignment) {
void do_deallocate(void *p, size_t bytes, size_t alignment) override {
CUDA_CHECK(cudaFree(p));
}

bool do_is_equal(const memory_resource &other) const noexcept {
bool do_is_equal(const memory_resource &other) const noexcept override {
return this == &other;
}

void do_release() override {}
};

class CUDATrackedMemory : public CUDAMemory {
public:
void *do_allocate(size_t size, size_t alignment){
if (size == 0)
return nullptr;
void *do_allocate(size_t size, size_t alignment) override {
if (size == 0) return nullptr;

void *ptr;
CUDA_CHECK(cudaMallocManaged(&ptr, size));
Expand All @@ -51,9 +52,8 @@ class CUDATrackedMemory : public CUDAMemory {
return ptr;
}

void do_deallocate(void* p, size_t size, size_t alignment) {
if (!p)
return;
void do_deallocate(void *p, size_t size, size_t alignment) override {
if (!p) return;

CUDA_CHECK(cudaFree(p));

Expand All @@ -64,10 +64,19 @@ class CUDATrackedMemory : public CUDAMemory {
bytesAllocated -= size;
}

bool do_is_equal(const memory_resource &other) const noexcept {
bool do_is_equal(const memory_resource &other) const noexcept override {
return this == &other;
}

void do_release() override {
std::lock_guard<std::mutex> lock(mutex);
for (auto iter : allocations)
CUDA_CHECK(cudaFree(iter.first));
allocations.clear();
bytesAllocated = 0;
}


void PrefetchToGPU() const {
// only linux supports uniform memory prefetching on demand
#if KRR_PLATFORM_LINUX
Expand Down
3 changes: 3 additions & 0 deletions src/core/math/include/krrmath/complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,7 @@ template <typename T> KRR_CALLABLE T abs(const Complex<T> &z) { return z.abs();

template <typename T> KRR_CALLABLE Complex<T> sqrt(const Complex<T> &z) { return z.sqrt(); }

using Complexf = Complex<float>;
using Complexd = Complex<double>;

NAMESPACE_END(krr)
9 changes: 9 additions & 0 deletions src/core/raytracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ enum class MaterialType : uint32_t{
Null = 0,
Diffuse,
Dielectric,
Conductor,
Disney,
Count
};

KRR_ENUM_DEFINE(MaterialType, {
{MaterialType::Null, "null"},
{MaterialType::Diffuse, "diffuse"},
{MaterialType::Conductor, "conductor"},
{MaterialType::Dielectric, "dielectric"},
{MaterialType::Disney, "disney"}
})

class Ray {
public:
KRR_CALLABLE Ray() = default;
Expand Down
6 changes: 4 additions & 2 deletions src/core/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,16 @@ void Material::renderUI() {
static const char *shadingModels[] = {"MetallicRoughness", "SpecularGlossiness"};
static const char *textureTypes[] = {"Diffuse", "Specular", "Emissive", "Normal",
"Transmission"};
static const char *bsdfTypes[] = {"Null", "Diffuse", "Dielectric", "Disney"};
static const char *bsdfTypes[] = {"Null", "Diffuse", "Dielectric", "Conductor", "Disney"};
bool updated = false;
updated |= ui::ListBox("Shading model", (int *) &mShadingModel, shadingModels, 2);
updated |= ui::ListBox("BSDF", (int *) &mBsdfType, bsdfTypes, (int) MaterialType::Count);
updated |= ui::DragFloat4("Diffuse", (float *) &mMaterialParams.diffuse, 1e-3, 0, 1);
updated |= ui::DragFloat4("Specular", (float *) &mMaterialParams.specular, 1e-3, 0, 1);
updated |= ui::DragFloat("Specular transmission", &mMaterialParams.specularTransmission, 1e-3, 0, 1);
updated |= ui::InputFloat("Index of Refraction", &mMaterialParams.IoR);
if (mMaterialParams.spectralEta) ui::Text("Has Spectral Eta");
else updated |= ui::InputFloat("IoR", &mMaterialParams.IoR);
if (mMaterialParams.spectralK) ui::Text("Has Spectral K");
setUpdated(updated);
}

Expand Down
18 changes: 16 additions & 2 deletions src/core/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ class Material : public SceneGraphLeaf {
struct MaterialParams {
RGBA diffuse{ 1 }; // RGB for base color and A (optional) for opacity
RGBA specular{ 0 }; // G-roughness B-metallic A-shininess in MetalRough model
// RGB - specular color (F0); A - shininess in SpecGloss model
// RGB - specular color (F0); A - shininess in SpecGloss model
float specularTransmission{0};
float IoR{ 1.5f };
float specularTransmission{ 0 };
Spectra spectralEta{}, spectralK{};
};

Material() {};
Expand Down Expand Up @@ -163,6 +164,19 @@ class Material : public SceneGraphLeaf {
int mMaterialId{-1};
};

KRR_ENUM_DEFINE(Material::TextureType, {
{Material::TextureType::Diffuse, "diffuse"},
{Material::TextureType::Specular, "specular"},
{Material::TextureType::Normal, "normal"},
{Material::TextureType::Emissive, "emissive"},
{Material::TextureType::Transmission, "transmission"},
})

KRR_ENUM_DEFINE(Material::ShadingModel, {
{Material::ShadingModel::MetallicRoughness, "metallic_roughness"},
{Material::ShadingModel::SpecularGlossiness, "specular_glossiness"},
})

namespace rt {
class TextureData {
public:
Expand Down
6 changes: 5 additions & 1 deletion src/main/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace { // status bits
static bool sShowUI = true;
static bool sSaveHDR = false;
static bool sSaveFrames = false;
static bool sLockCamera = false;
static bool sRequestScreenshot = false;
static size_t sSaveFrameInterval = 2;
}
Expand All @@ -32,7 +33,7 @@ void RenderApp::backBufferResized() {
bool RenderApp::onMouseEvent(io::MouseEvent &mouseEvent) {
if (mpUIRenderer->onMouseEvent(mouseEvent)) return true;
if (DeviceManager::onMouseEvent(mouseEvent)) return true;
if (mScene && mScene->onMouseEvent(mouseEvent)) return true;
if (!sLockCamera && mScene && mScene->onMouseEvent(mouseEvent)) return true;
return false;
}

Expand Down Expand Up @@ -136,6 +137,7 @@ void RenderApp::renderUI() {
ui::EndMenu();
}
if (ui::BeginMenu("Tools")) {
ui::MenuItem("Lock Camera", NULL, &sLockCamera);
if (ui::MenuItem("Save config")) saveConfig("");
ui::MenuItem("Save HDR", NULL, &sSaveHDR);
if (ui::MenuItem("Screen shot")) sRequestScreenshot = true;
Expand Down Expand Up @@ -255,6 +257,7 @@ void RenderApp::loadConfig(const json config) {
if (config.contains("renderer")) {
const json render_config = config.at("renderer");
sSaveHDR = render_config.value("save_hdr", true);
sLockCamera = render_config.value("lock_camera", false);
sSaveFrames = render_config.value("save_frames", false);
sSaveFrameInterval = render_config.value("save_frame_interval", 5);
}
Expand Down Expand Up @@ -326,6 +329,7 @@ void RenderApp::finalize() {
mScene.reset();
// Destroy created vulkan resources before destroy vulkan device
shutdown();
gpContext.reset();
}

NAMESPACE_END(krr)
3 changes: 2 additions & 1 deletion src/render/bsdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#include "materials/null.h"
#include "materials/diffuse.h"
#include "materials/disney.h"
#include "materials/conductor.h"
#include "materials/dielectric.h"

NAMESPACE_BEGIN(krr)

class BxDF :public TaggedPointer<NullBsdf, DiffuseBrdf,
DielectricBsdf, DisneyBsdf>{
DielectricBsdf, ConductorBsdf, DisneyBsdf>{
public:
using TaggedPointer::TaggedPointer;

Expand Down
7 changes: 7 additions & 0 deletions src/render/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ enum class ColorSpaceType {
ACES2065_1,
};

KRR_ENUM_DEFINE(ColorSpaceType, {
{ColorSpaceType::sRGB, "sRGB"},
{ColorSpaceType::DCI_P3, "DCI_P3"},
{ColorSpaceType::Rec2020, "Rec2020"},
{ColorSpaceType::ACES2065_1, "ACES2065_1"},
})

class RGB : public Array3f {
public:
using Array3f::Array3f;
Expand Down
Loading

0 comments on commit 9c45393

Please sign in to comment.