-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:favreau/BioExplorer
- Loading branch information
Showing
10 changed files
with
295 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |