Skip to content

Commit c5cfdb8

Browse files
committed
QBVH is now optional
1 parent 0df48f2 commit c5cfdb8

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

RadeonRays/src/intersector/intersector_lds.cpp

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ namespace RadeonRays
7171
IntersectorLDS::IntersectorLDS(Calc::Device *device)
7272
: Intersector(device)
7373
, m_gpuData(new GpuData(device))
74-
, m_bvh(nullptr)
7574
{
7675
std::string buildopts;
7776
#ifdef RR_RAY_MASK
@@ -119,30 +118,36 @@ namespace RadeonRays
119118
void IntersectorLDS::Process(const World &world)
120119
{
121120
// If something has been changed we need to rebuild BVH
122-
if (!m_bvh || world.has_changed() || world.GetStateChange() != ShapeImpl::kStateChangeNone)
121+
if (!m_gpuData->bvh || world.has_changed() || world.GetStateChange() != ShapeImpl::kStateChangeNone)
123122
{
124123
// Free previous data
125-
if (m_bvh)
124+
if (m_gpuData->bvh)
126125
{
127126
m_device->DeleteBuffer(m_gpuData->bvh);
128127
}
129128

130129
// Look up build options for world
130+
auto type = world.options_.GetOption("bvh.type");
131131
auto builder = world.options_.GetOption("bvh.builder");
132132
auto nbins = world.options_.GetOption("bvh.sah.num_bins");
133133
auto tcost = world.options_.GetOption("bvh.sah.traversal_cost");
134134

135-
bool use_sah = false;
135+
bool use_qbvh = true, use_sah = false;
136136
int num_bins = (nbins ? static_cast<int>(nbins->AsFloat()) : 64);
137137
float traversal_cost = (tcost ? tcost->AsFloat() : 10.0f);
138138

139+
if (type && type->AsString() == "qbvh")
140+
{
141+
use_qbvh = true;
142+
}
143+
139144
if (builder && builder->AsString() == "sah")
140145
{
141146
use_sah = true;
142147
}
143148

144149
// Create the bvh
145-
m_bvh.reset(new Bvh2(traversal_cost, num_bins, use_sah));
150+
Bvh2 bvh(traversal_cost, num_bins, use_sah);
146151

147152
// Partition the array into meshes and instances
148153
std::vector<const Shape *> shapes(world.shapes_);
@@ -154,34 +159,62 @@ namespace RadeonRays
154159
});
155160

156161
// TODO: deal with the instance stuff (gboisse)
157-
m_bvh->Build(shapes.begin(), firstinst);
162+
bvh.Build(shapes.begin(), firstinst);
158163

159-
QBvhTranslator translator;
160-
translator.Process(*m_bvh);
164+
// Upload BVH data to GPU memory
165+
if (!use_qbvh)
166+
{
167+
auto bvh_size_in_bytes = bvh.GetSizeInBytes();
168+
m_gpuData->bvh = m_device->CreateBuffer(bvh_size_in_bytes, Calc::BufferType::kRead);
161169

162-
// Update GPU data
163-
auto bvh_size_in_bytes = translator.GetSizeInBytes();
164-
m_gpuData->bvh = m_device->CreateBuffer(bvh_size_in_bytes, Calc::BufferType::kRead);
170+
// Get the pointer to mapped data
171+
Calc::Event *e = nullptr;
172+
Bvh2::Node *bvhdata = nullptr;
165173

166-
// Get the pointer to mapped data
167-
Calc::Event *e = nullptr;
168-
QBvhTranslator::Node *bvhdata = nullptr;
174+
m_device->MapBuffer(m_gpuData->bvh, 0, 0, bvh_size_in_bytes, Calc::MapType::kMapWrite, (void **)&bvhdata, &e);
169175

170-
m_device->MapBuffer(m_gpuData->bvh, 0, 0, bvh_size_in_bytes, Calc::MapType::kMapWrite, (void **)&bvhdata, &e);
176+
e->Wait();
177+
m_device->DeleteEvent(e);
171178

172-
e->Wait();
173-
m_device->DeleteEvent(e);
179+
// Copy BVH data
180+
for (std::size_t i = 0; i < bvh.m_nodecount; ++i)
181+
bvhdata[i++] = bvh.m_nodes[i];
174182

175-
// Copy BVH data
176-
std::size_t i = 0;
177-
for (auto it = translator.nodes_.begin(); it != translator.nodes_.end(); ++it)
178-
bvhdata[i++] = *it;
183+
// Unmap gpu data
184+
m_device->UnmapBuffer(m_gpuData->bvh, 0, bvhdata, &e);
185+
186+
e->Wait();
187+
m_device->DeleteEvent(e);
188+
}
189+
else
190+
{
191+
QBvhTranslator translator;
192+
translator.Process(bvh);
179193

180-
// Unmap gpu data
181-
m_device->UnmapBuffer(m_gpuData->bvh, 0, bvhdata, &e);
194+
// Update GPU data
195+
auto bvh_size_in_bytes = translator.GetSizeInBytes();
196+
m_gpuData->bvh = m_device->CreateBuffer(bvh_size_in_bytes, Calc::BufferType::kRead);
182197

183-
e->Wait();
184-
m_device->DeleteEvent(e);
198+
// Get the pointer to mapped data
199+
Calc::Event *e = nullptr;
200+
QBvhTranslator::Node *bvhdata = nullptr;
201+
202+
m_device->MapBuffer(m_gpuData->bvh, 0, 0, bvh_size_in_bytes, Calc::MapType::kMapWrite, (void **)&bvhdata, &e);
203+
204+
e->Wait();
205+
m_device->DeleteEvent(e);
206+
207+
// Copy BVH data
208+
std::size_t i = 0;
209+
for (auto it = translator.nodes_.begin(); it != translator.nodes_.end(); ++it)
210+
bvhdata[i++] = *it;
211+
212+
// Unmap gpu data
213+
m_device->UnmapBuffer(m_gpuData->bvh, 0, bvhdata, &e);
214+
215+
e->Wait();
216+
m_device->DeleteEvent(e);
217+
}
185218

186219
// Make sure everything is committed
187220
m_device->Finish(0);

RadeonRays/src/intersector/intersector_lds.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,5 @@ namespace RadeonRays
5252

5353
// Implementation data
5454
std::unique_ptr<GpuData> m_gpuData;
55-
// Bvh data structure
56-
std::unique_ptr<Bvh2> m_bvh;
5755
};
5856
}

0 commit comments

Comments
 (0)