Skip to content

Commit

Permalink
Merge branch 'master' of github.com:favreau/BioExplorer
Browse files Browse the repository at this point in the history
  • Loading branch information
favreau committed Sep 22, 2023
2 parents f6d7376 + 3f0d3e5 commit a82a552
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 17 deletions.
1 change: 1 addition & 0 deletions platform/core/common/Properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Camera properties
*/
static const char* CAMERA_PROPERTY_TYPE_PERSPECTIVE = "perspective";
static const char* CAMERA_PROPERTY_TYPE_ORTHOGRAPHIC = "orthographic";
static const char* CAMERA_PROPERTY_TYPE_ANAGLYPH = "anaglyph";

static const char* CAMERA_PROPERTY_POSITION = "pos";
static const char* CAMERA_PROPERTY_DIRECTION = "dir";
Expand Down
3 changes: 3 additions & 0 deletions platform/engines/optix6/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ if(CMAKE_VERSION VERSION_GREATER "3.6" AND CMAKE_VERSION VERSION_LESS "3.10")
endif()

set(${NAME}_CU
cuda/camera/AnaglyphCamera.cu
cuda/camera/PerspectiveCamera.cu
cuda/camera/OrthographicCamera.cu
cuda/Constantbg.cu
Expand All @@ -66,6 +67,7 @@ set(${NAME}_SOURCES
OptiXFrameBuffer.cpp
OptiXScene.cpp
OptiXCamera.cpp
OptiXAnaglyphCamera.cpp
OptiXPerspectiveCamera.cpp
OptiXOrthographicCamera.cpp
OptiXRenderer.cpp
Expand All @@ -81,6 +83,7 @@ set_source_files_properties(
OptiXFrameBuffer.cpp
OptiXScene.cpp
OptiXCamera.cpp
OptiXAnaglyphCamera.cpp
OptiXPerspectiveCamera.cpp
OptiXOrthographicCamera.cpp
OptiXRenderer.cpp
Expand Down
92 changes: 92 additions & 0 deletions platform/engines/optix6/OptiXAnaglyphCamera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright (c) 2019, EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "OptiXAnaglyphCamera.h"

#include "OptiXCamera.h"
#include "OptiXContext.h"

#include <platform/core/common/CommonTypes.h>

#include <platform/engines/optix6/OptiX6Engine_generated_AnaglyphCamera.cu.ptx.h>
#include <platform/engines/optix6/OptiX6Engine_generated_Constantbg.cu.ptx.h>

const std::string PTX_PERSPECTIVE_CAMERA = OptiX6Engine_generated_AnaglyphCamera_cu_ptx;
const std::string PTX_MISS = OptiX6Engine_generated_Constantbg_cu_ptx;

namespace core
{
OptiXAnaglyphCamera::OptiXAnaglyphCamera()
: OptiXCameraProgram()
{
auto context = OptiXContext::get().getOptixContext();
_rayGenerationProgram = context->createProgramFromPTXString(PTX_PERSPECTIVE_CAMERA, CUDA_FUNC_PERSPECTIVE_CAMERA);
_missProgram = context->createProgramFromPTXString(PTX_MISS, OPTIX_CUDA_FUNCTION_CAMERA_ENVMAP_MISS);
_exceptionProgram = context->createProgramFromPTXString(PTX_PERSPECTIVE_CAMERA, OPTIX_CUDA_FUNCTION_EXCEPTION);
}

void OptiXAnaglyphCamera::commit(const OptiXCamera& camera, ::optix::Context context)
{
auto position = camera.getPosition();
const auto stereo = camera.getPropertyOrValue<bool>(CAMERA_PROPERTY_STEREO.name.c_str(), DEFAULT_CAMERA_STEREO);
const auto interpupillaryDistance =
camera.getPropertyOrValue<double>(CAMERA_PROPERTY_INTERPUPILLARY_DISTANCE.name.c_str(),
DEFAULT_CAMERA_INTERPUPILLARY_DISTANCE);
auto aspect =
camera.getPropertyOrValue<double>(CAMERA_PROPERTY_ASPECT_RATIO.name.c_str(), DEFAULT_CAMERA_ASPECT_RATIO);

if (stereo)
aspect *= 2.f;

const auto up = glm::rotate(camera.getOrientation(), UP_VECTOR);

Vector3d u, v, w;
float ulen, vlen, wlen;
w = camera.getTarget() - position;

wlen = glm::length(w);
u = normalize(glm::cross(w, up));
v = normalize(glm::cross(u, w));

vlen = wlen * tanf(0.5f *
camera.getPropertyOrValue<double>(CAMERA_PROPERTY_FIELD_OF_VIEW.name.c_str(),
DEFAULT_CAMERA_FIELD_OF_VIEW) *
M_PI / 180.f);
v *= vlen;
ulen = vlen * aspect;
u *= ulen;
const Vector3f ipd_offset = 0.5f * interpupillaryDistance * u;

context[CONTEXT_CAMERA_U]->setFloat(u.x, u.y, u.z);
context[CONTEXT_CAMERA_V]->setFloat(v.x, v.y, v.z);
context[CONTEXT_CAMERA_W]->setFloat(w.x, w.y, w.z);

context[CONTEXT_CAMERA_EYE]->setFloat(position.x, position.y, position.z);
context[CONTEXT_CAMERA_APERTURE_RADIUS]->setFloat(
camera.getPropertyOrValue<double>(CAMERA_PROPERTY_APERTURE_RADIUS.name.c_str(),
DEFAULT_CAMERA_APERTURE_RADIUS));
context[CONTEXT_CAMERA_FOCAL_DISTANCE]->setFloat(
camera.getPropertyOrValue<double>(CAMERA_PROPERTY_FOCAL_DISTANCE.name.c_str(), DEFAULT_CAMERA_FOCAL_DISTANCE));
context[CONTEXT_CAMERA_OFFSET]->setFloat(0, 0);

context[CONTEXT_CAMERA_STEREO]->setUint(stereo);
context[CONTEXT_CAMERA_IPD_OFFSET]->setFloat(ipd_offset.x, ipd_offset.y, ipd_offset.z);
}
} // namespace core
39 changes: 39 additions & 0 deletions platform/engines/optix6/OptiXAnaglyphCamera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2019, EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <memory>

#include <optixu/optixpp_namespace.h>

#include "OptiXCameraProgram.h"

namespace core
{
class OptiXAnaglyphCamera : public OptiXCameraProgram
{
public:
OptiXAnaglyphCamera();
~OptiXAnaglyphCamera() final = default;

void commit(const OptiXCamera& camera, ::optix::Context context) final;
};
} // namespace core
3 changes: 3 additions & 0 deletions platform/engines/optix6/OptiXContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ OptiXContext::OptiXContext()

OptiXContext::~OptiXContext()
{
RT_DESTROY_MAP(_bounds);
RT_DESTROY_MAP(_intersects);
RT_DESTROY_MAP(_optixTextureSamplers);
RT_DESTROY(_optixContext);
}

Expand Down
20 changes: 20 additions & 0 deletions platform/engines/optix6/OptiXEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <platform/core/parameters/ParametersManager.h>

#include "Logs.h"
#include "OptiXAnaglyphCamera.h"
#include "OptiXCamera.h"
#include "OptiXEngine.h"
#include "OptiXFrameBuffer.h"
Expand Down Expand Up @@ -91,6 +92,7 @@ void OptiXEngine::_createCameras()
{
PLUGIN_INFO("Registering '" << CAMERA_PROPERTY_TYPE_PERSPECTIVE << "' camera");
PropertyMap properties;
properties.setProperty(COMMON_PROPERTY_EXPOSURE);
properties.setProperty(CAMERA_PROPERTY_FIELD_OF_VIEW);
properties.setProperty(aspect);
properties.setProperty(CAMERA_PROPERTY_APERTURE_RADIUS);
Expand All @@ -108,6 +110,7 @@ void OptiXEngine::_createCameras()
{
PLUGIN_INFO("Registering '" << CAMERA_PROPERTY_TYPE_ORTHOGRAPHIC << "' camera");
PropertyMap properties;
properties.setProperty(COMMON_PROPERTY_EXPOSURE);
properties.setProperty(CAMERA_PROPERTY_HEIGHT);
properties.setProperty(aspect);
properties.setProperty(CAMERA_PROPERTY_ENABLE_CLIPPING_PLANES);
Expand All @@ -116,6 +119,23 @@ void OptiXEngine::_createCameras()
context.addCamera(CAMERA_PROPERTY_TYPE_ORTHOGRAPHIC, camera);
addCameraType(CAMERA_PROPERTY_TYPE_ORTHOGRAPHIC, properties);
}

{
PLUGIN_INFO("Registering '" << CAMERA_PROPERTY_TYPE_ANAGLYPH << "' camera");
PropertyMap properties;
properties.setProperty(COMMON_PROPERTY_EXPOSURE);
properties.setProperty(CAMERA_PROPERTY_FIELD_OF_VIEW);
properties.setProperty(aspect);
properties.setProperty(CAMERA_PROPERTY_APERTURE_RADIUS);
properties.setProperty(CAMERA_PROPERTY_FOCAL_DISTANCE);
properties.setProperty(CAMERA_PROPERTY_NEAR_CLIP);
properties.setProperty(CAMERA_PROPERTY_ENABLE_CLIPPING_PLANES);
properties.setProperty(CAMERA_PROPERTY_INTERPUPILLARY_DISTANCE);

auto camera = std::make_shared<OptiXAnaglyphCamera>();
context.addCamera(CAMERA_PROPERTY_TYPE_ANAGLYPH, camera);
addCameraType(CAMERA_PROPERTY_TYPE_ANAGLYPH, properties);
}
}

void OptiXEngine::_createRenderers()
Expand Down
6 changes: 0 additions & 6 deletions platform/engines/optix6/OptiXModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@

using namespace optix;

#define RT_DESTROY_MAP(__map) \
for (auto obj : __map) \
{ \
RT_DESTROY(obj.second); \
}

namespace core
{
template <typename T>
Expand Down
1 change: 0 additions & 1 deletion platform/engines/optix6/OptiXRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ void OptiXRenderer::commit()
const auto bounds = _scene->getBounds();
const auto epsilon = bounds.getSize().x / 1000.f;
context[CONTEXT_RENDERER_SCENE_EPSILON]->setFloat(epsilon);

context[CONTEXT_RENDERER_RADIANCE_RAY_TYPE]->setUint(0);
context[CONTEXT_RENDERER_SHADOW_RAY_TYPE]->setUint(1);
context[CONTEXT_RENDERER_AMBIENT_LIGHT_COLOR]->setFloat(bgColor.x, bgColor.y, bgColor.z);
Expand Down
28 changes: 18 additions & 10 deletions platform/engines/optix6/OptiXUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,24 @@ namespace core
{
#define BRAYNS_OPTIX_SAMPLE_NAME "braynsOptix7Engine"

#define RT_DESTROY(__object) \
try \
{ \
if (__object) \
__object->destroy(); \
} \
catch (...) \
{ \
} \
__object = nullptr;
#define RT_DESTROY(__object) \
{ \
try \
{ \
if (__object) \
__object->destroy(); \
} \
catch (...) \
{ \
} \
__object = nullptr; \
}

#define RT_DESTROY_MAP(__map) \
for (auto obj : __map) \
{ \
RT_DESTROY(obj.second); \
}

static void context_log_cb(unsigned int level, const char* tag, const char* message, void* /*cbdata */)
{
Expand Down
119 changes: 119 additions & 0 deletions platform/engines/optix6/cuda/camera/AnaglyphCamera.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2019, EPFL/Blue Brain Project
* All rights reserved. Do not distribute without permission.
*
* This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 3.0 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <platform/engines/optix6/cuda/Context.cuh>
#include <platform/engines/optix6/cuda/Helpers.cuh>
#include <platform/engines/optix6/cuda/Random.cuh>

using namespace optix;

__device__ float4 trace(const float3 ray_origin, const float3 offset, const float2 screen, const float2 subpixel_jitter)
{
float2 p = (make_float2(launch_index) + subpixel_jitter) / screen * 2.f - 1.f;
float3 origin = ray_origin + offset;

const float3 d = p.x * U + p.y * V + W;
const float fs = (focalDistance == 0.f ? 1.f : focalDistance);
const float dotD = dot(d, d);
const float denom = pow(dotD, 1.5f);
float3 ray_direction = normalize(d);
const float3 ray_target = origin + fs * ray_direction;

PerRayData_radiance prd;
prd.importance = 1.f;
prd.depth = 0;
prd.rayDdx = (dotD * U - dot(d, U) * d) / (denom * screen.x);
prd.rayDdy = (dotD * V - dot(d, V) * d) / (denom * screen.y);

if (apertureRadius > 0.f)
{
// Lens sampling
const float2 sample = optix::square_to_disk(make_float2(jitter4.z, jitter4.w));
origin = origin + apertureRadius * (sample.x * normalize(U) + sample.y * normalize(V));
ray_direction = normalize(ray_target - origin);
}

float near = sceneEpsilon;
float far = INFINITY;

// Clipping planes
if (enableClippingPlanes)
applyClippingPlanes(origin, ray_direction, near, far);

// Tracing
const optix::Ray ray(origin, ray_direction, radiance_ray_type, near, far);
rtTrace(top_object, ray, prd);

return prd.result;
}

// Pass 'seed' by reference to keep randomness state
__device__ float4 launch(uint& seed, const float2 screen, const bool use_randomness)
{
float3 ray_origin = eye;

// Subpixel jitter: send the ray through a different position inside the pixel each time, to provide antialiasing.
const float2 subpixel_jitter =
use_randomness ? make_float2(rnd(seed) - 0.5f, rnd(seed) - 0.5f) : make_float2(0.f, 0.f);

const float4 colorLeft = trace(ray_origin, -ipd_offset, screen, subpixel_jitter);
const float4 leftAnaglyphColor =
make_float4(colorLeft.x * 0.299f + colorLeft.y * 0.587f + colorLeft.z * 0.114f, 0.f, 0.f, colorLeft.w);

const float4 colorRight = trace(ray_origin, ipd_offset, screen, subpixel_jitter);
const float4 rightAnaglyphColor = make_float4(0.f, colorRight.y, colorRight.z, colorRight.w);

const float4 result = leftAnaglyphColor + rightAnaglyphColor;
return make_float4(make_float3(result) * mainExposure, result.w);
}

RT_PROGRAM void perspectiveCamera()
{
const size_t2 screen = output_buffer.size();
const float2 screen_f = make_float2(screen);

uint seed = tea<16>(screen.x * launch_index.y + launch_index.x, frame);

const int num_samples = max(1, samples_per_pixel);
const bool use_randomness = frame > 0 || num_samples > 1;
float4 result = make_float4(0.f);
for (int i = 0; i < num_samples; i++)
result += launch(seed, screen_f, use_randomness);
result /= num_samples;

float4 acc_val;
if (frame > 0)
{
acc_val = accum_buffer[launch_index];
acc_val = lerp(acc_val, result, 1.0f / static_cast<float>(frame + 1));
}
else
acc_val = result;

output_buffer[launch_index] = make_color(acc_val);

if (accum_buffer.size().x > 1 && accum_buffer.size().y > 1)
accum_buffer[launch_index] = acc_val;
}

RT_PROGRAM void exception()
{
output_buffer[launch_index] = make_color(bad_color);
}

0 comments on commit a82a552

Please sign in to comment.