Skip to content

Commit

Permalink
use LaunchParameter<Integrator>
Browse files Browse the repository at this point in the history
instead of LaunchParamsIntegrator
  • Loading branch information
cuteday committed Nov 10, 2023
1 parent a0e9812 commit e1aabe1
Show file tree
Hide file tree
Showing 24 changed files with 87 additions and 60 deletions.
14 changes: 7 additions & 7 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
[submodule "src/ext/assimp"]
path = src/ext/assimp
url = https://github.com/assimp/assimp.git
url = git@github.com:assimp/assimp.git
[submodule "src/ext/glfw"]
path = src/ext/glfw
url = https://github.com/glfw/glfw.git
url = git@github.com:glfw/glfw.git
[submodule "src/ext/pybind11"]
path = src/ext/pybind11
url = https://github.com/pybind/pybind11.git
url = git@github.com:pybind/pybind11.git
branch = stable
[submodule "src/ext/pbrtparser"]
path = src/ext/pbrtparser
url = https://github.com/cuteday/pbrt-parser.git
url = git@github.com:cuteday/pbrt-parser.git
branch = cute
[submodule "src/core/math/3rdparty/eigen"]
path = src/core/math/3rdparty/eigen
url = https://github.com/cuteday/Eigen.git
url = git@github.com:cuteday/Eigen.git
[submodule "src/ext/nvrhi"]
path = src/ext/nvrhi
url = https://github.com/cuteday/nvrhi.git
url = git@github.com:cuteday/nvrhi.git
[submodule "src/ext/openvdb"]
path = src/ext/openvdb
url = https://github.com/cuteday/openvdb_win.git
url = git@github.com:cuteday/openvdb_win.git
23 changes: 15 additions & 8 deletions src/core/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,29 @@ class Camera {
Vector3f u{ 1, 0, 0 }; // camera right [dependent to aspect ratio]
Vector3f v{ 0, 1, 0 }; // camera up [dependent to aspect ratio]
Vector3f w{ 0, 0, -1 }; // camera forward

Medium medium{nullptr}; // the ray is inside the medium

KRR_CALLABLE Ray getRay(Vector2i pixel, Vector2i frameSize, Sampler& sampler) {
Ray ray;
/* 1. Statified sample on the film plane (within the fragment) */
Vector2f p = (Vector2f)pixel + Vector2f(0.5f) + sampler.get2D(); // uniform sample + box filter
Vector2f ndc = Vector2f(2 * p) / Vector2f(frameSize) + Vector2f(-1.f); // ndc in [-1, 1]^2
if (lensRadius > 0) { /*Thin lens*/
Vector2f p =
(Vector2f) pixel + Vector2f(0.5f) + sampler.get2D(); // uniform sample + box filter
Vector2f ndc =
Vector2f(2 * p) / Vector2f(frameSize) + Vector2f(-1.f); // ndc in [-1, 1]^2
if (lensRadius > 0) { /*Thin lens*/
/* 2. Sample the lens (uniform) */
Vector3f focalPoint = pos + ndc[0] * u + ndc[1] * v + w;
Vector2f apertureSample = lensRadius > M_EPSILON ? uniformSampleDisk(sampler.get2D()) : Vector2f::Zero();
ray.origin = pos + lensRadius * (apertureSample[0] * normalize(u) + apertureSample[1] * normalize(v));
ray.dir = normalize(focalPoint - ray.origin);
} else { /*Pin hole*/
Vector2f apertureSample =
lensRadius > M_EPSILON ? uniformSampleDisk(sampler.get2D()) : Vector2f::Zero();
ray.origin = pos + lensRadius * (apertureSample[0] * normalize(u) +
apertureSample[1] * normalize(v));
ray.dir = normalize(focalPoint - ray.origin);
} else { /*Pin hole*/
ray.origin = pos;
ray.dir = normalize(ndc[0] * u + ndc[1] * v + w);
ray.dir = normalize(ndc[0] * u + ndc[1] * v + w);
}
ray.medium = medium;
return ray;
}

Expand Down
18 changes: 10 additions & 8 deletions src/core/device/optix.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,19 @@ class OptixBackend {

void initialize(const OptixInitializeParameters& params);
void setScene(std::shared_ptr<Scene> _scene);
template <typename T>
void launch(const T& parameters, string entryPoint, int width,
int height, int depth = 1) {
template <typename Integrator>
void launch(const LaunchParameters<Integrator>& parameters, string entryPoint,
int width, int height, int depth = 1) {
if (height * width * depth == 0) return;
static T *launchParams{nullptr};
if (!launchParams) cudaMalloc(&launchParams, sizeof(T));
static LaunchParameters<Integrator> *launchParams{nullptr};
if (!launchParams) cudaMalloc(&launchParams, sizeof(LaunchParameters<Integrator>));
if (!entryPoints.count(entryPoint))
Log(Fatal, "The entrypoint %s is not initialized!", entryPoint.c_str());
cudaMemcpyAsync(launchParams, &parameters, sizeof(T), cudaMemcpyHostToDevice, cudaStream);
OPTIX_CHECK(optixLaunch(optixPipeline, cudaStream, CUdeviceptr(launchParams),
sizeof(T), &SBT[entryPoints[entryPoint]], width, height, depth));
cudaMemcpyAsync(launchParams, &parameters, sizeof(LaunchParameters<Integrator>),
cudaMemcpyHostToDevice, cudaStream);
OPTIX_CHECK(optixLaunch(optixPipeline, cudaStream, CUdeviceptr(launchParams),
sizeof(LaunchParameters<Integrator>), &SBT[entryPoints[entryPoint]],
width, height, depth));
}

std::shared_ptr<Scene> getScene() const { return scene; }
Expand Down
3 changes: 3 additions & 0 deletions src/core/device/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,7 @@ class RTScene {
std::shared_ptr<OptixScene> mOptixScene;
};

template <typename Integrator>
struct LaunchParameters {};

KRR_NAMESPACE_END
2 changes: 1 addition & 1 deletion src/main/kiraray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern "C" int main(int argc, char *argv[]) {
"Switch to Release build for normal performance!");
#endif

string configFile = "common/configs/example_cbox.json";
string configFile = "common/configs/example_volbox.json";
if (argc < 2){
Log(Warning, "No config file specified, using default config file: %s", configFile.c_str());
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/misc/render/ppg/device.cu
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using namespace krr;
KRR_NAMESPACE_BEGIN

extern "C" __constant__ LaunchParamsPPG launchParams;
extern "C" __constant__ LaunchParameters<PPGPathTracer> launchParams;

template <typename... Args>
KRR_DEVICE_FUNCTION void traceRay(OptixTraversableHandle traversable, Ray ray,
Expand Down
4 changes: 2 additions & 2 deletions src/misc/render/ppg/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ void PPGPathTracer::initialize() {

void PPGPathTracer::traceClosest(int depth) {
PROFILE("Trace intersect rays");
static LaunchParamsPPG params = {};
static LaunchParameters<PPGPathTracer> params = {};
params.traversable = backend->getRootTraversable();
params.sceneData = backend->getSceneData();
params.colorSpace = KRR_DEFAULT_COLORSPACE;
Expand All @@ -91,7 +91,7 @@ void PPGPathTracer::traceClosest(int depth) {

void PPGPathTracer::traceShadow() {
PROFILE("Trace shadow rays");
static LaunchParamsPPG params = {};
static LaunchParameters<PPGPathTracer> params = {};
params.traversable = backend->getRootTraversable();
params.sceneData = backend->getSceneData();
params.colorSpace = KRR_DEFAULT_COLORSPACE;
Expand Down
7 changes: 5 additions & 2 deletions src/misc/render/ppg/ppg.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

KRR_NAMESPACE_BEGIN

typedef struct {
class PPGPathTracer;

template <>
struct LaunchParameters<PPGPathTracer> {
RayQueue* currentRayQueue;
RayQueue* nextRayQueue;
ShadowRayQueue* shadowRayQueue;
Expand All @@ -19,6 +22,6 @@ typedef struct {
rt::SceneData sceneData;
GuidedPathStateBuffer* guidedState;
OptixTraversableHandle traversable;
} LaunchParamsPPG;
};

KRR_NAMESPACE_END
2 changes: 1 addition & 1 deletion src/misc/render/zero_guiding/device.cu
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using namespace krr;
KRR_NAMESPACE_BEGIN

extern "C" __constant__ LaunchParamsZero launchParams;
extern "C" __constant__ LaunchParameters<ZeroGuidingPT> launchParams;

template <typename... Args>
KRR_DEVICE_FUNCTION void traceRay(OptixTraversableHandle traversable, Ray ray,
Expand Down
4 changes: 2 additions & 2 deletions src/misc/render/zero_guiding/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void ZeroGuidingPT::initialize() {

void ZeroGuidingPT::traceClosest(int depth) {
PROFILE("Trace intersect rays");
static LaunchParamsZero params = {};
static LaunchParameters<ZeroGuidingPT> params = {};
params.traversable = backend->getRootTraversable();
params.sceneData = backend->getSceneData();
params.colorSpace = KRR_DEFAULT_COLORSPACE;
Expand All @@ -90,7 +90,7 @@ void ZeroGuidingPT::traceClosest(int depth) {

void ZeroGuidingPT::traceShadow() {
PROFILE("Trace shadow rays");
static LaunchParamsZero params = {};
static LaunchParameters<ZeroGuidingPT> params = {};
params.traversable = backend->getRootTraversable();
params.sceneData = backend->getSceneData();
params.colorSpace = KRR_DEFAULT_COLORSPACE;
Expand Down
7 changes: 5 additions & 2 deletions src/misc/render/zero_guiding/zeroguiding.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

KRR_NAMESPACE_BEGIN

typedef struct {
class ZeroGuidingPT;

template <>
struct LaunchParameters<ZeroGuidingPT> {
RayQueue* currentRayQueue;
RayQueue* nextRayQueue;
ShadowRayQueue* shadowRayQueue;
Expand All @@ -19,6 +22,6 @@ typedef struct {
rt::SceneData sceneData;
GuidedPathStateBuffer* guidedState;
OptixTraversableHandle traversable;
} LaunchParamsZero;
};

KRR_NAMESPACE_END
4 changes: 3 additions & 1 deletion src/render/bdpt/bdpt.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
KRR_NAMESPACE_BEGIN

using namespace shader;
class BDPTIntegrator;

struct LaunchParamsBDPT {
template <>
struct LaunchParameters<BDPTIntegrator> {
uint frameID{ 0 };
Vector2i fbSize = Vector2i::Zero();
bool debugOutput = false;
Expand Down
2 changes: 1 addition & 1 deletion src/render/bdpt/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ KRR_NAMESPACE_BEGIN
extern "C" char BDPT_PTX[];

namespace {
LaunchParamsBDPT *launchParamsDevice{};
LaunchParameters<BDPTIntegrator> *launchParamsDevice{};
}

BDPTIntegrator::BDPTIntegrator() {
Expand Down
2 changes: 1 addition & 1 deletion src/render/bdpt/integrator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BDPTIntegrator: public RenderPass{
OptixPipeline pipeline;
OptixModule module;

LaunchParamsBDPT launchParams;
LaunchParameters<BDPTIntegrator> launchParams;
};

KRR_NAMESPACE_END
2 changes: 1 addition & 1 deletion src/render/passes/gbuffer/device.cu
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using namespace krr;
KRR_NAMESPACE_BEGIN

extern "C" __constant__ LaunchParamsGBuffer launchParams;
extern "C" __constant__ LaunchParameters <GBufferPass> launchParams;

template <typename... Args>
KRR_DEVICE_FUNCTION void traceRay(OptixTraversableHandle traversable, Ray ray, float tMax,
Expand Down
7 changes: 5 additions & 2 deletions src/render/passes/gbuffer/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

KRR_NAMESPACE_BEGIN

typedef struct {
class GBufferPass;

template <>
struct LaunchParameters<GBufferPass> {
Vector2i frameSize;
size_t frameIndex;

rt::SceneData sceneData;
Camera::CameraData cameraData;
OptixTraversableHandle traversable;
} LaunchParamsGBuffer;
};

KRR_NAMESPACE_END
2 changes: 1 addition & 1 deletion src/render/passes/gbuffer/gbuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void GBufferPass::setScene(Scene::SharedPtr scene) {

void GBufferPass::render(RenderContext* context) {
PROFILE("GBuffer drawing");
static LaunchParamsGBuffer launchParams = {};
static LaunchParameters <GBufferPass> launchParams = {};
launchParams.frameIndex = getFrameIndex();
launchParams.frameSize = getFrameSize();
launchParams.cameraData = mScene->getCamera()->getCameraData();
Expand Down
2 changes: 1 addition & 1 deletion src/render/passes/gbuffer/gbuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GBufferPass : public RenderPass {

private:
OptixBackend::SharedPtr mOptixBackend;
LaunchParamsGBuffer mLaunchParams;
LaunchParameters <GBufferPass> mLaunchParams;

bool mEnableDepth{};
bool mEnableDiffuse{};
Expand Down
2 changes: 1 addition & 1 deletion src/render/path/device.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ KRR_NAMESPACE_BEGIN
using namespace shader;
using namespace rt;

extern "C" __constant__ LaunchParamsPT launchParams;
extern "C" __constant__ LaunchParameters<MegakernelPathTracer> launchParams;

template <typename... Args>
KRR_DEVICE_FUNCTION void traceRay(OptixTraversableHandle traversable, Ray ray, float tMax,
Expand Down
18 changes: 10 additions & 8 deletions src/render/path/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,28 @@ const string shaderProgramNames[RAY_TYPE_COUNT] = {
"ShadowTransmittanceRay"
};

struct LaunchParamsPT {
uint frameID{ 0 };
class MegakernelPathTracer;

template <>
struct LaunchParameters<MegakernelPathTracer> {
uint frameID{0};
Vector2i fbSize = Vector2i::Zero();
// per pixel debugging output
bool debugOutput = false;
bool NEE = false; // enable next event estimation (and MIS)
Vector2i debugPixel = {960, 540};
bool debugOutput = false;
bool NEE = false; // enable next event estimation (and MIS)
Vector2i debugPixel = {960, 540};
int maxDepth = 10;
float probRR = 0.8;
float clampThreshold = 1e4f; // clamp max radiance contrib per frame
int spp = 1;
int lightSamples = 1;
// scene
// scene
Camera::CameraData camera;
LightSampler lightSampler;
rt::SceneData sceneData;

const RGBColorSpace *colorSpace;
CudaRenderTarget colorBuffer;
OptixTraversableHandle traversable{ 0 };
OptixTraversableHandle traversable{0};
};

struct PathData {
Expand Down
2 changes: 1 addition & 1 deletion src/render/path/pathtracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MegakernelPathTracer: public RenderPass{

private:
OptixBackend::SharedPtr optixBackend;
LaunchParamsPT launchParams;
LaunchParameters<MegakernelPathTracer> launchParams;

friend void to_json(json &j, const MegakernelPathTracer &p) {
j = json{
Expand Down
2 changes: 1 addition & 1 deletion src/render/wavefront/device.cu
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using namespace krr;
KRR_NAMESPACE_BEGIN

extern "C" __constant__ LaunchParams launchParams;
extern "C" __constant__ LaunchParameters <WavefrontPathTracer> launchParams;

template <typename... Args>
KRR_DEVICE_FUNCTION void traceRay(OptixTraversableHandle traversable, Ray ray,
Expand Down
4 changes: 2 additions & 2 deletions src/render/wavefront/integrator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void WavefrontPathTracer::initialize() {
void WavefrontPathTracer::traceClosest(int depth) {
PROFILE("Trace intersect rays");
// Telling whether volume rendering is enabled by mediumSampleQueue is null?
static LaunchParams params = {};
static LaunchParameters <WavefrontPathTracer> params = {};
params.traversable = backend->getRootTraversable();
params.sceneData = backend->getSceneData();
params.colorSpace = KRR_DEFAULT_COLORSPACE;
Expand All @@ -59,7 +59,7 @@ void WavefrontPathTracer::traceClosest(int depth) {

void WavefrontPathTracer::traceShadow() {
PROFILE("Trace shadow rays");
static LaunchParams params = {};
static LaunchParameters <WavefrontPathTracer> params = {};
params.traversable = backend->getRootTraversable();
params.sceneData = backend->getSceneData();
params.colorSpace = KRR_DEFAULT_COLORSPACE;
Expand Down
Loading

0 comments on commit e1aabe1

Please sign in to comment.