Skip to content

Commit ac64f6c

Browse files
committed
Construction of Embree acceleration structure
1 parent 786cf4e commit ac64f6c

File tree

9 files changed

+101
-3
lines changed

9 files changed

+101
-3
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,9 @@
8888
"bitset": "cpp",
8989
"regex": "cpp",
9090
"typeindex": "cpp"
91+
},
92+
"cmake.buildEnvironment": {
93+
"embree_DIR": "C:/embree-4.3.1.x64.windows/lib/cmake/embree-4.3.1/",
94+
"TBB_DIR": "C:/Program Files (x86)/Intel/oneAPI/tbb/2021.12/lib/cmake/tbb/"
9195
}
9296
}

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ else()
1616
set(REFERENCE_MODE ON)
1717
endif()
1818

19+
set(embree_DIR "C:/embree-4.3.1/lib/cmake/embree-4.3.1/") # Replace with directory to local Embree installation
20+
find_package(embree REQUIRED)
1921
find_package(OpenGL REQUIRED)
2022
find_package(OpenMP REQUIRED)
2123

@@ -24,7 +26,7 @@ enable_sanitizers(CGFinProjLib)
2426
set_project_warnings(CGFinProjLib)
2527
include(${CMAKE_CURRENT_LIST_DIR}/src/CMakeLists.txt)
2628
target_include_directories(CGFinProjLib INTERFACE "${CMAKE_CURRENT_LIST_DIR}/src/")
27-
target_link_libraries(CGFinProjLib INTERFACE CGFramework OpenGL::GLU OpenMP::OpenMP_CXX)
29+
target_link_libraries(CGFinProjLib INTERFACE CGFramework OpenGL::GLU OpenMP::OpenMP_CXX embree)
2830
target_compile_features(CGFinProjLib INTERFACE cxx_std_20)
2931

3032
# Main executable config

