Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BRAYNS-638 Refactor engine v2. #1264

Merged
merged 7 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions src/brayns/core/enginev2/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,48 @@

#include "Camera.h"

namespace brayns::experimental
{
void Camera::setTransform(const Affine3 &transform)
namespace
{
setParam("transform", transform);
}
using namespace brayns::experimental;

void Camera::setNearClip(float distance)
void setCameraParams(OSPCamera handle, const CameraSettings &settings)
{
setParam("nearClip", distance);
setObjectParam(handle, "position", settings.position);
setObjectParam(handle, "direction", settings.direction);
setObjectParam(handle, "up", settings.up);
setObjectParam(handle, "nearClip", settings.nearClippingDistance);
}

void PerspectiveCamera::setFovy(float degrees)
{
setParam("fovy", degrees);
}

void PerspectiveCamera::setAspectRatio(float aspect)
namespace brayns::experimental
{
setParam("aspect", aspect);
}

void OrthographicCamera::setHeight(float height)
OSPCamera ObjectReflector<PerspectiveCamera>::createHandle(OSPDevice device, const Settings &settings)
{
setParam("height", height);
auto handle = ospNewCamera("perspective");
throwLastDeviceErrorIfNull(device, handle);

setCameraParams(handle, settings.base);

setObjectParam(handle, "fovy", settings.fovy);
setObjectParam(handle, "aspect", settings.aspectRatio);

commitObject(handle);

return handle;
}

void OrthographicCamera::setAspectRatio(float aspect)
OSPCamera ObjectReflector<OrthographicCamera>::createHandle(OSPDevice device, const Settings &settings)
{
setParam("aspect", aspect);
auto handle = ospNewCamera("perspective");
throwLastDeviceErrorIfNull(device, handle);

setCameraParams(handle, settings.base);

setObjectParam(handle, "height", settings.height);
setObjectParam(handle, "aspect", settings.aspectRatio);

commitObject(handle);

return handle;
}
}
52 changes: 35 additions & 17 deletions src/brayns/core/enginev2/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,45 +21,63 @@

#pragma once

#include "Managed.h"
#include "Object.h"

namespace brayns::experimental
{
struct CameraSettings
{
Vector3 position = {0.0F, 0.0F, 0.0F};
Vector3 direction = {0.0F, 0.0F, 1.0F};
Vector3 up = {0.0F, 1.0F, 0.0F};
float nearClippingDistance = 1.0e-6F;
};

class Camera : public Managed<OSPCamera>
{
public:
using Managed::Managed;
};

void setTransform(const Affine3 &transform);
void setNearClip(float distance);
struct PerspectiveCameraSettings
{
CameraSettings base = {};
float fovy = 60.0F;
float aspectRatio = 1.0F;
};

class PerspectiveCamera : public Camera
{
public:
static inline const std::string name = "perspective";

using Camera::Camera;
};

template<>
struct ObjectReflector<PerspectiveCamera>
{
using Settings = PerspectiveCameraSettings;

void setFovy(float degrees);
void setAspectRatio(float aspect);
static OSPCamera createHandle(OSPDevice device, const Settings &settings);
};

struct OrthographicCameraSettings
{
CameraSettings base = {};
float height = 1.0F;
float aspectRatio = 1.0F;
};

class OrthographicCamera : public Camera
{
public:
static inline const std::string name = "orthographic";

using Camera::Camera;

void setHeight(float height);
void setAspectRatio(float aspect);
};
}

namespace ospray
template<>
struct ObjectReflector<OrthographicCamera>
{
OSPTYPEFOR_SPECIALIZATION(brayns::experimental::Camera, OSP_CAMERA)
OSPTYPEFOR_SPECIALIZATION(brayns::experimental::PerspectiveCamera, OSP_CAMERA)
OSPTYPEFOR_SPECIALIZATION(brayns::experimental::OrthographicCamera, OSP_CAMERA)
using Settings = OrthographicCameraSettings;

static OSPCamera createHandle(OSPDevice device, const Settings &settings);
};
}
36 changes: 30 additions & 6 deletions src/brayns/core/enginev2/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,40 @@

#include <span>

#include <ospray/ospray_cpp.h>
#include "Object.h"

