Skip to content

Commit

Permalink
fix crashes, memory
Browse files Browse the repository at this point in the history
  • Loading branch information
potree committed Jan 26, 2020
1 parent 6f11c82 commit 84cfd61
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 52 deletions.
100 changes: 53 additions & 47 deletions modules/progressive/ProgressiveBINLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <algorithm>
#include <sstream>

#include "GL\glew.h"
#include "GL\glew.h"
#include "GLFW\glfw3.h"

#include "ComputeShader.h"
Expand Down Expand Up @@ -61,6 +61,8 @@ class BINLoader {
auto size = fs::file_size(file);
numPoints = size / 16;
//numPoints = numPoints > 400'000'000 ? 400'000'000 : numPoints;

//numPoints = 400'000'000;
}

createBinaryLoaderThread();
Expand Down Expand Up @@ -163,36 +165,36 @@ class BINLoader {

class ProgressiveBINLoader {

// see https://en.wikipedia.org/wiki/Primality_test
bool isPrime(uint64_t n) {
if (n <= 3) {
return n > 1;
} else if ((n % 2) == 0 || (n % 3) == 0) {
return false;
}

uint64_t i = 5;
while ((i * i) <= n) {
if ((n % i) == 0 || (n % (i + 2)) == 0) {
return false;
}


i = i + 6;
}

return true;
// see https://en.wikipedia.org/wiki/Primality_test
bool isPrime(uint64_t n) {
if (n <= 3) {
return n > 1;
} else if ((n % 2) == 0 || (n % 3) == 0) {
return false;
}

uint64_t i = 5;
while ((i * i) <= n) {
if ((n % i) == 0 || (n % (i + 2)) == 0) {
return false;
}


i = i + 6;
}

return true;
}

//
// Primes where p = 3 mod 4 allow us to generate random numbers without duplicates in range [0, prime - 1]
// https://preshing.com/20121224/how-to-generate-a-sequence-of-unique-random-integers/
uint64_t previousPrimeCongruent3mod4(uint64_t start) {
for (uint64_t i = start -1; true; i--) {
if ((i % 4) == 3 && isPrime(i)) {
return i;
}
}
uint64_t previousPrimeCongruent3mod4(uint64_t start) {
for (uint64_t i = start -1; true; i--) {
if ((i % 4) == 3 && isPrime(i)) {
return i;
}
}
}

public:
Expand Down Expand Up @@ -235,7 +237,7 @@ class ProgressiveBINLoader {

glCreateBuffers(1, &ssVertexBuffer);
uint32_t size = numPointsInBuffer * bytePerPoint;


//auto flags = GL_MAP_WRITE_BIT;
//auto flags = GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT;
//glNamedBufferStorage(ssVertexBuffer, size, 0, flags);
Expand All @@ -261,10 +263,10 @@ class ProgressiveBINLoader {
glNamedBufferData(ssChunk4B, chunkSize, nullptr, usage);
}

{
glCreateBuffers(1, &ssDebug);
glNamedBufferData(ssDebug, loader->numPoints * 4, nullptr, usage);
}
//{
// glCreateBuffers(1, &ssDebug);
// glNamedBufferData(ssDebug, loader->numPoints * 4, nullptr, usage);
//}

string csPath = "../../modules/progressive/distribute.cs";
csDistribute = new ComputeShader(csPath);
Expand Down Expand Up @@ -308,6 +310,10 @@ class ProgressiveBINLoader {
{// upload
glNamedBufferSubData(ssChunk16B, 0, chunkSize, chunk->data);
//glNamedBufferSubData(ssChunkIndices, 0, chunkSize * 4, chunk->shuffledOrder.data());

// don't keep the data in RAM
// only for benchmarking reasons, do not commit uncommented!?!
//delete chunk;
}

{// distribute to shuffled location
Expand All @@ -317,7 +323,7 @@ class ProgressiveBINLoader {

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, ssInput);

glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 15, ssDebug);
//glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 15, ssDebug);

//cout << "ssDebug: " << ssDebug << endl;

Expand Down Expand Up @@ -454,21 +460,21 @@ class ProgressiveBINLoader {

}

template<typename SourceType, typename TargetType>
static void transformAttribute(void* sourceBuffer, void *targetBuffer, int numPoints, double scale, double offset, int targetByteOffset) {

SourceType* source = reinterpret_cast<SourceType*>(sourceBuffer);
int32_t* target = reinterpret_cast<int32_t*>(targetBuffer);

for (int i = 0; i < numPoints; i++) {
TargetType value = double(source[i]) * scale + offset;

//target[i] = *reinterpret_cast<int32_t*>(&value);

auto ptr = &reinterpret_cast<uint8_t*>(targetBuffer)[4 * i + targetByteOffset];
reinterpret_cast<TargetType*>(ptr)[0] = value;
}

template<typename SourceType, typename TargetType>
static void transformAttribute(void* sourceBuffer, void *targetBuffer, int numPoints, double scale, double offset, int targetByteOffset) {

SourceType* source = reinterpret_cast<SourceType*>(sourceBuffer);
int32_t* target = reinterpret_cast<int32_t*>(targetBuffer);

for (int i = 0; i < numPoints; i++) {
TargetType value = double(source[i]) * scale + offset;

//target[i] = *reinterpret_cast<int32_t*>(&value);

auto ptr = &reinterpret_cast<uint8_t*>(targetBuffer)[4 * i + targetByteOffset];
reinterpret_cast<TargetType*>(ptr)[0] = value;
}

}

};
Expand Down
8 changes: 4 additions & 4 deletions modules/progressive/distribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ struct Vertex{



layout(std430, binding = 15) buffer ssDebugBuffer{
uint debugBuffer[];
};
// layout(std430, binding = 15) buffer ssDebugBuffer{
// uint debugBuffer[];
// };

layout(location = 2) uniform int uNumPoints;
layout(location = 3) uniform double uPrime;
Expand Down Expand Up @@ -120,7 +120,7 @@ void main(){
// disable shuffling
//targetIndex = globalInputIndex;

atomicAdd(debugBuffer[targetIndex], 1);
// atomicAdd(debugBuffer[targetIndex], 1);

//targetIndex = globalInputIndex;
//targetIndex = 124122626 - globalInputIndex;
Expand Down
1 change: 0 additions & 1 deletion modules/progressive/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ monitorJS(`${rootDir}/modules/progressive/eval/benchmark_candi_sari.js`);
monitorJS(`${rootDir}/modules/progressive/eval/benchmark_matterhorn.js`);
monitorJS(`${rootDir}/modules/progressive/eval/benchmark_morro_bay.js`);
monitorJS(`${rootDir}/modules/progressive/eval/benchmark_morro_bay_1billion.js`);
monitorJS(`${rootDir}/modules/progressive/eval/benchmark_small.js`);
monitorJS(`${rootDir}/modules/progressive/eval/screenshots_wien.js`);
monitorJS(`${rootDir}/modules/progressive/eval/vr_wien.js`);
monitorJS(`${rootDir}/modules/progressive/eval/vr_candi_sari.js`);
Expand Down
6 changes: 6 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ void monitorFile(string file, std::function<void()> event) {

std::thread([file, event]() {

if (!fs::exists(file)) {
cout << "ERROR(monitorFile): file does not exist: " << file << endl;

return;
}

auto lastWriteTime = fs::last_write_time(fs::path(file));

while (true) {
Expand Down

0 comments on commit 84cfd61

Please sign in to comment.