-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructures.cu
258 lines (203 loc) · 7.78 KB
/
structures.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
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "structures.h"
#include "device_functions.h"
#include <cuda_runtime.h>
color_t* bitmap_pixel_at(bitmap_t * bitmap, size_t x, size_t y) {
return bitmap->pixels + bitmap->width * y + x;
}
color_t vec3_to_color(const vec3_t *vec) {
color_t color;
float red, green, blue;
red = vec->x * 255;
green = vec->y * 255;
blue = vec->z * 255;
color.red = red < 0 ? 0 : red > 255 ? 255 : red;
color.green = green < 0 ? 0 : green > 255 ? 255 : green;
color.blue = blue < 0 ? 0 : blue > 255 ? 255 : blue;
return color;
}
vec3_t color_to_vec3(const color_t *color) {
vec3_t v;
v.x = color->red / 255.0f;
v.y = color->green / 255.0f;
v.z = color->blue / 255.0f;
return v;
}
color_t* drawbuffer_get_color_at(drawbuffer_t *buffer, int x, int y) {
return buffer->colorBuffer + buffer->width * y + x;
}
float* drawbuffer_get_zvalue_at(drawbuffer_t *buffer, int x, int y) {
return buffer->zBuffer + buffer->width * y + x;
}
void mesh_set_normals(mesh_t *mesh) {
int i;
for (i = 0; i < mesh->triangleCount; ++i) {
vec3_t vector1, vector2, normal;
ivec3_t *tri = mesh->triangles + i;
vector1 = vec3_subtract(&mesh->vertices[tri->y].location,
&mesh->vertices[tri->x].location);
vector2 = vec3_subtract(&mesh->vertices[tri->z].location,
&mesh->vertices[tri->x].location);
normal = vec3_cross(&vector1, &vector2);
vec3_normalize(&normal);
mesh->vertices[tri->x].normal = vec3_add(&mesh->vertices[tri->x].normal, &normal);
mesh->vertices[tri->y].normal = vec3_add(&mesh->vertices[tri->y].normal, &normal);
mesh->vertices[tri->z].normal = vec3_add(&mesh->vertices[tri->z].normal, &normal);
}
for (i = 0; i < mesh->vertexCount; ++i) {
vertex_t *vert = mesh->vertices + i;
vec3_normalize(&vert->normal);
}
}
void mesh_translate_locations(mesh_t *mesh, mat4_t *mtx) {
vertex_t *vertex;
vertex = mesh->vertices;
for (int i = 0; i < mesh->vertexCount; ++i, ++vertex)
vertex->location = mat4_translate_point(mtx, &vertex->location);
}
void mesh_light_directional(mesh_t *mesh, vec3_t *lightDir, vec3_t *lightColor) {
vertex_t *vertex;
vec3_t diffColor;
*lightDir = vec3_normalized(lightDir);
vertex = mesh->vertices;
for (int i = 0; i < mesh->vertexCount; ++i, ++vertex) {
float df = vec3_dot(&vertex->normal, lightDir);
df = df < 0 ? 0 : df;
diffColor = vec3_scale(lightColor, df);
vertex->color = vec3_add(&diffColor, &vertex->color);
}
}
__device__ void atomicAddf(float* address, float value) {
float old = value;
while ((old = atomicExch(address, atomicExch(address, 0.0f)+old))!=0.0f);
}
__device__ void cuda_vec3_add_atomic(vec3_t *v1, vec3_t *v2) {
atomicAddf(&v1->x, v2->x);
atomicAddf(&v1->y, v2->y);
atomicAddf(&v1->z, v2->z);
}
__device__ vec3_t cuda_vec3_scale(vec3_t *v, float s) {
vec3_t retV;
retV.x = v->x * s;
retV.y = v->y * s;
retV.z = v->z * s;
return retV;
}
__device__ vec3_t cuda_vec3_add(vec3_t *v1, vec3_t *v2) {
vec3_t retV;
retV.x = v1->x + v2->x;
retV.y = v1->y + v2->y;
retV.z = v1->z + v2->z;
return retV;
}
__device__ vec3_t cuda_vec3_subtract(vec3_t *v1, vec3_t *v2) {
vec3_t retV;
retV.x = v1->x - v2->x;
retV.y = v1->y - v2->y;
retV.z = v1->z - v2->z;
return retV;
}
__device__ vec3_t cuda_vec3_cross(vec3_t *v1, vec3_t *v2) {
vec3_t retV;
retV.x = v1->y * v2->z - v1->z * v2->y;
retV.y = v1->z * v2->x - v1->x * v2->z;
retV.z = v1->x * v2->y - v1->y * v2->x;
return retV;
}
__device__ float cuda_vec3_length(vec3_t *v) {
return sqrtf(v->x * v->x + v->y * v->y + v->z * v->z);
}
__device__ float cuda_vec3_dot(vec3_t *v1, vec3_t *v2) {
return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z;
}
__device__ void cuda_vec3_normalize(vec3_t *v) {
float l = cuda_vec3_length(v);
v->x /= l;
v->y /= l;
v->z /= l;
}
__device__ void cuda_mat4_translate_point(mat4_t *m, vec3_t *pt) {
vec3_t newpt;
float w;
newpt.x = pt->x * m->x.x + pt->y * m->y.x + pt->z * m->z.x + m->w.x;
newpt.y = pt->x * m->x.y + pt->y * m->y.y + pt->z * m->z.y + m->w.y;
newpt.z = pt->x * m->x.z + pt->y * m->y.z + pt->z * m->z.z + m->w.z;
w = pt->x * m->x.w + pt->y * m->y.w + pt->z * m->z.w + m->w.w;
pt->x = newpt.x / w;
pt->y = newpt.y / w;
pt->z = newpt.z / w;
}
__global__ void cuda_add_normals(vertex_t *vertices, ivec3_t *triangles, int triCount) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
vec3_t vector1, vector2, normal;
ivec3_t *tri;
if (i < triCount) {
tri = &triangles[i];
vector1 = cuda_vec3_subtract(&vertices[tri->y].location,
&vertices[tri->x].location);
vector2 = cuda_vec3_subtract(&vertices[tri->z].location,
&vertices[tri->x].location);
normal = cuda_vec3_cross(&vector1, &vector2);
cuda_vec3_normalize(&normal);
cuda_vec3_add_atomic(&vertices[tri->x].normal, &normal);
cuda_vec3_add_atomic(&vertices[tri->y].normal, &normal);
cuda_vec3_add_atomic(&vertices[tri->z].normal, &normal);
}
}
__global__ void cuda_normalize_normals(vertex_t *vertices, int vertCount) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
vertex_t *vert;
if (i < vertCount) {
vert = vertices + i;
cuda_vec3_normalize(&vert->normal);
}
}
__global__ void cuda_transform_vertices(vertex_t *vertices, mat4_t *mtx, int vertCount) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
if (i < vertCount) {
cuda_mat4_translate_point(mtx, &vertices[i].location);
}
}
void mesh_set_normals_cuda(mesh_t *mesh) {
int block_size = 16;
int num_blocks = mesh->triangleCount / block_size + (mesh->triangleCount % block_size == 0 ? 0 : 1);
cuda_add_normals <<< num_blocks, block_size >>> (mesh->d_vertices, mesh->d_triangles, mesh->triangleCount);
num_blocks = mesh->vertexCount / block_size + (mesh->vertexCount % block_size == 0 ? 0 : 1);
cuda_normalize_normals <<< num_blocks, block_size >>> (mesh->d_vertices, mesh->vertexCount);
}
void mesh_translate_locations_cuda(mesh_t *mesh, mat4_t *mtx) {
mat4_t *d_mtx;
int block_size = 16;
int num_blocks = mesh->vertexCount / block_size + (mesh->vertexCount % block_size == 0 ? 0 : 1);
cudaMalloc((void **) &d_mtx, sizeof(mat4_t));
cudaMemcpy(d_mtx, mtx, sizeof(mat4_t), cudaMemcpyHostToDevice);
cuda_transform_vertices <<< num_blocks, block_size >>> (mesh->d_vertices, d_mtx, mesh->vertexCount);
cudaFree(d_mtx);
}
__global__ void cuda_light_vertices(vertex_t *vertices, vec3_t *lightDir, vec3_t *lightColor, int vertCount) {
int i = blockIdx.x * blockDim.x + threadIdx.x;
float df;
vec3_t diffColor;
if (i < vertCount) {
df = cuda_vec3_dot(&vertices[i].normal, lightDir);
df = df < 0 ? 0 : df;
diffColor = cuda_vec3_scale(lightColor, df);
vertices[i].color = cuda_vec3_add(&diffColor, &vertices[i].color);
}
}
void mesh_light_directional_cuda(mesh_t *mesh, vec3_t *lightDir, vec3_t *lightColor) {
int block_size = 16;
int num_blocks = mesh->vertexCount / block_size + (mesh->vertexCount % block_size == 0 ? 0 : 1);
vec3_t *d_lightDir;
vec3_t *d_lightColor;
*lightDir = vec3_normalized(lightDir);
cudaMalloc((void **) &d_lightDir, sizeof(vec3_t));
cudaMemcpy(d_lightDir, lightDir, sizeof(vec3_t), cudaMemcpyHostToDevice);
cudaMalloc((void **) &d_lightColor, sizeof(vec3_t));
cudaMemcpy(d_lightColor, lightColor, sizeof(vec3_t), cudaMemcpyHostToDevice);
cuda_light_vertices <<< num_blocks, block_size >>> (mesh->d_vertices, d_lightDir, d_lightColor, mesh->vertexCount);
cudaFree(d_lightDir);
cudaFree(d_lightColor);
}