-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGradientBasedFiller.cpp
More file actions
131 lines (102 loc) · 6.65 KB
/
Copy pathGradientBasedFiller.cpp
File metadata and controls
131 lines (102 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "GradientBasedFiller.h"
#include <iostream>
#include <cmath>
GradientBasedFiller::GradientBasedFiller(GLHandler *glHandler, unsigned int kernelRadius, unsigned int batchSize = 0) {
GradientBasedFiller::glHandler = glHandler;
GradientBasedFiller::kernelRadius = kernelRadius;
GradientBasedFiller::batchSize = batchSize;
GradientBasedFiller::stageUsesGPU = true;
}
GradientBasedFiller::~GradientBasedFiller() {
glHandler->deleteBuffer(GLHandler::EFTDEM_GRADIENT_BUFFER);
glHandler->deleteBuffer(GLHandler::EFTDEM_KERNEL_BUFFER);
glHandler->deleteBuffer(GLHandler::EFTDEM_HORIZONTAL_BUFFER);
glHandler->deleteBuffer(GLHandler::EFTDEM_TOTAL_WEIGHT_BUFFER);
glHandler->deleteBuffer(GLHandler::EFTDEM_SUM_BUFFER);
glHandler->deleteBuffer(GLHandler::EFTDEM_CLOSING_MASK_BUFFER);
}
/**
* Applies gradient-based filling on the provided heightMap.
* @param map the Heightmap that should be closed (if the Heightmap is already in the EFTDEM_HEIGHTMAP_BUFFER, the given map won't be used and may be an empty heightMap)
* @param generateOutput specifies whether to return a heightMap Object with the filled map or just an empty heightMap. This improves efficiency, by only moving Data from and to graphics memory when necessary.
* @return a heightMap with the filled height-data, or an empty map.
*/
heightMap * GradientBasedFiller::apply(heightMap *map, bool generateOutput) {
using namespace gl;
std::cout << "Applying gradient based filter using OpenGL..." << std::endl << std::endl;
auto start = std::chrono::high_resolution_clock::now();
//The shaders in this vector will be executed
shaderPaths = std::vector<std::string>();
auto pixelCount = (long) (map->resolutionX * map->resolutionY);
/*This vector should contain a vector of bufferSpecifications for every Shader specified in shaderPaths,
* containing the Specifications for all Buffers that need to be initialized before the respective shader is executed.*/
auto bufferSpecs = std::vector<std::vector<bufferSpecifications>>();
shaderPaths.emplace_back("gradient.glsl");
bufferSpecs.emplace_back(std::vector<bufferSpecifications>{bufferSpecifications{GLHandler::EFTDEM_GRADIENT_BUFFER, long(sizeof(GLfloat) * pixelCount * 2)}});
shaderPaths.emplace_back("kernelIDW.glsl");
bufferSpecs.emplace_back(std::vector<bufferSpecifications>{bufferSpecifications{GLHandler::EFTDEM_KERNEL_BUFFER, long(sizeof(GLfloat) * kernelRadius)}});
shaderPaths.emplace_back("gradientTotalWeightHorizontal.glsl");
bufferSpecs.emplace_back(std::vector<bufferSpecifications>{bufferSpecifications{GLHandler::EFTDEM_HORIZONTAL_BUFFER, long(sizeof(GLfloat) * pixelCount * 2)}});
shaderPaths.emplace_back("gradientTotalWeights.glsl");
bufferSpecs.emplace_back(std::vector<bufferSpecifications>{bufferSpecifications{GLHandler::EFTDEM_TOTAL_WEIGHT_BUFFER, long(sizeof(GLfloat) * pixelCount * 2)}});
shaderPaths.emplace_back("gradientSumHorizontal.glsl");
bufferSpecs.emplace_back();
shaderPaths.emplace_back("gradientSum.glsl");
bufferSpecs.emplace_back(std::vector<bufferSpecifications>{bufferSpecifications{GLHandler::EFTDEM_SUM_BUFFER, long(sizeof(GLfloat) * pixelCount * 2)}});
shaderPaths.emplace_back("gradientFiller.glsl");
bufferSpecs.emplace_back();
shaderPaths.emplace_back("discretization.glsl");
bufferSpecs.emplace_back(std::vector<bufferSpecifications>{bufferSpecifications{GLHandler::EFTDEM_CLOSING_MASK_BUFFER, long(sizeof(GLfloat) * pixelCount)}});
shaderPaths.emplace_back("IDWsumHorizontal.glsl");
bufferSpecs.emplace_back();
shaderPaths.emplace_back("IDWsum.glsl");
bufferSpecs.emplace_back();
shaderPaths.emplace_back("totalWeightsHorizontal.glsl");
bufferSpecs.emplace_back();
shaderPaths.emplace_back("totalWeights.glsl");
bufferSpecs.emplace_back();
auto shader = glHandler->getShaderPrograms(shaderPaths, true);
glHandler->setProgram(shader.at(0));
//if the height-data is already in the EFTDEM_HEIGHTMAP_BUFFER the coherent-buffer-mask will indicate that and the data does not need to be put on the Buffer
if (!glHandler->getCoherentBufferMask().at(GLHandler::EFTDEM_HEIGHTMAP_BUFFER)){
glHandler->dataToBuffer(GLHandler::EFTDEM_HEIGHTMAP_BUFFER,
(long) (sizeof(GLfloat) * pixelCount),
map->heights.data(), GL_STATIC_DRAW);
}
for (auto i = 0ul; i < shader.size(); i++) {
for (auto spec: bufferSpecs.at(i)) allocBuffer(spec.buffer, long(spec.size));
glHandler->setProgram(shader.at(i));
glUniform2ui(glGetUniformLocation(shader.at(i), "resolution"), map->resolutionX, map->resolutionY);
glUniform1ui(glGetUniformLocation(shader.at(i), "kernelRadius"), kernelRadius);
glHandler->dispatchShader(batchSize, map->resolutionX, map->resolutionY);
GLHandler::waitForShaderStorageIntegrity();
}
// dilation
auto program = glHandler->getShaderPrograms({"gradientRadialDilation.glsl"}, true).at(0);
auto resolutionLocation = gl::glGetUniformLocation(program, "resolution");
auto flippedLocation = gl::glGetUniformLocation(program, "flipped");
glHandler->setProgram(program);
if (!glHandler->getCoherentBufferMask().at(GLHandler::EFTDEM_SECOND_HEIGHTMAP_BUFFER)) {
auto initialState = new std::vector<GLfloat>(map->resolutionX * map->resolutionY, 0);
glHandler->dataToBuffer(GLHandler::EFTDEM_SECOND_HEIGHTMAP_BUFFER,
map->dataSize,
initialState->data(), GL_STATIC_DRAW);
delete initialState;
}
glUniform2ui(resolutionLocation, map->resolutionX, map->resolutionY);
glHandler->dispatchShader(flippedLocation, true, map->resolutionX, map->resolutionY, kernelRadius);
// erosion
program = glHandler->getShaderPrograms({"radialErosion.glsl"}, true).at(0);
resolutionLocation = gl::glGetUniformLocation(program, "resolution");
flippedLocation = gl::glGetUniformLocation(program, "flipped");
glHandler->setProgram(program);
glUniform2ui(resolutionLocation, map->resolutionX, map->resolutionY);
glHandler->dispatchShader(flippedLocation, false, map->resolutionX, map->resolutionY, kernelRadius);
auto end = std::chrono::high_resolution_clock::now();
std::cout << "Elapsed time for closing: " << duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
if (!generateOutput) return emptyHeightMapFromHeightMap(map);
auto filledMap = emptyHeightMapFromHeightMap(map);
glHandler->dataFromBuffer(GLHandler::EFTDEM_HEIGHTMAP_BUFFER,
0, map->dataSize, filledMap->heights.data());
return filledMap;
}