forked from kctess5/voxelizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.cu
164 lines (123 loc) · 5.43 KB
/
kernel.cu
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
#include "includes/CompFab.h"
#include "math.h"
#include "curand.h"
#include "curand_kernel.h"
#include "includes/cuda_math.h"
#include <iostream>
#include <string>
#include <sstream>
#include "stdio.h"
#include <vector>
#include "includes/vox3D.cuh"
#include "includes/square2D.cuh"
// check cuda calls for errors
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
if (code != cudaSuccess)
{
fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
if (abort) exit(code);
}
}
// set up random seed buffer
__global__ void setup_kernel ( curandState * state, unsigned long seed )
{
int id = threadIdx.x;
curand_init ( seed, id, 0, &state[id] );
}
// -------------------------------3D voxelizer -------------------------------
__global__ void voxelize_kernel_3D(vox3D voxelizer, curandState* globalState)
{
// find the position of the voxel
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int yIndex = blockDim.y * blockIdx.y + threadIdx.y;
unsigned int zIndex = blockDim.z * blockIdx.z + threadIdx.z;
int3 Index;
Index.x = xIndex;
Index.y = yIndex;
Index.z = zIndex;
if (voxelizer.samples > 0) {
voxelizer.voxelize_kernel_open_mesh(globalState, Index);
} else {
voxelizer.voxelize_kernel(Index);
}
}
// voxelize the given mesh with the given resolution and dimensions
void kernel_wrapper_3D(int samples, int w, int h, int d, int nodeN, CompFab::VoxelGrid *g_voxelGrid, std::vector<CompFab::Triangle> triangles, bool double_thick)
{
int blocksInX = (w+nodeN-1)/nodeN;
int blocksInY = (h+nodeN-1)/nodeN;
int blocksInZ = (d+nodeN-1)/nodeN;
dim3 Dg(blocksInX, blocksInY, blocksInZ);
dim3 Db(nodeN, nodeN, nodeN);
curandState* devStates;
if (samples > 0) {
// set up random numbers
dim3 tpb(RANDOM_SEEDS,1,1);
cudaMalloc ( &devStates, RANDOM_SEEDS*sizeof( curandState ) );
// setup seeds
setup_kernel <<< 1, tpb >>> ( devStates, time(NULL) );
}
// set up boolean array on the GPU
bool *gpu_inside_array;
gpuErrchk( cudaMalloc( (void **)&gpu_inside_array, sizeof(bool) * w * h * d ) );
gpuErrchk( cudaMemcpy( gpu_inside_array, g_voxelGrid->m_insideArray, sizeof(bool) * w * h * d, cudaMemcpyHostToDevice ) );
// set up triangle array on the GPU
CompFab::Triangle* triangle_array = &triangles[0];
CompFab::Triangle* gpu_triangle_array;
gpuErrchk( cudaMalloc( (void **)&gpu_triangle_array, sizeof(CompFab::Triangle) * triangles.size() ) );
gpuErrchk( cudaMemcpy( gpu_triangle_array, triangle_array, sizeof(CompFab::Triangle) * triangles.size(), cudaMemcpyHostToDevice ) );
float3 lower_left = make_float3(g_voxelGrid->m_lowerLeft.m_x, g_voxelGrid->m_lowerLeft.m_y, g_voxelGrid->m_lowerLeft.m_z);
vox3D voxelizer(gpu_inside_array, w, h, d, gpu_triangle_array, triangles.size(), (float) g_voxelGrid->m_spacing, double_thick, lower_left, samples);
voxelize_kernel_3D <<< Dg, Db>>> (voxelizer, devStates);
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
gpuErrchk( cudaMemcpy( g_voxelGrid->m_insideArray, gpu_inside_array, sizeof(bool) * w * h * d, cudaMemcpyDeviceToHost ) );
gpuErrchk( cudaFree(gpu_inside_array) );
gpuErrchk( cudaFree(gpu_triangle_array) );
}
// --------------------------- 2D square grid -------------------------
__global__ void voxelize_kernel_2D(square2D square_grid, curandState* globalState)
{
// find the position of the voxel
unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;
unsigned int yIndex = blockDim.y * blockIdx.y + threadIdx.y;
int2 Index;
Index.x = xIndex;
Index.y = yIndex;
square_grid.voxelize_kernel(Index);
}
// voxelize the given mesh with the given resolution and dimensions
void kernel_wrapper_2D(int samples, int w, int h, CompFab::VoxelGrid *g_voxelGrid, std::vector<CompFab::Triangle> triangles, bool double_thick)
{
int blocksInX = (w+8-1)/8;
int blocksInY = (h+8-1)/8;
dim3 Dg(blocksInX, blocksInY);
dim3 Db(8, 8);
curandState* devStates;
if (samples > 0) {
// set up random numbers
dim3 tpb(RANDOM_SEEDS,1,1);
cudaMalloc ( &devStates, RANDOM_SEEDS*sizeof( curandState ) );
// setup seeds
setup_kernel <<< 1, tpb >>> ( devStates, time(NULL) );
}
// set up boolean array on the GPU
bool *gpu_inside_array;
gpuErrchk( cudaMalloc( (void **)&gpu_inside_array, sizeof(bool) * w * h ) );
gpuErrchk( cudaMemcpy( gpu_inside_array, g_voxelGrid->m_insideArray, sizeof(bool) * w * h, cudaMemcpyHostToDevice ) );
// set up triangle array on the GPU
CompFab::Triangle* triangle_array = &triangles[0];
CompFab::Triangle* gpu_triangle_array;
gpuErrchk( cudaMalloc( (void **)&gpu_triangle_array, sizeof(CompFab::Triangle) * triangles.size() ) );
gpuErrchk( cudaMemcpy( gpu_triangle_array, triangle_array, sizeof(CompFab::Triangle) * triangles.size(), cudaMemcpyHostToDevice ) );
float2 lower_left = make_float2(g_voxelGrid->m_lowerLeft.m_x, g_voxelGrid->m_lowerLeft.m_y);
square2D square_grid(gpu_inside_array, w, h, gpu_triangle_array, triangles.size(), (float) g_voxelGrid->m_spacing, double_thick, lower_left, samples);
voxelize_kernel_2D <<< Dg, Db>>> (square_grid, devStates);
gpuErrchk( cudaPeekAtLastError() );
gpuErrchk( cudaDeviceSynchronize() );
gpuErrchk( cudaMemcpy( g_voxelGrid->m_insideArray, gpu_inside_array, sizeof(bool) * w * h, cudaMemcpyDeviceToHost ) );
gpuErrchk( cudaFree(gpu_inside_array) );
gpuErrchk( cudaFree(gpu_triangle_array) );
}