vcglib/apps/nexus/prefetch.cpp

130 lines
3.8 KiB
C++
Raw Normal View History

2004-12-15 09:46:16 +01:00
#include <set>
2004-12-13 01:44:48 +01:00
#include "prefetch.h"
2004-12-14 15:10:22 +01:00
#include "nexusmt.h"
2004-12-13 01:44:48 +01:00
using namespace std;
using namespace nxs;
void Prefetch::init(NexusMt *m, std::vector<unsigned int> &selected,
std::vector<PServer::Item> &visited) {
2004-12-14 15:10:22 +01:00
// cerr << "Init\n";
2004-12-13 01:44:48 +01:00
safety.lock();
mt = m;
missing.clear();
2004-12-15 09:46:16 +01:00
mt->todraw.clear();
2004-12-13 01:44:48 +01:00
2004-12-14 15:10:22 +01:00
unsigned int notloaded = 0;
2004-12-15 09:46:16 +01:00
set<unsigned int> tmp;
//std::map<unsigned int, float> tmp;
vector<QueuePServer::Data> flush;
2004-12-13 01:44:48 +01:00
for(unsigned int i = 0; i < selected.size(); i++) {
unsigned int patch = selected[i];
2004-12-15 09:46:16 +01:00
tmp.insert(patch);
//tmp[patch] = 0.0f;
if(!mt->patches.entries[patch].patch) {
//cerr << "miss: " << patch << endl;
load.post(patch);
notloaded++;
} else {
PatchInfo &info = mt->index[patch];
QueuePServer::Data &data = mt->patches.Lookup(patch, info.nvert, info.nface, 0.0f, flush);
if(flush.size() != 0) {
cerr << "Flushing!\n";
exit(0);
}
mt->todraw.push_back(&data);
}
//missing.push_back(PServer::Item(patch, 0.0f));
2004-12-13 01:44:48 +01:00
}
2004-12-14 15:10:22 +01:00
if(notloaded)
cerr << "Patches to load: " << notloaded << endl;
2004-12-13 01:44:48 +01:00
for(unsigned int i = 0; i < visited.size(); i++) {
PServer::Item &item = visited[i];
if(tmp.count(item.patch)) continue;
2004-12-15 09:46:16 +01:00
// if(mt->patches.entries[item.patch].patch)
// tmp[item.patch] = item.priority;
if(item.priority != 0.0f)
missing.push_back(item);
2004-12-13 01:44:48 +01:00
}
QueuePServer &ps = mt->patches;
for(unsigned int i = 0; i < ps.heap.size(); i++) {
PServer::Item &item = ps.heap[i];
if(tmp.count(item.patch))
2004-12-15 09:46:16 +01:00
item.priority = 0;
else {
if(item.priority == 0)
item.priority = 1;
item.priority *= 1.1;
}
/*if(tmp.count(item.patch))
2004-12-13 01:44:48 +01:00
item.priority = tmp[item.patch];
else
2004-12-15 09:46:16 +01:00
item.priority = 1e30;*/
2004-12-13 01:44:48 +01:00
}
make_heap(ps.heap.begin(), ps.heap.end());
2004-12-14 15:10:22 +01:00
2004-12-13 01:44:48 +01:00
sort(missing.begin(), missing.end()); //CRITICAL reverse pero'!
2004-12-15 09:46:16 +01:00
reverse(missing.begin(), missing.end());
load.post(0xffffffff);
2004-12-13 01:44:48 +01:00
safety.unlock();
}
2004-12-15 09:46:16 +01:00
void Prefetch::execute() {
2004-12-13 01:44:48 +01:00
while(1) {
2004-12-15 09:46:16 +01:00
if(get_signaled()) return;
vector<QueuePServer::Data> flush;
2004-12-14 15:10:22 +01:00
2004-12-15 09:46:16 +01:00
if(load.get_count() || missing.size() == 0) {
pt::message *msg = load.getmessage();
if(msg->id != 0xffffffff) {
safety.lock();
PatchInfo &info = mt->index[msg->id];
2004-12-14 15:10:22 +01:00
2004-12-15 09:46:16 +01:00
//posting draw message
QueuePServer::Data &data = mt->patches.Lookup(msg->id, info.nvert, info.nface, 0.0f, flush);
pt::message *msg = new pt::message(QueuePServer::DRAW, (unsigned int)&data);
msg->result = msg->id;
draw.post(msg);
//p;osting flush messages
for(unsigned int i = 0; i < flush.size(); i++) {
QueuePServer::Data *data = new QueuePServer::Data;
*data = flush[i];
draw.post(QueuePServer::FLUSH, (unsigned int)data);
}
safety.unlock();
}
delete msg;
} else {
safety.lock();
if(missing.size() != 0) {
PServer::Item item = missing.back();
missing.pop_back();
if(item.priority > mt->patches.MaxPriority()) {
missing.clear();
} else {
PatchInfo &info = mt->index[item.patch];
//cerr << "prefetching: " << item.patch << endl;
mt->patches.Lookup(item.patch, info.nvert, info.nface, item.priority, flush);
for(unsigned int i = 0; i < flush.size(); i++) {
QueuePServer::Data *data = new QueuePServer::Data;
*data = flush[i];
draw.post(QueuePServer::FLUSH, (unsigned int)data);
}
}
}
safety.unlock();
}
}
2004-12-13 01:44:48 +01:00
}