-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebcl.html
148 lines (132 loc) · 4.38 KB
/
webcl.html
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
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>game of life</title>
<link rel='stylesheet' href='css/style.css' />
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script type='text/javascript' src='js/drawingtable.js'></script>
<script type='text/javascript' src='js/gol.js'></script>
<script id="clgameOfLife" type="text/x-opencl">
__kernel void clGol(__global unsigned int* vectorIn, __global unsigned int* vectorOut, unsigned int vectorWidth, unsigned int vectorSize)
{
unsigned int index = get_global_id(0);
if (index >= (vectorSize)){
return;
}
unsigned int x = floor((float)(index/vectorWidth));
unsigned int y = index - (x * vectorWidth);
unsigned int liveCells = 0;
unsigned int targetX = 0;
unsigned int targetY = 0;
//FIND NEIGHBORS AND COUNT LIVES
//TOP
if ((index-vectorWidth >= 0 && index-vectorWidth <= vectorSize) && vectorIn[index-vectorWidth] == 1){
liveCells ++;
}
//TOP LEFT
if((index-vectorWidth)-1 >= 0 && (index-vectorWidth)-1 <= vectorSize){
targetX = floor((float)((index-vectorWidth-1)/vectorWidth));
targetY = (index-vectorWidth-1) - (targetX * vectorWidth);
if ((targetX < x && targetY < y) && vectorIn[(index-vectorWidth)-1] == 1){
liveCells ++;
}
}
//TOP RIGHT
if((index-vectorWidth)+1 >= 0 && (index-vectorWidth)+1 <= vectorSize){
targetX = floor((float)((index-vectorWidth+1)/vectorWidth));
targetY = (index-vectorWidth+1) - (targetX * vectorWidth);
if ((targetX < x && targetY > y) && vectorIn[(index-vectorWidth)+1] == 1){
liveCells ++;
}
}
//LEFT
if(index-1 >= 0 && index-1 <=vectorSize){
targetX = floor((float)((index-1)/vectorWidth));
if ((targetX == x) && vectorIn[index-1] == 1){
liveCells ++;
}
}
//RIGHT
if(index+1 >= 0 && index+1 <=vectorSize){
targetX = floor((float)((index+1)/vectorWidth));
if ((targetX == x) && vectorIn[index+1] == 1){
liveCells ++;
}
}
// BOTTOM
if ((index+vectorWidth >= 0 && index+vectorWidth <= vectorSize) && vectorIn[index+vectorWidth] == 1){
liveCells ++;
}
//BOTTOM LEFT
if((index+vectorWidth)-1 >= 0 && (index+vectorWidth)-1 <= vectorSize){
targetX = floor((float)(((index+vectorWidth)-1)/vectorWidth));
targetY = ((index+vectorWidth)-1) - (targetX * vectorWidth);
if ((targetX > x && targetY < y) && vectorIn[(index+vectorWidth)-1] == 1){
liveCells ++;
}
}
//BOTTOM RIGHT
if((index+vectorWidth)+1 >= 0 && (index+vectorWidth)+1 <= vectorSize){
targetX = floor((float)(((index+vectorWidth)+1)/vectorWidth));
targetY = ((index+vectorWidth)+1) - (targetX * vectorWidth);
if ((targetX > x && targetY > y) && vectorIn[(index+vectorWidth)+1] == 1){
liveCells ++;
}
}
//GAME RULES
if (vectorIn[index] == 1 && liveCells < 2) //Qualquer célula viva com menos de dois vizinhos vivos morre de solidão.
vectorOut[index] = 0;
else if (vectorIn[index] == 1 && liveCells > 3) //Qualquer célula viva com mais de três vizinhos vivos morre de superpopulação.
vectorOut[index] = 0;
else if (vectorIn[index] == 0 && liveCells == 3) //Qualquer célula morta com exatamente três vizinhos vivos se torna uma célula viva.
vectorOut[index] = 1;
else
vectorOut[index] = vectorIn[index];
}
</script>
<script type='text/javascript'>
$("#drawing-table").ready(function(){
$("#start").click(function() {
document.world.startParallel();
});
$("#stop").click(function() {
document.world.stop();
});
});
</script>
</head>
<body>
<div id="page-wrap">
<div id="controls">
<fieldset id="grid-size-wrap">
<legend>Grid Controls</legend>
<select id="gridSize">
<option value="5,5" selected>5 x 5</option>
<option value="10,10">10 x 10</option>
<option value="20,20" >20 x 20</option>
</select>
<br />
Changing size clears the design
</fieldset>
<fieldset id="controls-wrap">
<legend>Controls</legend>
<button id="start">Start</button>
<button id="stop">Pause</button>
<button id="clear">Clear</button>
</fieldset>
<fieldset id="information-wrap">
<legend>Running Information</legend>
Generation: <a id="generation">0</a> | Live cells: <a id="liveCells">0</a>
</fieldset>
</div>
<div id="table-wrap">
<table id="drawing-table">
<tr>
<td></td>
</tr>
</table>
</div>
</div>
</body>
</html>