namespace brayns::experimental
{
template<typename T>
using SharedArray = std::span<T>;
class Data : public Managed<OSPData>
{
public:
using Managed::Managed;
};

template<OsprayDataType T>
Data toSharedData(std::span<T> items)
{
auto data = items.data();
auto type = ospray::OSPTypeFor<T>::value;
auto size = items.size();
auto handle = ospNewSharedData(data, type, size);
return Data(handle);
}

template<OsprayDataType T>
void setObjectData(OSPObject handle, const char *id, std::span<T> items)
{
setObjectParam(handle, id, toSharedData(items));
}

template<typename T>
ospray::cpp::SharedData toSharedData(SharedArray<T> data)
template<OsprayDataType T>
void setObjectDataIfNotEmpty(OSPObject handle, const char *id, std::span<T> items)
{
return ospray::cpp::SharedData(data.data(), data.size());
if (items.empty())
{
removeObjectParam(handle, id);
return;
}
setObjectData(handle, id, items);
}
}
89 changes: 50 additions & 39 deletions src/brayns/core/enginev2/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,62 +28,73 @@ Device::Device(OSPDevice handle):
{
}

GeometricModel Device::createGeometricModel()
OSPDevice Device::getHandle() const
{
auto handle = ospNewGeometricModel();
return GeometricModel(handle);
return _handle.get();
}

VolumetricModel Device::createVolumetricModel()
Future Device::render(const Context &context)
{
auto handle = ospNewVolumetricModel();
return VolumetricModel(handle);
return startRendering(_handle.get(), context);
}

Group Device::createGroup()
std::optional<PickResult> Device::pick(const PickSettings &settings)
{
auto handle = ospNewGroup();
return Group(handle);
return tryPick(_handle.get(), settings);
}

Instance Device::createInstance()
void Device::Deleter::operator()(OSPDevice device) const
{
auto handle = ospNewInstance();
return Instance(handle);
ospDeviceRelease(device);
ospShutdown();
}

World Device::createWorld()
Device createDevice(Logger &logger)
{
auto handle = ospNewWorld();
return World(handle);
}
auto error = ospLoadModule("cpu");

Framebuffer Device::createFramebuffer(const FramebufferSettings &settings)
{
auto width = static_cast<int>(settings.width);
auto height = static_cast<int>(settings.height);
auto format = static_cast<OSPFrameBufferFormat>(settings.format);
auto channels = static_cast<std::uint32_t>(OSP_FB_NONE);
for (auto channel : settings.channels)
if (error != OSP_NO_ERROR)
{
channels |= static_cast<OSPFrameBufferChannel>(channel);
auto message = fmt::format("Failed to load OSPRay CPU module (code = {})", static_cast<int>(error));
throw std::runtime_error(message);
}
auto handle = ospNewFrameBuffer(width, height, format, channels);
return Framebuffer(handle);
}

RenderTask Device::render(const RenderSettings &settings)
{
auto framebuffer = settings.framebuffer.getHandle();
auto renderer = settings.renderer.getHandle();
auto camera = settings.camera.getHandle();
auto world = settings.world.getHandle();
auto future = ospRenderFrame(framebuffer, renderer, camera, world);
return RenderTask(future);
}
auto currentDevice = ospGetCurrentDevice();
if (currentDevice != nullptr)
{
throw std::invalid_argument("OSPRay only accepts one device created at a time");
}

void Device::Deleter::operator()(OSPDevice device) const
{
ospDeviceRelease(device);
auto device = ospNewDevice();

if (device == nullptr)
{
throw std::runtime_error("Failed to create device");
}

auto logLevel = OSP_LOG_DEBUG;
ospDeviceSetParam(device, "logLevel", OSP_UINT, &logLevel);

auto warnAsError = true;
ospDeviceSetParam(device, "warnAsError", OSP_BOOL, &warnAsError);

auto errorCallback = [](auto *userData, auto code, const auto *message)
{
auto &logger = *static_cast<Logger *>(userData);
logger.error("Device error (code = {}): {}", static_cast<int>(code), message);
};
ospDeviceSetErrorCallback(device, errorCallback, &logger);

auto statusCallback = [](auto *userData, const auto *message)
{
auto &logger = *static_cast<Logger *>(userData);
logger.debug("Device status: {}", message);
};
ospDeviceSetStatusCallback(device, statusCallback, &logger);

ospDeviceCommit(device);
ospSetCurrentDevice(device);

return Device(device);
}
}
Loading