framework/third_party/eigen-3.4.0/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ if(BUILD_TESTING)
583583
message(STATUS " | http://eigen.tuxfamily.org/index.php?title=Tests")
584584
endif()
585585
message(STATUS "blas | Build BLAS library (not the same thing as Eigen)")
586-
message(STATUS "eigen_uninstall | Remove files installed by the install target")
586+
message(STATUS "uninstall| Remove files installed by the install target")
587587
message(STATUS "---------+--------------------------------------------------------------")
588588
message(STATUS "")
589589

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ target_sources(CGFinProjLib
44

55
"${CMAKE_CURRENT_LIST_DIR}/ray_tracing/bounding_volume_hierarchy.cpp"
66
"${CMAKE_CURRENT_LIST_DIR}/ray_tracing/bvh_interface.cpp"
7+
"${CMAKE_CURRENT_LIST_DIR}/ray_tracing/embree_interface.cpp"
78
"${CMAKE_CURRENT_LIST_DIR}/ray_tracing/interpolate.cpp"
89
"${CMAKE_CURRENT_LIST_DIR}/ray_tracing/intersect.cpp"
910

src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
#include <scene/light.h>
1+
#include <ray_tracing/embree_interface.h>
22
#include <rendering/render.h>
33
#include <rendering/reservoir.h>
44
#include <rendering/screen.h>
5+
#include <scene/light.h>
56
#include <ui/draw.h>
67
#include <ui/ui.h>
78
#include <utils/config.h>
@@ -60,6 +61,7 @@ int main(int argc, char** argv) {
6061
SceneType sceneType = SceneType::CornellNightClub;
6162
std::optional<Ray> optDebugRay;
6263
Scene scene = loadScenePrebuilt(sceneType, config.dataPath);
64+
EmbreeInterface embreeInterface(scene);
6365
BvhInterface bvh(&scene);
6466
std::shared_ptr<ReservoirGrid> previousFrameGrid;
6567

src/ray_tracing/embree_interface.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "embree_interface.h"
2+
3+
#include <utils/utils.h>
4+
5+
#include <format>
6+
#include <stdexcept>
7+
8+
EmbreeInterface::EmbreeInterface(const Scene& scene) {
9+
// Create device
10+
m_device = rtcNewDevice(NULL);
11+
if (!m_device) { std::runtime_error(std::format("Error {}: Cannot create device ", magic_enum::enum_name(rtcGetDeviceError(NULL)))); }
12+
rtcSetDeviceErrorFunction(m_device, errorFunction, NULL);
13+
14+
// Load all geometry to Embree scene
15+
m_scene = rtcNewScene(m_device);
16+
for (const Mesh& mesh : scene.meshes) {
17+
// Create and populate buffers to house vertex and triangle data
18+
RTCGeometry geom = rtcNewGeometry(m_device, RTC_GEOMETRY_TYPE_TRIANGLE);
19+
rtcSetGeometryVertexAttributeCount(geom, 1);
20+
glm::vec3* positions = (glm::vec3*) rtcSetNewGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(glm::vec3), mesh.vertices.size());
21+
glm::vec2* texCoords = (glm::vec2*) rtcSetNewGeometryBuffer(geom, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(glm::vec2), mesh.vertices.size());
22+
glm::uvec3* indices = (glm::uvec3*) rtcSetNewGeometryBuffer(geom, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(glm::uvec3), mesh.triangles.size());
23+
populateVertexDataBuffers(positions, texCoords, mesh.vertices);
24+
populateIndexBuffer(indices, mesh.triangles);
25+
26+
// Commit geometry and store its ID for material info lookup
27+
rtcCommitGeometry(geom);
28+
uint32_t geomId = rtcAttachGeometry(m_scene, geom);
29+
m_meshToMaterial[geomId] = mesh.material;
30+
rtcReleaseGeometry(geom);
31+
}
32+
rtcCommitScene(m_scene);
33+
}
34+
35+
EmbreeInterface::~EmbreeInterface() {
36+
rtcReleaseScene(m_scene);
37+
rtcReleaseDevice(m_device);
38+
}
39+
40+
void EmbreeInterface::populateVertexDataBuffers(glm::vec3* positionBuffer, glm::vec2* texCoordBuffer,
41+
const std::vector<Vertex>& vertices) {
42+
for (size_t vertexIdx = 0ULL; vertexIdx < vertices.size(); vertexIdx++) {
43+
positionBuffer[vertexIdx] = vertices[vertexIdx].position;
44+
texCoordBuffer[vertexIdx] = vertices[vertexIdx].texCoord;
45+
}
46+
}
47+
48+
void EmbreeInterface::populateIndexBuffer(glm::uvec3* indexBuffer, const std::vector<glm::uvec3>& indices) {
49+
for (size_t triangleIdx = 0ULL; triangleIdx < indices.size(); triangleIdx++) {
50+
indexBuffer[triangleIdx] = indices[triangleIdx];
51+
}
52+
}

src/ray_tracing/embree_interface.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
#ifndef _EMBREE_INTERFACE_H_
3+
#define _EMBREE_INTERFACE_H_
4+
5+
#include <embree4/rtcore.h>
6+
7+
#include <scene/scene.h>
8+
9+
#include <unordered_map>
10+
11+
12+
class EmbreeInterface {
13+
public:
14+
EmbreeInterface(const Scene& scene);
15+
~EmbreeInterface();
16+
17+
private:
18+
void populateVertexDataBuffers(glm::vec3* positionBuffer, glm::vec2* texCoordBuffer,
19+
const std::vector<Vertex>& vertices);
20+
void populateIndexBuffer(glm::uvec3* indexBuffer, const std::vector<glm::uvec3>& indices);
21+
22+
RTCDevice m_device;
23+
RTCScene m_scene;
24+
std::unordered_map<uint32_t, Material> m_meshToMaterial;
25+
};
26+
27+
28+
#endif // _EMBREE_INTERFACE_H_

src/utils/utils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ DISABLE_WARNINGS_PUSH()
33
#include <glm/gtc/type_ptr.hpp>
44
DISABLE_WARNINGS_POP()
55

6+
#include <embree4/rtcore.h>
7+
68
#include <framework/opengl_includes.h>
79

810
#include <scene/texture.h>
@@ -54,6 +56,10 @@ bool testVisibilityLightSample(const glm::vec3& samplePos, const BvhInterface& b
5456
return false;
5557
}
5658

59+
void errorFunction(void* userPtr, enum RTCError error, const char* str) {
60+
std::cout << std::format("[EMBREE] {}: {}", magic_enum::enum_name(error), str) << std::endl;
61+
}
62+
5763
void setOpenGLMatrices(const Trackball& camera) {
5864
// Load view matrix.
5965
glMatrixMode(GL_MODELVIEW);

src/utils/utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ inline bool inRangeInclusive(T val, T low, T high) { return low <= val && val <=
2929
glm::vec3 diffuseAlbedo(const HitInfo& hitInfo, const Features& features);
3030
bool testVisibilityLightSample(const glm::vec3& samplePos, const BvhInterface& bvh, const Features& features, Ray ray, HitInfo hitInfo);
3131

32+
// Embree
33+
void errorFunction(void* userPtr, enum RTCError error, const char* str);
34+
3235
// Framework
3336
void setOpenGLMatrices(const Trackball& camera);
3437

0 commit comments

Comments
 (0)