Skip to content

Commit

Permalink
Update utilities for parallel processing and memory arena.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed Jul 28, 2018
1 parent f929ce6 commit d9a2ee7
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 107 deletions.
16 changes: 7 additions & 9 deletions sources/accelerators/bvh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static const int orderTable[] = {
0x40123, 0x40132, 0x41023, 0x41032, 0x42301, 0x43201, 0x42310, 0x43210, // ++|++
};

int test_AABB(const __m128 bboxes[2][3], const __m128 org[3], const __m128 idir[3], const int sign[3], __m128 tmin, __m128 tmax) {
int test_AABB(const __m128 bboxes[2][3], const __m128 org[3], const __m128 idir[3], const int sign[3], __m128 tmin, __m128 tmax) noexcept {
tmin = _mm_max_ps(tmin, _mm_mul_ps(_mm_sub_ps(bboxes[sign[0]][0], org[0]), idir[0]));
tmax = _mm_min_ps(tmax, _mm_mul_ps(_mm_sub_ps(bboxes[1 - sign[0]][0], org[0]), idir[0]));
tmin = _mm_max_ps(tmin, _mm_mul_ps(_mm_sub_ps(bboxes[sign[1]][1], org[1]), idir[1]));
Expand Down Expand Up @@ -134,6 +134,7 @@ BVHAccel::BVHAccel(const std::vector<std::shared_ptr<Primitive>> &prims,
}

BVHAccel::~BVHAccel() {
release();
}

Bounds3d BVHAccel::worldBound() const {
Expand All @@ -144,7 +145,7 @@ void BVHAccel::construct() {
if (primitives_.empty()) return;

std::vector<BVHPrimitiveInfo> primitiveInfo(primitives_.size());
for (int i = 0; i < primitives_.size(); i++) {
for (int i = 0; i < (int)primitives_.size(); i++) {
primitiveInfo[i] = { i, primitives_[i]->worldBound() };
}

Expand Down Expand Up @@ -241,10 +242,10 @@ BVHNode* BVHAccel::constructRec(std::vector<BVHPrimitiveInfo>& buildData,
}

void BVHAccel::release() {
for (int i = 0; i < simdNodes_.size(); i++) {
align_free(simdNodes_[i]);
for (auto &node : simdNodes_) {
align_free(node);
}

root_ = nullptr;
simdNodes_.clear();
}
Expand All @@ -253,8 +254,7 @@ void BVHAccel::collapse2QBVH(BVHNode* node) {
BVHNode *lc = node->left;
BVHNode *rc = node->right;

SIMDBVHNode* n =
static_cast<SIMDBVHNode*>(align_alloc(sizeof(SIMDBVHNode), 16));
SIMDBVHNode* n = (SIMDBVHNode *)align_alloc(sizeof(SIMDBVHNode), 16);
Assertion(n != nullptr, "allocation failed !!");

simdNodes_.push_back(n);
Expand Down Expand Up @@ -337,7 +337,6 @@ bool BVHAccel::intersectBVH(Ray &ray, SurfaceInteraction *isect) const{
nodeStack.push(root_);

bool hit = false;
int cnt = 0;
while (!nodeStack.empty()) {
BVHNode* node = nodeStack.top();
nodeStack.pop();
Expand Down Expand Up @@ -367,7 +366,6 @@ bool BVHAccel::intersectBVH(Ray& ray) const {
std::stack<BVHNode*> nodeStack;
nodeStack.push(root_);

bool hit = false;
while (!nodeStack.empty()) {
BVHNode* node = nodeStack.top();
nodeStack.pop();
Expand Down
14 changes: 8 additions & 6 deletions sources/core/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "memory.h"

#include <cstdlib>
#include <cstddef>
#include <algorithm>

namespace spica {
Expand All @@ -22,12 +23,13 @@ MemoryArena::MemoryArena(MemoryArena&& arena) noexcept

MemoryArena::~MemoryArena() {
align_free(currentBlock_);
for (auto& block : usedBlocks_) align_free(block.second);
for (auto& block : usedBlocks_) align_free(block.second);
for (auto& block : availableBlocks_) align_free(block.second);
}

void* MemoryArena::allocBytes(size_t nBytes) {
nBytes = ((nBytes + 0xf) & (~0xf));
const int align = alignof(std::max_align_t);
nBytes = ((nBytes + align - 1) & ~(align - 1));
if (currentBlockPos_ + nBytes > currentAllocSize_) {
if (currentBlock_) {
usedBlocks_.emplace_back(currentAllocSize_, currentBlock_);
Expand All @@ -39,15 +41,15 @@ void* MemoryArena::allocBytes(size_t nBytes) {
iter != availableBlocks_.end(); ++iter) {
if (iter->first >= nBytes) {
currentAllocSize_ = iter->first;
currentBlock_ = iter->second;
currentBlock_ = iter->second;
availableBlocks_.erase(iter);
break;
}
}

if (!currentBlock_) {
currentAllocSize_ = std::max(nBytes, blockSize_);
currentBlock_ = (unsigned char*)align_alloc(currentAllocSize_, 64);
currentBlock_ = (uint8_t*)align_alloc(currentAllocSize_, 64);
}
currentBlockPos_ = 0;
}
Expand All @@ -63,8 +65,8 @@ void MemoryArena::reset() {

size_t MemoryArena::totalAllocated() const {
size_t total = currentAllocSize_;
for (auto& block : usedBlocks_) total += block.first;
for (auto& block : availableBlocks_) total += block.first;
for (const auto &block : usedBlocks_) total += block.first;
for (const auto &block : availableBlocks_) total += block.first;
return total;
}

Expand Down
4 changes: 2 additions & 2 deletions sources/core/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SPICA_EXPORTS MEM_ALIGN(64) MemoryArena : private Uncopyable {
using Elem = typename std::remove_extent<T>::type;
Elem *ret = (Elem*)allocBytes(sizeof(Elem) * size);
for (size_t i = 0; i < size; i++) {
new (ret + i) Elem();
new (&ret[i]) Elem();
}
return ret;
}
Expand All @@ -60,7 +60,7 @@ class SPICA_EXPORTS MEM_ALIGN(64) MemoryArena : private Uncopyable {
const size_t blockSize_;
size_t currentBlockPos_ = 0;
size_t currentAllocSize_ = 0;
unsigned char* currentBlock_ = nullptr;
uint8_t* currentBlock_ = nullptr;
std::list<std::pair<size_t, unsigned char*>> usedBlocks_, availableBlocks_;
}; // class MemoryArena

Expand Down

0 comments on commit d9a2ee7

Please sign in to comment.