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

implement transform array extension in helide #244

Merged
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
30 changes: 30 additions & 0 deletions code_gen/api/khr_instance_transform_array.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"info" : {
"name" : "KHR_INSTANCE_TRANSFORM_ARRAY",
"type" : "extension",
"dependencies" : ["khr_instance_transform"]
},

"objects" : [
{
"type" : "ANARI_INSTANCE",
"name" : "transform",
"description" : "instance object",
"parameters" : [
{
"name" : "transform",
"types" : ["ANARI_ARRAY1D"],
"elementType" : ["ANARI_FLOAT32_MAT4"],
"tags" : [],
"description" : "array of transforms applied to objects in the instance"
}, {
"name" : "id",
"types" : ["ANARI_ARRAY1D"],
"elementType" : ["ANARI_UINT32"],
"tags" : [],
"description" : "per-transform user id for instanceId channel"
}
]
}
]
}
37 changes: 24 additions & 13 deletions src/helide/HelideDefinitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"dependencies": [
"anari_core_1_0",
"anari_core_objects_base_1_0",
"khr_instance_transform",
"khr_camera_orthographic",
"khr_camera_perspective",
"khr_device_synchronization",
Expand All @@ -19,6 +18,8 @@
"khr_geometry_quad",
"khr_geometry_sphere",
"khr_geometry_triangle",
"khr_instance_transform",
"khr_instance_transform_array",
"khr_material_matte",
"khr_material_physicallyBased",
"khr_renderer_ambient_light",
Expand All @@ -39,18 +40,14 @@
"parameters": [
{
"name": "allowInvalidMaterials",
"types": [
"ANARI_BOOL"
],
"types": ["ANARI_BOOL"],
"tags": [],
"default": true,
"description": "show surfaces with invalid materials"
},
{
"name": "invalidMaterialColor",
"types": [
"ANARI_FLOAT32_VEC4"
],
"types": ["ANARI_FLOAT32_VEC4"],
"tags": [],
"default": [
1.0,
Expand All @@ -68,9 +65,7 @@
"parameters": [
{
"name": "mode",
"types": [
"ANARI_STRING"
],
"types": ["ANARI_STRING"],
"tags": [],
"default": "default",
"values": [
Expand All @@ -95,14 +90,30 @@
},
{
"name": "eyeLightBlendRatio",
"types": [
"ANARI_FLOAT32"
],
"types": ["ANARI_FLOAT32"],
"tags": [],
"default": 0.5,
"minimum": 0.0,
"maximum": 1.0,
"description": "how much eye lighting factor should be used in default render mode"
},
{
"name": "taskGrainSizeWidth",
"types": ["ANARI_INT32"],
"tags": [],
"default": 4,
"minimum": 1,
"maximum": 128,
"description": "grain size of the render task along the width of the frame"
},
{
"name": "taskGrainSizeHeight",
"types": ["ANARI_INT32"],
"tags": [],
"default": 4,
"minimum": 1,
"maximum": 128,
"description": "grain size of the render task along the height of the frame"
}
]
}
Expand Down
643 changes: 365 additions & 278 deletions src/helide/HelideDeviceQueries.cpp

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/helide/HelideDeviceQueries.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace helide {
#define ANARI_INFO_parameter 9
#define ANARI_INFO_channel 10
#define ANARI_INFO_use 11
const int extension_count = 25;
const int extension_count = 26;
const char ** query_extensions();
const char ** query_object_types(ANARIDataType type);
const ANARIParameter * query_params(ANARIDataType type, const char *subtype);
Expand Down
2 changes: 1 addition & 1 deletion src/helide/HelideGlobalState.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// helium
#include "helium/BaseGlobalDeviceState.h"
// embree
#include "embree3/rtcore.h"
#include "embree4/rtcore.h"

namespace helide {

Expand Down
16 changes: 9 additions & 7 deletions src/helide/HelideMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// helium
#include "helium/helium_math.h"
// embree
#include <embree3/rtcore_common.h>
#include <embree4/rtcore_common.h>
// std
#include <array>
#include <optional>
Expand All @@ -25,18 +25,19 @@ struct Ray
float3 dir;
float time{0.f};
float tfar{std::numeric_limits<float>::max()};
unsigned int mask{~0u};
unsigned int id{0};
unsigned int flags{0};
uint32_t mask{~0u};
uint32_t id{0u};
uint32_t flags{0u};

// Hit //

float3 Ng;
float u;
float v;
unsigned int primID{RTC_INVALID_GEOMETRY_ID}; // primitive ID
unsigned int geomID{RTC_INVALID_GEOMETRY_ID}; // geometry ID
unsigned int instID{RTC_INVALID_GEOMETRY_ID}; // instance ID
uint32_t primID{RTC_INVALID_GEOMETRY_ID}; // primitive ID
uint32_t geomID{RTC_INVALID_GEOMETRY_ID}; // geometry ID
uint32_t instID{RTC_INVALID_GEOMETRY_ID}; // instance ID
uint32_t instArrayID{RTC_INVALID_GEOMETRY_ID}; // instance sub-array ID
};

struct Volume;
Expand All @@ -47,6 +48,7 @@ struct VolumeRay
box1 t{0.f, std::numeric_limits<float>::max()};
Volume *volume{nullptr};
uint32_t instID{RTC_INVALID_GEOMETRY_ID};
uint32_t instArrayID{RTC_INVALID_GEOMETRY_ID};
};

using UniformAttributeSet = std::array<std::optional<float4>, 5>;
Expand Down
34 changes: 19 additions & 15 deletions src/helide/external/embree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

project(local_embree LANGUAGES CXX)

option(EMBREE_GEOMETRY_CURVE "" ON)
option(EMBREE_GEOMETRY_GRID "" OFF)
option(EMBREE_GEOMETRY_INSTANCE "" ON)
option(EMBREE_GEOMETRY_POINT "" ON)
option(EMBREE_GEOMETRY_QUAD "" ON)
option(EMBREE_GEOMETRY_SUBDIVISION "" OFF)
option(EMBREE_GEOMETRY_TRIANGLE "" ON)
option(EMBREE_GEOMETRY_USER "" OFF)
if (APPLE AND ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
option(EMBREE_GEOMETRY_CURVE "" ON)
option(EMBREE_GEOMETRY_GRID "" OFF)
option(EMBREE_GEOMETRY_INSTANCE "" ON)
option(EMBREE_GEOMETRY_INSTANCE_ARRAY "" ON)
option(EMBREE_GEOMETRY_POINT "" ON)
option(EMBREE_GEOMETRY_QUAD "" ON)
option(EMBREE_GEOMETRY_SUBDIVISION "" OFF)
option(EMBREE_GEOMETRY_TRIANGLE "" ON)
option(EMBREE_GEOMETRY_USER "" OFF)
if ((APPLE OR UNIX) AND ${CMAKE_SYSTEM_PROCESSOR} STREQUAL "arm64")
option(EMBREE_ISA_NEON "" OFF)
option(EMBREE_ISA_NEON2X "" ON)
else()
Expand All @@ -21,16 +22,19 @@ else()
option(EMBREE_ISA_SSE42 "" ON)
option(EMBREE_ISA_SSE2 "" OFF)
endif()
option(EMBREE_ISPC_SUPPORT "" OFF)
option(EMBREE_STATIC_LIB "" ON)
option(EMBREE_TUTORIALS "" OFF)
option(EMBREE_TUTORIALS_GLFW "" OFF)
option(EMBREE_ISPC_SUPPORT "" OFF)
option(EMBREE_STATIC_LIB "" ON)
option(EMBREE_STAT_COUNTERS "" OFF)
option(EMBREE_SYCL_SUPPORT "" OFF)
option(EMBREE_TUTORIALS "" OFF)
option(EMBREE_TUTORIALS_GLFW "" OFF)
set(EMBREE_TASKING_SYSTEM "INTERNAL" CACHE STRING "" FORCE)
set(EMBREE_MAX_INSTANCE_LEVEL_COUNT "1" CACHE STRING "" FORCE)

anari_sdk_fetch_project(
NAME anari_helide_embree
URL "https://github.com/embree/embree/archive/refs/tags/v3.13.5.zip"
MD5 d87ce56557a38e332cb3e0fa5cac8cfd
URL "https://github.com/RenderKit/embree/archive/refs/tags/v4.3.3.zip"
MD5 a03f49d17084612dc0f28bdc36d92e89
ADD_SUBDIR
)

Expand Down
29 changes: 20 additions & 9 deletions src/helide/frame/Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ static void serial_for(I size, FUNC &&f)
template <typename R, typename TASK_T>
static std::future<R> async(std::packaged_task<R()> &task, TASK_T &&fcn)
{
#if 1
return std::async(fcn);
#else
task = std::packaged_task<R()>(std::forward<TASK_T>(fcn));
auto future = task.get_future();
embree::TaskScheduler::spawn([&]() { task(); });
return future;
#endif
}

template <typename R>
Expand Down Expand Up @@ -154,16 +158,23 @@ void Frame::renderFrame()

m_world->embreeSceneUpdate();

const auto taskGrainSize = uint2(m_renderer->taskGrainSize());

const auto &size = m_frameData.size;
embree::parallel_for(size.y, [&](int y) {
serial_for(size.x, [&](int x) {
auto screen = screenFromPixel(float2(x, y));
auto imageRegion = m_camera->imageRegion();
screen.x = linalg::lerp(imageRegion.x, imageRegion.z, screen.x);
screen.y = linalg::lerp(imageRegion.y, imageRegion.w, screen.y);
Ray ray = m_camera->createRay(screen);
writeSample(x, y, m_renderer->renderSample(screen, ray, *m_world));
});
using Range = embree::range<uint32_t>;
embree::parallel_for(0u, size.y, taskGrainSize.y, [&](const Range &ry) {
for (auto y = ry.begin(); y < ry.end(); y++) {
embree::parallel_for(0u, size.x, taskGrainSize.x, [&](const Range &rx) {
for (auto x = rx.begin(); x < rx.end(); x++) {
auto screen = screenFromPixel(float2(x, y));
auto imageRegion = m_camera->imageRegion();
screen.x = linalg::lerp(imageRegion.x, imageRegion.z, screen.x);
screen.y = linalg::lerp(imageRegion.y, imageRegion.w, screen.y);
Ray ray = m_camera->createRay(screen);
writeSample(x, y, m_renderer->renderSample(screen, ray, *m_world));
}
});
}
});

if (m_callback)
Expand Down
26 changes: 14 additions & 12 deletions src/helide/renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ void Renderer::commit()
m_ambientRadiance = getParam<float>("ambientRadiance", 1.f);
m_falloffBlendRatio = getParam<float>("eyeLightBlendRatio", 0.5f);
m_mode = renderModeFromString(getParamString("mode", "default"));
m_taskGrainSize.x = getParam<int32_t>("taskGrainSizeWidth", 4);
m_taskGrainSize.y = getParam<int32_t>("taskGrainSizeHeight", 4);
}

PixelSample Renderer::renderSample(
Expand All @@ -118,9 +120,9 @@ PixelSample Renderer::renderSample(

// Intersect Surfaces //

RTCIntersectContext context;
rtcInitIntersectContext(&context);
rtcIntersect1(w.embreeScene(), &context, (RTCRayHit *)&ray);
RTCIntersectArguments iargs;
rtcInitIntersectArguments(&iargs);
rtcIntersect1(w.embreeScene(), (RTCRayHit *)&ray, &iargs);
const bool hitGeometry = ray.geomID != RTC_INVALID_GEOMETRY_ID;

// Intersect Volumes //
Expand All @@ -139,8 +141,8 @@ PixelSample Renderer::renderSample(
if (hitGeometry || hitVolume) {
retval.primId = hitVolume ? 0 : ray.primID;
retval.objId = hitVolume ? vray.volume->id() : w.surfaceFromRay(ray)->id();
retval.instId = hitVolume ? w.instanceFromRay(vray)->id()
: w.instanceFromRay(ray)->id();
retval.instId = hitVolume ? w.instanceFromRay(vray)->id(vray.instArrayID)
: w.instanceFromRay(ray)->id(ray.instArrayID);
}

return retval;
Expand Down Expand Up @@ -237,10 +239,10 @@ float4 Renderer::shadeRay(const float2 &screen,
const auto n = linalg::mul(inst->xfmInvRot(), ray.Ng);
const auto falloff =
std::abs(linalg::dot(-ray.dir, linalg::normalize(n)));
const float4 sc =
surface->getSurfaceColor(ray, inst->getUniformAttributes());
const float so =
surface->getSurfaceOpacity(ray, inst->getUniformAttributes());
const float4 sc = surface->getSurfaceColor(
ray, inst->getUniformAttributes(ray.instArrayID));
const float so = surface->getSurfaceOpacity(
ray, inst->getUniformAttributes(ray.instArrayID));
const float o = surface->adjustedAlpha(std::clamp(sc.w * so, 0.f, 1.f));
const float3 c = m_heatmap->valueAtLinear<float3>(o);
const float3 fc = c * falloff;
Expand All @@ -253,11 +255,11 @@ float4 Renderer::shadeRay(const float2 &screen,
const Instance *inst = w.instanceFromRay(ray);
const Surface *surface = w.surfaceFromRay(ray);

const auto n = linalg::mul(inst->xfmInvRot(), ray.Ng);
const auto n = linalg::mul(inst->xfmInvRot(ray.instArrayID), ray.Ng);
const auto falloff =
std::abs(linalg::dot(-ray.dir, linalg::normalize(n)));
const float4 c =
surface->getSurfaceColor(ray, inst->getUniformAttributes());
const float4 c = surface->getSurfaceColor(
ray, inst->getUniformAttributes(ray.instArrayID));
const float3 sc = float3(c.x, c.y, c.z) * std::clamp(falloff, 0.f, 1.f);
geometryColor =
((m_falloffBlendRatio * sc)
Expand Down
10 changes: 10 additions & 0 deletions src/helide/renderer/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct Renderer : public Object

virtual void commit() override;

int2 taskGrainSize() const;

PixelSample renderSample(const float2 &screen, Ray ray, const World &w) const;

static Renderer *createInstance(
Expand All @@ -64,11 +66,19 @@ struct Renderer : public Object
float m_ambientRadiance{1.f};
float m_falloffBlendRatio{0.5f};
RenderMode m_mode{RenderMode::DEFAULT};
int2 m_taskGrainSize{4, 4};

helium::IntrusivePtr<Array1D> m_heatmap;
helium::IntrusivePtr<Array2D> m_bgImage;
};

// Inlined definitions ////////////////////////////////////////////////////////

inline int2 Renderer::taskGrainSize() const
{
return m_taskGrainSize;
}

} // namespace helide

HELIDE_ANARI_TYPEFOR_SPECIALIZATION(helide::Renderer *, ANARI_RENDERER);
Loading
Loading