Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cuteday committed Apr 14, 2024
1 parent 8013680 commit a1ac667
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 42 deletions.
9 changes: 6 additions & 3 deletions src/core/device/soa.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class SOA {
template <typename T>
class SOAIterator {
public:
using difference_type = int;
using difference_type = int;
using value_type = typename SOA<T>::GetSetIndirector;
using reference = typename SOA<T>::GetSetIndirector &;
using pointer = void;
using iterator_category = std::random_access_iterator_tag;

KRR_CALLABLE SOAIterator() : m_soa(nullptr), m_index(0) {}
KRR_CALLABLE SOAIterator(SOA<T> *soa, int index) : m_soa(soa), m_index(index) {}
Expand All @@ -39,13 +43,12 @@ class SOAIterator {
KRR_CALLABLE bool operator>(const SOAIterator &it) const { return m_index > it.m_index; }
KRR_CALLABLE bool operator>=(const SOAIterator &it) const { return m_index >= it.m_index; }

KRR_CALLABLE SOA<T> *operator->() { return m_soa; }
KRR_CALLABLE typename SOA<T>::GetSetIndirector operator*() { return {m_soa, m_index}; }
KRR_CALLABLE typename SOA<T>::GetSetIndirector operator[](difference_type n) { return {m_soa, m_index + n}; }

private:
std::conditional_t<std::is_const_v<T>, const SOA<T>*, SOA<T>*> m_soa;
int m_index;
difference_type m_index;
};

NAMESPACE_END(krr)
11 changes: 11 additions & 0 deletions src/render/wavefront/workqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ class PixelStateBuffer : public SOA<PixelState> {
template <typename WorkItem>
class WorkQueue : public SOA<WorkItem> {
public:
using value_type = WorkItem;
using iterator = typename SOA<WorkItem>::iterator;
using const_iterator = typename SOA<WorkItem>::const_iterator;
using reverse_iterator = typename SOA<WorkItem>::reverse_iterator;
using const_reverse_iterator = typename SOA<WorkItem>::const_reverse_iterator;

WorkQueue() = default;
KRR_HOST WorkQueue(int n, Allocator alloc) : SOA<WorkItem>(n, alloc) {}
KRR_HOST WorkQueue& operator=(const WorkQueue& w) {
Expand All @@ -34,6 +40,11 @@ class WorkQueue : public SOA<WorkItem> {
return *this;
}

KRR_CALLABLE iterator begin() { return iterator(this, 0); }
KRR_CALLABLE const_iterator begin() const { return const_iterator(this, 0); }
KRR_CALLABLE iterator end() { return iterator(this, m_size.load()); }
KRR_CALLABLE const_iterator end() const { return const_iterator(this, m_size.load()); }

KRR_CALLABLE int size() const {
return m_size.load();
}
Expand Down
12 changes: 6 additions & 6 deletions src/util/morton.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ KRR_CALLABLE T expandBits(T x) {
}

template <typename T, int dim, std::enable_if_t<std::is_integral_v<T>, bool> = true>
KRR_CALLABLE T mortonEncode(Vector<T, dim> v) {
KRR_CALLABLE T encodeMorton(Vector<T, dim> v) {
if constexpr (dim == 2) {
return (expandBits<T, 1>(v[1]) << 1) | expandBits<T, 1>(v[0]);
} else if constexpr (dim == 3) {
return (expandBits<T, 2>(v[2]) << 2) | (expandBits<T, 2>(v[1]) << 1) |
expandBits<T, 2>(v[0]);
} else {
static_assert(false, "mortonEncode<int>: dim must be 2 or 3.");
static_assert(false, "encodeMorton<int>: dim must be 2 or 3.");
}
}

template <typename T, int dim, std::enable_if_t<std::is_floating_point_v<T>, bool> = true>
KRR_CALLABLE decltype(auto) mortonEncode(Vector<T, dim> v) {
KRR_CALLABLE decltype(auto) encodeMorton(Vector<T, dim> v) {
using VectorType = Vector<T, dim>;
if constexpr (std::is_same_v<T, float>) {
v = VectorType(v * 1024.f).cwiseMin(1023.f).cwiseMax(0.f);
return mortonEncode<uint32_t, dim>(Vector<uint32_t, dim>(v));
return encodeMorton<uint32_t, dim>(Vector<uint32_t, dim>(v));
} else if constexpr (std::is_same_v<T, double>) {
v = VectorType(v * (1 << 21)).cwiseMax(0.0).cwiseMin(static_cast<double>((1 << 21) - 1));
return mortonEncode<uint64_t, dim>(Vector<uint64_t, dim>(v));
return encodeMorton<uint64_t, dim>(Vector<uint64_t, dim>(v));
} else {
static_assert(false, "mortonEncode<float>: T must be float or double.");
static_assert(false, "encodeMorton<float>: T must be float or double.");
}
}

Expand Down
44 changes: 11 additions & 33 deletions src/util/soac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,8 @@ int main(int argc, char* argv[]) {

// operator[] madness...
printf(" struct GetSetIndirector {\n");
if (!soa.templateType.empty()) {
printf(" KRR_CALLABLE\n");
printf(" operator %s<%s>() const {\n", soa.type.c_str(),
soa.templateType.c_str());
printf(" %s<%s> r;\n", soa.type.c_str(), soa.templateType.c_str());
}
else {
printf(" KRR_CALLABLE\n");
printf(" operator %s() const {\n", soa.type.c_str());
printf(" %s r;\n", soa.type.c_str());
}
printf(" KRR_CALLABLE operator %s() const {\n",workItemName.c_str());
printf(" %s r;\n", workItemName.c_str());
for (const auto& member : soa.members)
for (int i = 0; i < member.names.size(); ++i) {
std::string name = member.names[i];
Expand All @@ -431,12 +422,7 @@ int main(int argc, char* argv[]) {
printf(" return r;\n");
printf(" }\n");

printf(" KRR_CALLABLE\n");
if (!soa.templateType.empty())
printf(" void operator=(const %s<%s> &a) {\n", soa.type.c_str(),
soa.templateType.c_str());
else
printf(" void operator=(const %s &a) {\n", soa.type.c_str());
printf(" KRR_CALLABLE void operator=(const %s &a) {\n", workItemName.c_str());
for (const auto& member : soa.members)
for (int i = 0; i < member.names.size(); ++i) {
std::string name = member.names[i];
Expand All @@ -451,27 +437,20 @@ int main(int argc, char* argv[]) {
name.c_str());
}
printf(" }\n\n");
printf(" KRR_CALLABLE GetSetIndirector() = default;\n"); // default ctor
// GetSetIndirector members
printf(" SOA *soa;\n");
printf(" int i;\n");
printf(" };\n\n");

printf(" KRR_CALLABLE\n");
printf(" GetSetIndirector operator[](int i) {\n");
printf(" KRR_CALLABLE GetSetIndirector operator[](int i) {\n");
printf(" DCHECK_LT(i, nAlloc);\n");
printf(" return GetSetIndirector{this, i};\n");
printf(" }\n");
printf(" KRR_CALLABLE\n");
if (!soa.templateType.empty()) {
printf(" %s<%s> operator[](int i) const {\n", soa.type.c_str(),
soa.templateType.c_str());
printf(" DCHECK_LT(i, nAlloc);\n");
printf(" %s<%s> r;\n", soa.type.c_str(), soa.templateType.c_str());
}
else {
printf(" %s operator[](int i) const {\n", soa.type.c_str());
printf(" DCHECK_LT(i, nAlloc);\n");
printf(" %s r;\n", soa.type.c_str());
}

printf(" KRR_CALLABLE %s operator[](int i) const {\n", workItemName.c_str());
printf(" DCHECK_LT(i, nAlloc);\n");
printf(" %s r;\n", workItemName.c_str());
for (const auto& member : soa.members)
for (int i = 0; i < member.names.size(); ++i) {
std::string name = member.names[i];
Expand All @@ -485,8 +464,7 @@ int main(int argc, char* argv[]) {
printf(" r.%s = this->%s[i];\n", name.c_str(), name.c_str());
}
printf(" return r;\n");
printf(" }\n");
printf("\n");
printf(" }\n\n");

// Member definitions
printf(" int nAlloc{ };\n");
Expand Down

0 comments on commit a1ac667

Please sign in to comment.