-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel.cu
More file actions
278 lines (257 loc) · 10.5 KB
/
Copy pathkernel.cu
File metadata and controls
278 lines (257 loc) · 10.5 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <Windows.h>//needed to save output as a bitmap file.
#include <stdio.h>
const char inputpath[] = { "D:\\HGT2\\S44E170.hgt" };//source path for the HGT file
const char outputfile[] = { "D:\\HGT2\\_Output\\NormalMapCUDA.bmp" };//path to dump the output file (normal map)
const short HGT_DIM = 3601;//resolution of HGT files (1 arc-second)
const int NORM_DIM = 3600;//resolution of normal map.
//Note HGT files are conveniently 3601 by 3601 (instead of just 3600 by 3600) so we don't have problems loading adjacent HGT files to get the correct values at the border.
const float NORM_DIM_F = 3600.0f;
const int NormalMapSize = NORM_DIM * NORM_DIM;
const int HGTArraySize = HGT_DIM * HGT_DIM;
//--------------------------------------------------------------------------------//
cudaError_t HGTtoNormalCuda(float3*c, const short*a, unsigned int size, unsigned int normalmapsize);
//--------------------------------------------------------------------------------//
// Kernel Helper functions
__device__ float3 normalize(float3 v)
{
double len = sqrt((float)(v.x * v.x + v.y * v.y + v.z * v.z));
v.x /= len;
v.y /= len;
v.z /= len;
return v;
}
//--------------------------------------------------------------------------------//
//Calculate the normal vector of a triangle
__device__ float3 CalcNormal(float p1x, float p1y, float p1z, float p2x, float p2y, float p2z, float p3x, float p3y, float p3z)
{
long nScale = 30;//approximately 30 meters per point for high resolution HGT files (90 when using the low res HGT format)
p1x = p1x * nScale;
p1y = p1y * nScale;
p2x = p2x * nScale;
p2y = p2y * nScale;
p3x = p3x * nScale;
p3y = p3y * nScale;
float Ax = p2x - p1x;
float Ay = p2y - p1y;
float Az = p2z - p1z;
float Bx = p3x - p1x;
float By = p3y - p1y;
float Bz = p3z - p1z;
float3 n;
n.x = Ay * Bz - Az * By;
n.y = Az * Bx - Ax * Bz;
n.z = Ax * By - Ay * Bx;
n = normalize(n);
return n;
}
//--------------------------------------------------------------------------------//
__device__ float GetHeight(const short* a, int h, int j)
{
int tid = j * HGT_DIM + h;
return (float)a[tid];
}
//--------------------------------------------------------------------------------//
//main Kernel
__global__ void HGTToNormalKernel(float3* pNormal, const short* pHGT, int count)
{
int threadsPerBlock = blockDim.x * blockDim.y * blockDim.z;
int threadPosInBlock = threadIdx.x +
blockDim.x * threadIdx.y +
blockDim.x * blockDim.y * threadIdx.z;
int blockPosInGrid = blockIdx.x +
gridDim.x * blockIdx.y +
gridDim.x * gridDim.y * blockIdx.z;
int tid = blockPosInGrid * threadsPerBlock + threadPosInBlock;//calculate global index to array
if (tid < count)
{
int h = tid % NORM_DIM;
int j = tid / NORM_DIM;
//calculate the normal for the two adjacent triangles in this cell and average them
float3 v3a = CalcNormal(h, j, GetHeight(pHGT, h, j), h + 1, j, GetHeight(pHGT, h + 1, j), h, j + 1, GetHeight(pHGT, h, j + 1));
float3 v3b = CalcNormal(h + 1, j, GetHeight(pHGT, h + 1, j), h + 1, j + 1, GetHeight(pHGT, h + 1, j + 1), h, j + 1, GetHeight(pHGT, h, j + 1) );
float3 vNornmal;
vNornmal.x = (v3a.x + v3b.x) / 2;
vNornmal.y = (v3a.y + v3b.y) / 2;
vNornmal.z = (v3a.z + v3b.z) / 2;
pNormal[tid] = normalize(vNornmal);
}
}
//--------------------------------------------------------------------------------//
//function to save as bitmap (Windows only)
bool SaveBitmapRGB(BYTE* Buffer, int width, int height, long paddedsize, LPCTSTR bmpfile)
{
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER info;
memset(&bmfh, 0, sizeof(BITMAPFILEHEADER));
memset(&info, 0, sizeof(BITMAPINFOHEADER));
bmfh.bfType = 0x4d42;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;
bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize;
bmfh.bfOffBits = 0x36;
info.biSize = sizeof(BITMAPINFOHEADER);
info.biWidth = width;
info.biHeight = height;
info.biPlanes = 1;
info.biBitCount = 24;
info.biCompression = BI_RGB;
info.biSizeImage = 0;
info.biXPelsPerMeter = 0x0ec4;
info.biYPelsPerMeter = 0x0ec4;
info.biClrUsed = 0;
info.biClrImportant = 0;
HANDLE file = CreateFile(bmpfile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (NULL == file)
{
CloseHandle(file);
return false;
}
unsigned long bwritten;
if (WriteFile(file, &bmfh, sizeof(BITMAPFILEHEADER),
&bwritten, NULL) == false)
{
CloseHandle(file);
return false;
}
if (WriteFile(file, &info, sizeof(BITMAPINFOHEADER),
&bwritten, NULL) == false)
{
CloseHandle(file);
return false;
}
if (WriteFile(file, Buffer, paddedsize, &bwritten, NULL) == false)
{
CloseHandle(file);
return false;
}
CloseHandle(file);
return true;
}
//-----------------------------------------------------------------------------------//
int main()
{
//TODO: update following two lines to use std::unique_ptr
short* pHGTData = new short[HGTArraySize]; //stores the HGT data we load form disk
float3* pNormData = new float3[NormalMapSize];//Will contain the normal map data output by CUDA
//load HGT file and reverse the byte order
FILE* pFile = 0;
pFile = fopen(inputpath, "rb");
if (pFile != 0)
{
short i = 0;
while (true)
{
int n = fread((char*)(pHGTData + i * HGT_DIM), sizeof(short), HGT_DIM, pFile);
i++;
if (n == 0) break;
}
fclose(pFile);
//swap bytes around
for (int h = 0; h < HGT_DIM * HGT_DIM; h++)
{
short w = pHGTData[h];
pHGTData[h] = MAKEWORD(HIBYTE(w), LOBYTE(w));
}
//Calculate the normal map using CUDA
cudaError_t cudaStatus = HGTtoNormalCuda(pNormData, pHGTData, HGTArraySize, NormalMapSize);
if (cudaStatus == cudaSuccess) {
printf(" c[0].xyz = {%f,%f,%f}\n", pNormData[0].x, pNormData[1].y, pNormData[2].z);
//save as a bitmap to view the normals. Normals are in Tangent space
BYTE* pBMPData = new BYTE[NORM_DIM * NORM_DIM * 3];
if (pBMPData)
{
for (int h = 0; h < NORM_DIM; h++)
{
for (int j = 0; j < NORM_DIM; j++)
{
//get normal vector and encode into RGB components of the bitmap. Note, normals will be in Tangent space
float3 normal = pNormData[h * NORM_DIM + j];
pBMPData[(NORM_DIM - h - 1) * NORM_DIM * 3 + j * 3 + 0] = 255 * (0.5 + 0.5 * normal.z);
pBMPData[(NORM_DIM - h - 1) * NORM_DIM * 3 + j * 3 + 1] = 255 * (0.5 + 0.5 * -1 * normal.x);//invert green axis
pBMPData[(NORM_DIM - h - 1) * NORM_DIM * 3 + j * 3 + 2] = 255 * (0.5 + 0.5 * normal.y);
//hmmm is red and green reversed?? Had to swap y and x around...
}
}
SaveBitmapRGB(pBMPData, NORM_DIM, NORM_DIM, NORM_DIM * NORM_DIM * 3, outputfile);
delete[] pBMPData;//should use unique_ptr...
}
// cudaDeviceReset must be called before exiting in order for profiling and
// tracing tools such as Nsight and Visual Profiler to show complete traces.
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}
}
else
{
fprintf(stderr, "HGTtoNormalCuda failed!");
}
}
//release memory (yes, should really use unique_ptr...)
delete[] pHGTData;
delete[] pNormData;
return 0;
}
//-----------------------------------------------------------------------------------//
// Helper function for using CUDA to caluclate normal map fro high res HGT file in parallel.
cudaError_t HGTtoNormalCuda(float3 * pNormData, const short* pHGTData, unsigned int size, unsigned int NormalMapSize)
{
short *devHGTData = 0;
float3 *devNormData = 0;
cudaError_t cudaStatus;
// Choose which GPU to run on, change this on a multi-GPU system.
cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
goto Error;
}
// Allocate GPU buffers for three vectors (two input, one output) .
cudaStatus = cudaMalloc((void**)&devNormData, NormalMapSize * sizeof(float3));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
cudaStatus = cudaMalloc((void**)&devHGTData, size * sizeof(short));
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMalloc failed!");
goto Error;
}
// Copy input vectors from host memory to GPU buffers.
cudaStatus = cudaMemcpy(devHGTData, pHGTData, size * sizeof(short), cudaMemcpyHostToDevice);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
// Launch a kernel on the GPU with one thread for each element.
const int count = NORM_DIM * NORM_DIM;
dim3 block(8, 8, 8);
dim3 grid(450, 450);
HGTToNormalKernel<<<grid, block>>>(devNormData, devHGTData, count);
// Check for any errors launching the kernel
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "HGTToNormalKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
goto Error;
}
// cudaDeviceSynchronize waits for the kernel to finish, and returns
// any errors encountered during the launch.
cudaStatus = cudaDeviceSynchronize();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching HGTToNormalKernel!\n", cudaStatus);
goto Error;
}
// Copy output vector from GPU buffer to host memory.
cudaStatus = cudaMemcpy(pNormData, devNormData, NormalMapSize * sizeof(float3), cudaMemcpyDeviceToHost);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaMemcpy failed!");
goto Error;
}
Error:
cudaFree(devNormData);
cudaFree(devHGTData);
return cudaStatus;
}
//-----------------------------------------------------------------------------------//