Skip to content
This repository has been archived by the owner on Mar 19, 2020. It is now read-only.

Commit

Permalink
fix bug in sahbvh
Browse files Browse the repository at this point in the history
  • Loading branch information
椎名深雪 committed Dec 29, 2019
1 parent a4c8fd8 commit 54e683a
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 148 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ project(MiyukiRenderer)

option(USE_EMBREE "Use Embree as ray intersection backend" ON)
option(USE_OIDN "Use OIDN as denoiser" ON)
option(EMBREE_FROM_SOUCE "Download and compile embree from souce" OFF)
option(EMBREE_FROM_SOURCE "Download and compile embree from source" OFF)

set(CMAKE_CXX_STANDARD 17)

if(MSVC)
if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qpar /arch:AVX2 /std:c++17 /GL")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX2 /std:c++17 /GL")
endif()
endif()
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
Expand Down Expand Up @@ -61,7 +61,7 @@ file(GLOB libcoreSource src/core/*.cpp

IF(USE_EMBREE)
add_compile_definitions(MYK_USE_EMBREE)
IF(EMBREE_FROM_SOUCE)
IF(EMBREE_FROM_SOURCE)
include(ExternalProject)
include(${PROJECT_SOURCE_DIR}/cmake/cmake-embree)
include_directories(${EMBREE_INCLUDE_DIRS})
Expand Down
66 changes: 33 additions & 33 deletions cmake/cmake-embree
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ ExternalProject_Add(
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DEMBREE_TUTORIALS=OFF"
"-DEMBREE_FILTER_FUNCTION=OFF"
"-DEMBREE_RAY_PACKETS=OFF"
"-DEMBREE_RAY_PACKETS=ON"
"-DEMBREE_TASKING_SYSTEM=INTERNAL"
"-DEMBREE_GEOMETRY_QUAD=OFF"
"-DEMBREE_GEOMETRY_CURVE=OFF"
"-DEMBREE_GEOMETRY_SUBDIVISION=OFF"
"-DEMBREE_GEOMETRY_INSTANCE=OFF"
"-DEMBREE_GEOMETRY_INSTANCE=ON"
"-DEMBREE_GEOMETRY_USER=OFF"
"-DEMBREE_GEOMETRY_POINT=OFF"
"-DEMBREE_ISPC_SUPPORT=OFF"
"-DEMBREE_ISPC_SUPPORT=ON"
"-DEMBREE_STATIC_LIB=ON"
"-DEMBREE_MAX_ISA=${EMBREE_MAX_ISA}"
"${EMBREE_IGNORE_CMAKE_CXX_FLAGS}"
Expand All @@ -41,39 +41,39 @@ ExternalProject_Add(
INSTALL_COMMAND ""
)

set(EMBREE_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/external/embree3//src/Embree3/include/)
set(EMBREE_INCLUDE_DIRS ${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3/include/)

IF(WIN32)
SET(EMBREE_LIBRARIES
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/embree3.lib"
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/embree_sse42.lib"
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/embree_avx.lib"
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/embree_avx2.lib"
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/lexers.lib"
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/math.lib"
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/simd.lib"
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/tasking.lib"
optimized "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Release/sys.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/embree3.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/embree_sse42.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/embree_avx.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/embree_avx2.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/lexers.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/math.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/simd.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/tasking.lib"
debug "${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/Debug/sys.lib"
SET(EMBREE_LIBRARY
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/embree3.lib"
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/embree_sse42.lib"
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/embree_avx.lib"
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/embree_avx2.lib"
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/lexers.lib"
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/math.lib"
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/simd.lib"
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/tasking.lib"
optimized "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/sys.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/embree3.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/embree_sse42.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/embree_avx.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/embree_avx2.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/lexers.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/math.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/simd.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/tasking.lib"
debug "${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/sys.lib"
)
ELSE()
SET(EMBREE_LIBRARIES
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/libembree3.a"
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/libembree_sse42.a"
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/libembree_avx.a"
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/libembree_avx2.a"
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/liblexers.a"
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/libmath.a"
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/libsimd.a"
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/libtasking.a"
"${PROJECT_SOURCE_DIR}/lib/embree3/src/Embree3-build/libsys.a"
SET(EMBREE_LIBRARY
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/libembree3.a"
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/libembree_sse42.a"
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/libembree_avx.a"
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/libembree_avx2.a"
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/liblexers.a"
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/libmath.a"
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/libsimd.a"
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/libtasking.a"
"${PROJECT_SOURCE_DIR}/external/embree3/src/Embree3-build/libsys.a"
)
ENDIF()
Binary file modified data/breakfast_room/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified data/breakfast_room/out.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion data/breakfast_room/scene.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"integrator": {
"type": "PathTracer",
"props": {
"spp": 32,
"spp": 8,
"occludeDistance": 2,
"minDepth": 3,
"maxDepth": 7
Expand Down
Binary file modified data/living_room/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 3 additions & 9 deletions src/core/accelerators/embree-backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ namespace miyuki::core {
}
this->scene = &scene;
rtcScene = rtcNewScene(device);
int id = 0;
for (const auto &mesh : scene.meshes) {
auto geometry = rtcNewGeometry(device, RTC_GEOMETRY_TYPE_TRIANGLE);
auto vertices = (Float *) rtcSetNewGeometryBuffer(geometry, RTC_BUFFER_TYPE_VERTEX, 0,
Expand All @@ -69,14 +68,10 @@ namespace miyuki::core {
triangles[3 * i + j] = mesh->triangles[i].indices.position[j];
}
}
for (size_t i = 0; i < mesh->_vertex_data.position.size(); i++) {
for (int32_t j = 0; j < 3; j++)
vertices[3 * i + j] = mesh->_vertex_data.position[i][j];
}
std::memcpy(vertices,&mesh->_vertex_data.position[0][0], mesh->_vertex_data.position.size() * sizeof(float) * 3);
rtcCommitGeometry(geometry);
rtcAttachGeometryByID(rtcScene, geometry, id);
rtcAttachGeometry(rtcScene, geometry);
rtcReleaseGeometry(geometry);
id++;
}
rtcCommitScene(rtcScene);
}
Expand Down Expand Up @@ -109,9 +104,8 @@ namespace miyuki::core {
if (rayHit.hit.geomID == RTC_INVALID_GEOMETRY_ID || rayHit.hit.primID == RTC_INVALID_GEOMETRY_ID)
return false;
isct.shape = &scene->meshes[rayHit.hit.geomID]->triangles[rayHit.hit.primID];
isct.Ng = isct.shape->Ng();
isct.Ng = normalize(vec3(rayHit.hit.Ng_x, rayHit.hit.Ng_y, rayHit.hit.Ng_z));
isct.uv = Point2f(rayHit.hit.u, rayHit.hit.v);
isct.Ns = isct.shape->normalAt(isct.uv);
isct.distance = rayHit.ray.tfar;
isct.p = ray.o + isct.distance * ray.d;
return true;
Expand Down
112 changes: 60 additions & 52 deletions src/core/accelerators/sahbvh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace miyuki::core {
primitive[i].getBoundingBox().centroid());
}

if (end - begin <= 4 || depth >= 64) {
if (end - begin <= 4 || depth >= 32) {
BVHNode node;

node.box = box;
Expand All @@ -100,61 +100,69 @@ namespace miyuki::core {
axis = 2;
}
}

constexpr size_t nBuckets = 12;
struct Bucket {
size_t count = 0;
Bounds3f bound;

Bucket()
: bound({{MaxFloat, MaxFloat, MaxFloat},
{MinFloat, MinFloat, MinFloat}}) {}
};
Bucket buckets[nBuckets];
for (int i = begin; i < end; i++) {
auto offset = centroidBound.offset(
primitive[i].getBoundingBox().centroid())[axis];
int b = std::min<int>(nBuckets - 1,
std::floor(offset * nBuckets));
buckets[b].count++;
buckets[b].bound =
buckets[b].bound.unionOf(primitive[i].getBoundingBox());
}
Float cost[nBuckets - 1] = {0};
for (int i = 0; i < nBuckets - 1; i++) {
Bounds3f b0, b1;
int count0 = 0, count1 = 0;
for (int j = 0; j <= i; j++) {
b0 = b0.unionOf(buckets[j].bound);
count0 += buckets[j].count;
MeshTriangle *mid = nullptr;
if (size[axis] > 0) {
constexpr size_t nBuckets = 12;
struct Bucket {
size_t count = 0;
Bounds3f bound;

Bucket()
: bound({{MaxFloat, MaxFloat, MaxFloat},
{MinFloat, MinFloat, MinFloat}}) {}
};
Bucket buckets[nBuckets];
for (int i = begin; i < end; i++) {
auto offset = centroidBound.offset(
primitive[i].getBoundingBox().centroid())[axis];
int b = std::min<int>(nBuckets - 1,
std::floor(offset * nBuckets));
buckets[b].count++;
buckets[b].bound =
buckets[b].bound.unionOf(primitive[i].getBoundingBox());
}
for (int j = i + 1; j < nBuckets; j++) {
b1 = b1.unionOf(buckets[j].bound);
count1 += buckets[j].count;
Float cost[nBuckets - 1] = {0};
for (int i = 0; i < nBuckets - 1; i++) {
Bounds3f b0{{MaxFloat, MaxFloat, MaxFloat},
{MinFloat, MinFloat, MinFloat}};
Bounds3f b1{{MaxFloat, MaxFloat, MaxFloat},
{MinFloat, MinFloat, MinFloat}};
int count0 = 0, count1 = 0;
for (int j = 0; j <= i; j++) {
b0 = b0.unionOf(buckets[j].bound);
count0 += buckets[j].count;
}
for (int j = i + 1; j < nBuckets; j++) {
b1 = b1.unionOf(buckets[j].bound);
count1 += buckets[j].count;
}
cost[i] = 0.125 + (count0 * b0.surfaceArea() +
count1 * b1.surfaceArea()) /
box.surfaceArea();
}
cost[i] = 0.125 + (count0 * b0.surfaceArea() +
count1 * b1.surfaceArea()) /
box.surfaceArea();
}
int splitBuckets = 0;
Float minCost = cost[0];
for (int i = 1; i < nBuckets - 1; i++) {
if (cost[i] <= minCost) {
minCost = cost[i];
splitBuckets = i;
int splitBuckets = 0;
Float minCost = cost[0];
for (int i = 1; i < nBuckets - 1; i++) {
if (cost[i] <= minCost) {
minCost = cost[i];
splitBuckets = i;
}
}
MIYUKI_CHECK(minCost > 0);
mid = std::partition(
&primitive[begin], &primitive[end - 1] + 1,
[&](MeshTriangle &p) {
int b = centroidBound.offset(
p.getBoundingBox().centroid())[axis] *
nBuckets;
if (b == nBuckets) {
b = nBuckets - 1;
}
return b <= splitBuckets;
});
} else {
mid = primitive.data() + (begin + end) / 2;
}
auto mid = std::partition(
&primitive[begin], &primitive[end - 1] + 1,
[&](MeshTriangle &p) {
int b = centroidBound.offset(
p.getBoundingBox().centroid())[axis] *
nBuckets;
if (b == nBuckets) {
b = nBuckets - 1;
}
return b <= splitBuckets;
});
auto ret = nodes.size();
nodes.emplace_back();

Expand Down
2 changes: 1 addition & 1 deletion src/core/integrators/rtao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace miyuki::core {
// log::log("{} {} {}\n",sample.ray.o.x,sample.ray.o.y,sample.ray.o.z);
Intersection isct;
if (scene->intersect(sample.ray, isct)) {
auto wo = isct.worldToLocal(-sample.ray.d);
auto wo = isct.worldToLocal(isct.wo);
auto w = CosineHemisphereSampling(sampler->next2D());
if (wo.y * w.y < 0) {
w = -w;
Expand Down
66 changes: 25 additions & 41 deletions src/core/mesh-importers/wavefront-importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,46 +126,17 @@ namespace miyuki::core {

std::unordered_map<std::string, size_t> name_to_id;
std::unordered_map<size_t, std::string> id_to_name;
std::unordered_map<std::string, std::unordered_map<int, std::string>> to_mangled_name;
//
// for (size_t i = 0; i < shapes.size(); i++) {
// const auto &name = shapes[i].name;
//
// if(name_to_id.find(name) == name_to_id.end()){
// name_to_id[name] = name_to_id.size();
// id_to_name[name_to_id[name]] = name;
// }
//
// }

for (size_t i = 0; i < shapes.size(); i++) {
const auto &name = shapes[i].name;

auto iter = to_mangled_name.find(name);

if (iter == to_mangled_name.end()) {
to_mangled_name[name] = {};
}
auto &m = to_mangled_name[name];
size_t count = 0;
for (size_t f = 0; f < shapes[i].mesh.num_face_vertices.size(); f++) {
bool new_name = false;
std::string final_name;

auto mat_id = shapes[i].mesh.material_ids[f];
if (m.find(mat_id) == m.end() || mat_id == -1) {
new_name = true;
if (mat_id >= 0) {
final_name = fmt::format("{}:{}", name, materials[mat_id].name);
} else {
final_name = fmt::format("{}:part{}", name, count + 1);
}
}
if (new_name) {
count++;
m[mat_id] = final_name;
name_to_id[final_name] = name_to_id.size();
auto mat = convertFromMTL(materials[mat_id]);
result.materials.emplace_back(mat);
mesh->materials[final_name] = mat;
id_to_name[name_to_id[final_name]] = final_name;
fmt::print("generated {}\n", final_name);
}
}
}
for (size_t i = 0; i < name_to_id.size(); i++) {
mesh->_names.emplace_back(id_to_name[i]);
}

// Loop over shapes
for (size_t s = 0; s < shapes.size(); s++) {
Expand All @@ -174,8 +145,16 @@ namespace miyuki::core {
for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) {
MeshTriangle primitive;
VertexIndices indices{};
auto mat_id = shapes[s].mesh.material_ids[f];
primitive.name_id = name_to_id.at(to_mangled_name.at(shapes[s].name).at(mat_id));
auto mat = materials[shapes[s].mesh.material_ids[f]].name;
if (mat.empty()) {
primitive.name_id = -1;
} else {
if (name_to_id.find(mat) == name_to_id.end()) {
name_to_id[mat] = name_to_id.size();
id_to_name[name_to_id[mat]] = mat;
}
primitive.name_id = name_to_id.at(mat);
}
int fv = shapes[s].mesh.num_face_vertices[f];
MIYUKI_CHECK(fv == 3);
for (size_t v = 0; v < fv; v++) {
Expand Down Expand Up @@ -205,6 +184,11 @@ namespace miyuki::core {
mesh->triangles.push_back(primitive);
}
}

for (size_t i = 0; i < name_to_id.size(); i++) {
mesh->_names.emplace_back(id_to_name[i]);
}

log::log("loaded {} vertices, {} normals, {} tex coords, {} primitives\n", mesh->_vertex_data.position.size(),
mesh->_vertex_data.normal.size(), mesh->_vertex_data.tex_coord.size(), mesh->triangles.size());
mesh->_loaded = true;
Expand Down
2 changes: 1 addition & 1 deletion src/core/shapes/mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ namespace miyuki::core {
}

Material *MeshTriangle::getMaterial() const {
return mesh->_materials[name_id].get();
return name_id >= 0 ? mesh->_materials[name_id].get() : nullptr;
}
}

Loading

0 comments on commit 54e683a

Please sign in to comment.