-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformKernel.cl
63 lines (60 loc) · 2.59 KB
/
transformKernel.cl
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
const char *kernelSource =
"void matrixByVector(__global const float* matrix, __private float* vector){"
"float x = vector[0];"
"float y = vector[1];"
"float z = vector[2];"
"float w = vector[3];"
"vector[0] = matrix[0] * x + matrix[1] * y + matrix[2] * z + matrix[3] * w;"
"vector[1] = matrix[4] * x + matrix[5] * y + matrix[6] * z + matrix[7] * w;"
"vector[2] = matrix[8] * x + matrix[9] * y + matrix[10] * z + matrix[11] * w;"
"vector[3] = matrix[12] * x + matrix[13] * y + matrix[14] * z + matrix[15] * w;"
"}"
"float dotProduct(__private float* vec1, __private float* vec2){"
"return vec1[0]*vec2[0] + vec1[1]*vec2[1] + vec1[2]*vec2[2];"
"}"
"int RGBClamp(float val){"
"if(val < 0) return 0;"
"if(val > 255) return 255;"
"return (int)val;"
"}"
"__kernel void transforms(__global const float* vertices,"
"__global const float* colors,"
"__global const float* normals,"
"__global const float* matrices,"
"__global float* outputVertices,"
"__global float* outputColors,"
"const int matrixCount,"
"const int height,"
"const int width) { "
"int globalIndex = get_global_id(0) * 3;"
"float vertex[4];"
"float normal[4];"
"for(int i = 0; i < 3; i++){ vertex[i] = vertices[globalIndex + i]; }"
"for(int i = 0; i < 3; i++){ normal[i] = normals[globalIndex + i]; }"
"vertex[3] = 1.0;"
"normal[3] = 1.0;"
"//NORMAL TRANSFORMS"
"for(int i = 0; i < 64; i += 16){"
" matrixByVector(&matrices[i], normal);"
"}"
"//VERTEX TRANSFORMS"
"int matrixBufferLength = matrixCount * 16;"
"for(int i = 0; i < matrixBufferLength; i += 16){"
" matrixByVector(&matrices[i], vertex);"
"}"
"float hardcodedLight[3] = {0.0, 0.0, 1.0};"
"float lightScalar = dotProduct(normal, hardcodedLight);"
"outputColors[globalIndex] = RGBClamp(colors[globalIndex] * lightScalar);"
"outputColors[globalIndex + 1] = RGBClamp(colors[globalIndex + 1] * lightScalar);"
"outputColors[globalIndex + 2] = RGBClamp(colors[globalIndex + 2] * lightScalar);"
"//perspective division"
"if(vertex[3] >= 1.0){"
" vertex[0] = vertex[0] / vertex[3];"
" vertex[1] = vertex[1] / vertex[3];"
"}"
"//screen space conversion"
"vertex[0] *= width;"
"vertex[1] *= height;"
"vertex[2] *= height;"
"for(int i = 0; i < 3; i++) { outputVertices[globalIndex + i] = vertex[i]; }"
"}";