diff --git a/GgProject.c b/GgProject.c new file mode 100644 index 0000000..fc2efad --- /dev/null +++ b/GgProject.c @@ -0,0 +1,231 @@ +/* +* Auteur : Gérald CALVARIN +* +* Projet Architecture et Accélérateurs Parallèles, partie Pascal Ballet : Godot, GLSL et compute shader ( tout ça pour programmation GPU) +* projet initié le 05/03/2025 +* +* Description : Ce projet va consister à faire un petit jeu de dames +* +* Étapes : 1- faire un damier (écran noir puis noir et blanc puis cases blanches et noires) +* 2- faire des pions (noirs et blancs ) +* 3- faire des déplacements +* 4- faires des actions (manger et avoir une dame) +*/ + + +/* + + correction du prof pour un damier ! + + int case_x=int(floor(x/16.0f)); + int case_y=int(floor(y/16.0f)); + + if((case_x+case_y)%2==0){ + //case blanche + } + else{ + //case noir + } + +*/ + + struct position { + float x; + float y; + }; + + +void main() { + uint x = gl_GlobalInvocationID.x; + uint y = gl_GlobalInvocationID.y; + uint p = x + y * WSX; + + + // définition de 4 variables o,r,g,b pour faciliter la gestion des couleurs + // + définition du fond marron + int opacite =255; + + int r = 0; + int g = 0; + int b = 0; + int i=0; + + //projet principale + #if 1 + { + + //définition de la dimension de chaque case + int dimension = (130/10); + + float cx = (x / dimension) * dimension + dimension / 2; + float cy = (y / dimension) * dimension + dimension / 2; + + + if (((x / dimension) + (y / dimension)) % 2 == 0) { + // Case blanche + + r = 240; + g = 240; + b = 240; + + } + else { + // Case marron + float dx = float(cx) - float(x); + float dy= float(cy) - float(y); + float dist=sqrt(dx*dx +dy*dy); + + //définition des pions noirs + if( (dist<(dimension/2)) && ( (y / dimension) < 3 )){ + + r = 0; + g = 0; + b = 0; + + if( int(mousex / dimension) == int(x / dimension) && int(mousey / dimension) == int(y / dimension) && mouse_button == 1){ + r=255; + g=0; + b=0; + //bouge(); + + } + + } + //définition des pions blanc + else if( (dist<(dimension/2)) && ( (y / dimension) >= 7 ) ){ + + r = 255; + g = 255; + b = 255; + + if( int(mousex / dimension) == int(x / dimension) && int(mousey / dimension) == int(y / dimension) && mouse_button == 1){ + r=255; + g=0; + b=0; + //bouge(); + + } + } + else{ + r = 210; + g = 180; + b = 140; + } + + } + + + + // La scène prend les valeurs orgb définies + data_0[p] = (opacite<<24)+(b<<16)+(g<<8)+r; + + } + #endif + + + + + // base pour les pions + //cercle vert qui suit la sourie sur fond rouge dégradé + #if 0 + { + if(step==0){ + r=255-int(y); + } + + else{ + float dx = float(mousex) - float(x); + float dy= float(mousey) - float(y); + float dist=sqrt(dx*dx +dy*dy); + + if(dist<10){ + g=255; + } + else{ + r=255-int(y); + } + } + // La scène prend les valeurs orgb définies + data_0[p] = (opacite<<24)+(b<<16)+(g<<8)+r; + } + #endif + +} + + + + +//exemples précédents ==> main séparé du projet !!!!! +#if 0 +{ + + int compter_voisin( uint x, uint y){ + return 3; + } + + + void main() { + + uint x = gl_GlobalInvocationID.x; + uint y = gl_GlobalInvocationID.y; + uint p = x + y * WSX; + + int opacite =255; + int r = 0; + int g = 0; + int b = 0; + + // Petite Animation + #if 0 + r = 0+int(x*(y+step)); + g = 0+int(y); + b = 0+int(x); + #endif + + // Division en x ou en y de 2 couleurs fixes + #if 0 + if(y<(128/2)){ + g = g+255; + } + else{ + b = b+255; + } + #endif + + #if 0 + // Initialisation jeu de la vie + if(step==0){ + if(data_0[p]>=0){ + r = 255; + g = 255; + b = 255; + } + data_0[p] = (opacite<<24)+(b<<16)+(g<<8)+r; //0xFF000000+(int(x)<<16) ; // - int(p)*(step+1); + } + else{ //jeu de la vie + + int nb_v=compter_voisin(x, y); + + if((r==0) && (g==0) && (b==00)){ + if(nb_v==3){ + r = 255; + g = 255; + b = 255; + } + else{ + r = 255; + g = 255; + b = 255; + } + } + + } + + + //data_0[p] = (opacite<<24)+(b<<16)+(g<<8)+r; //0xFF000000+(int(x)<<16) ; // - int(p)*(step+1); + //data_1[p] = 0xFFF00FFF ; + //data_1[p] = 0xFF0000AA + int( 1.0 + 99999.9*sin(float(x+float(step+y))/1000.0)); + #endif + } +} +#endif diff --git a/GgProject.tscn b/GgProject.tscn new file mode 100644 index 0000000..b2c21d5 --- /dev/null +++ b/GgProject.tscn @@ -0,0 +1,19 @@ +[gd_scene load_steps=3 format=3 uid="uid://ch5kc8lmik5ef"] + +[ext_resource type="Texture2D" uid="uid://bhxau0hh6k20" path="res://addons/compute_shader_studio/icon.png" id="1_yrdup"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="2_jf41d"] + +[node name="GgProject" type="Node2D"] + +[node name="Icon" type="Sprite2D" parent="."] +position = Vector2(590, 339) +scale = Vector2(2.33865, 1.38843) +texture = ExtResource("1_yrdup") + +[node name="ComputeShaderStudio2D" type="Node" parent="." node_paths=PackedStringArray("data")] +script = ExtResource("2_jf41d") +WSX = 130 +WSY = 130 +glsl_file = "res://GgProject.c" +GLSL_code = "" +data = [NodePath("../Icon")] diff --git a/examples/circles/circles.tscn b/examples/circles/circles.tscn index c66e724..1916a5c 100644 --- a/examples/circles/circles.tscn +++ b/examples/circles/circles.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://c1ux4sgouixua"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_amro2"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_amro2"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_ntl1q"] [node name="Circles" type="Node2D"] diff --git a/examples/example_4.tscn b/examples/example_4.tscn new file mode 100644 index 0000000..153d6db --- /dev/null +++ b/examples/example_4.tscn @@ -0,0 +1,97 @@ +[gd_scene load_steps=3 format=3 uid="uid://bg2ni580ra7pa"] + +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_ke3fj"] +[ext_resource type="Texture2D" uid="uid://2behgeplwycn" path="res://examples/grid_512x512.png" id="2_flqlh"] + +[node name="CompShadStudioEx4" type="Node2D"] + +[node name="Label" type="Label" parent="."] +offset_left = 0.999908 +offset_top = 1.0 +offset_right = 232.0 +offset_bottom = 60.0 +scale = Vector2(1.72, 1.72) +text = "Compute Shader Studio +Example 4: BIG Game Of Life" +horizontal_alignment = 1 +vertical_alignment = 1 + +[node name="ComputeShaderStudio2D2" type="Node" parent="." node_paths=PackedStringArray("data")] +script = ExtResource("1_ke3fj") +pause = true +nb_passes = 2 +WSX = 512 +WSY = 512 +GLSL_code = "// Write your code HERE +#define ALIVE 0xFFFFFFFF +#define DEAD 0xFF0000FF + +void compute_next_step(uint x, uint y, uint p) { + if ( x > 0 && y > 0 && x < WSX-1 && y < WSY - 1) { // in sandbox + uint n = 0 ; // Number of living neighbors + for (uint i = x-1; i <= x+1; i++) { + for (uint j = y-1; j <= y+1; j++) { + uint k = i + j * WSX; + if (k != p && data_0[k] == ALIVE ) + n++; + } + } + if (data_0[p] == DEAD) { + if ( n == 3) + data_1[p] = ALIVE; // Birth + else + data_1[p] = DEAD ; + } else { // ALIVE + if (n <=1 || n >= 4) // Under or over population + data_1[p] = DEAD; + else + data_1[p] = ALIVE; // Survive + } + } +} + +void main() { + uint x = gl_GlobalInvocationID.x; + uint y = gl_GlobalInvocationID.y; + uint p = x + y * WSX; + if ( step == 0 ) { // initialisation ************************ + if ( current_pass == 0 ) { + data_1[p] = DEAD ; + if (data_0[p] < 0 || x==0 || y==0 || x==WSX-1 || y==WSY-1) + data_0[p] = DEAD ; + else + data_0[p] = ALIVE ; + } + } else { // in process ********************************* + if (current_pass == 0) + compute_next_step(x, y, p); + else + data_0[p] = data_1[p]; // The future is now + } +} +" +data = [NodePath("../Grid512x512_0"), null] + +[node name="ButtonStep" type="Button" parent="."] +offset_left = 91.0 +offset_top = 131.0 +offset_right = 182.0 +offset_bottom = 162.0 +scale = Vector2(2.4, 2.4) +text = "Step" + +[node name="ButtonPlay" type="Button" parent="."] +offset_left = 92.0 +offset_top = 219.0 +offset_right = 183.0 +offset_bottom = 250.0 +scale = Vector2(2.4, 2.4) +text = "Play" + +[node name="Grid512x512_0" type="Sprite2D" parent="."] +position = Vector2(762.7, 314.7) +scale = Vector2(1.2, 1.2) +texture = ExtResource("2_flqlh") + +[connection signal="pressed" from="ButtonStep" to="ComputeShaderStudio2D2" method="_on_button_step"] +[connection signal="pressed" from="ButtonPlay" to="ComputeShaderStudio2D2" method="_on_button_play"] diff --git a/examples/simple_circle/simple_circle.tscn b/examples/simple_circle/simple_circle.tscn index b5037a7..0fe95b4 100644 --- a/examples/simple_circle/simple_circle.tscn +++ b/examples/simple_circle/simple_circle.tscn @@ -1,15 +1,12 @@ [gd_scene load_steps=3 format=3 uid="uid://c63rtjh0eurgm"] -[ext_resource type="Script" uid="uid://c8esqdv0y26yp" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_rlapi"] +[ext_resource type="Script" path="res://addons/compute_shader_studio/compute_shader_studio_2d.gd" id="1_rlapi"] [ext_resource type="Texture2D" uid="uid://demftcowdd5c6" path="res://examples/icon.svg" id="2_y7slp"] [node name="SimpleCircle" type="Node2D"] -[node name="ComputeShaderStudio2D" type="Node" parent="." node_paths=PackedStringArray("data")] +[node name="ComputeShaderStudio2D" type="Node" parent="."] script = ExtResource("1_rlapi") -WSX = 256 -glsl_file = "res://examples/simple_circle/simple_circle.cpp" -data = [NodePath("../Icon")] metadata/_custom_type_script = "uid://c8esqdv0y26yp" [node name="Icon" type="Sprite2D" parent="."] diff --git a/project.godot b/project.godot index 1617345..89c5daa 100644 --- a/project.godot +++ b/project.godot @@ -11,10 +11,6 @@ config_version=5 [application] config/name="compute_shader_studio" -run/main_scene="res://examples/example_1.tscn" -config/features=PackedStringArray("4.4", "Forward Plus") +run/main_scene="res://GgProject.tscn" +config/features=PackedStringArray("4.3", "Forward Plus") config/icon="res://addons/compute_shader_studio/icon.png" - -[editor_plugins] - -enabled=PackedStringArray("res://addons/compute_shader_studio/plugin.cfg")