|
| 1 | +-- part of OpenGLAda, (c) 2022 Felix Krause |
| 2 | +-- released under the terms of the MIT license, see the file "COPYING" |
| 3 | + |
| 4 | +with Ada.Text_IO; |
| 5 | + |
| 6 | +with GL.Compute; |
| 7 | +with GL.Files; |
| 8 | +with GL.Memory_Barriers; |
| 9 | +with GL.Objects.Buffers; |
| 10 | +with GL.Objects.Shaders; |
| 11 | +with GL.Objects.Programs; |
| 12 | +with GL.Types; |
| 13 | + |
| 14 | +with GL_Test.Display_Backend; |
| 15 | + |
| 16 | +procedure GL_Test.Compute is |
| 17 | + use GL.Types; |
| 18 | + |
| 19 | + procedure Load_Singles is new GL.Objects.Buffers.Load_To_Buffer |
| 20 | + (Single_Pointers); |
| 21 | + |
| 22 | + procedure Map_Singles is new GL.Objects.Buffers.Map |
| 23 | + (Single_Pointers); |
| 24 | + |
| 25 | + Data_Count : constant := 1024; |
| 26 | + |
| 27 | + Iteration_Count : Natural := 0; |
| 28 | + |
| 29 | + Buffer : GL.Objects.Buffers.Buffer; |
| 30 | + Invalid : GL.Objects.Buffers.Buffer; |
| 31 | + Compute_Shader : GL.Objects.Shaders.Shader |
| 32 | + (Kind => GL.Objects.Shaders.Compute_Shader); |
| 33 | + Program : GL.Objects.Programs.Program; |
| 34 | +begin |
| 35 | + Display_Backend.Init; |
| 36 | + Display_Backend.Open_Window (Width => 500, Height => 500); |
| 37 | + |
| 38 | + Buffer.Initialize_Id; |
| 39 | + Invalid.Initialize_Id; |
| 40 | + Compute_Shader.Initialize_Id; |
| 41 | + Program.Initialize_Id; |
| 42 | + |
| 43 | + -- initialize an SSBO with numbers from 1 to `Data_Count` |
| 44 | + declare |
| 45 | + use GL.Objects.Buffers; |
| 46 | + |
| 47 | + Values : Single_Array (1 .. Data_Count); |
| 48 | + begin |
| 49 | + for I In Values'Range loop |
| 50 | + Values (I) := Single (I); |
| 51 | + end loop; |
| 52 | + Shader_Storage_Buffer.Bind (Buffer); |
| 53 | + Load_Singles |
| 54 | + (Shader_Storage_Buffer, |
| 55 | + Values, |
| 56 | + Dynamic_Draw); |
| 57 | + -- The following dummy bind is necessary for the Bind_Buffer_Base |
| 58 | + -- to be effective. TODO: determine if it's a bug or not. |
| 59 | + Shader_Storage_Buffer.Bind (Invalid); |
| 60 | + Shader_Storage_Buffer.Bind_Buffer_Base (0, Buffer); |
| 61 | + end; |
| 62 | + |
| 63 | + -- load shader sources and compile shaders |
| 64 | + GL.Files.Load_Shader_Source_From_File |
| 65 | + (Compute_Shader, "../src/gl/gl_test-compute-shader.glsl"); |
| 66 | + |
| 67 | + Compute_Shader.Compile; |
| 68 | + |
| 69 | + if not Compute_Shader.Compile_Status then |
| 70 | + Ada.Text_IO.Put_Line ("Compilation of compute shader failed. log:"); |
| 71 | + Ada.Text_IO.Put_Line (Compute_Shader.Info_Log); |
| 72 | + end if; |
| 73 | + |
| 74 | + -- set up program |
| 75 | + Program.Attach (Compute_Shader); |
| 76 | + Program.Link; |
| 77 | + if not Program.Link_Status then |
| 78 | + Ada.Text_IO.Put_Line ("Program linking failed. Log:"); |
| 79 | + Ada.Text_IO.Put_Line (Program.Info_Log); |
| 80 | + return; |
| 81 | + end if; |
| 82 | + |
| 83 | + -- check various constant |
| 84 | + declare |
| 85 | + use GL.Compute; |
| 86 | + begin |
| 87 | + for Index in Index_Type loop |
| 88 | + Ada.Text_IO.Put ("Max compute work group count " & Index'Image & " :"); |
| 89 | + Ada.Text_IO.Put_Line (Int'Image (Max_Compute_Work_Group_Count (Index))); |
| 90 | + end loop; |
| 91 | + |
| 92 | + for Index in Index_Type loop |
| 93 | + Ada.Text_IO.Put ("Max compute work group size " & Index'Image & " :"); |
| 94 | + Ada.Text_IO.Put_Line (Int'Image (Max_Compute_Work_Group_Size (Index))); |
| 95 | + end loop; |
| 96 | + |
| 97 | + Ada.Text_IO.Put ("Max compute work group invocations :"); |
| 98 | + Ada.Text_IO.Put_Line (Int'Image (Max_Compute_Work_Group_Invocations)); |
| 99 | + end; |
| 100 | + |
| 101 | + Program.Use_Program; |
| 102 | + |
| 103 | + -- dispatch compute shader 5 times |
| 104 | + while Iteration_Count < 5 loop |
| 105 | + -- local size for x axis is 32 as specified in the shader |
| 106 | + GL.Compute.Dispatch_Compute (Data_Count / 32, 1, 1); |
| 107 | + GL.Memory_Barriers.Memory_Barrier |
| 108 | + ((Shader_Storage => True, others => False)); |
| 109 | + Iteration_Count := Iteration_Count + 1; |
| 110 | + end loop; |
| 111 | + |
| 112 | + -- print the final result to compare to the expected value |
| 113 | + declare |
| 114 | + use GL.Objects; |
| 115 | + use GL.Objects.Buffers; |
| 116 | + |
| 117 | + CPU_Total : Single := 0.0; |
| 118 | + GPU_Total : Single := 0.0; |
| 119 | + |
| 120 | + Value_Ptr : Single_Pointers.Pointer; |
| 121 | + begin |
| 122 | + Map_Singles (Shader_Storage_Buffer, Read_Only, Value_Ptr); |
| 123 | + |
| 124 | + for I in 1 .. Data_Count loop |
| 125 | + -- We run 5 iterations of the compute shader which simply doubles its |
| 126 | + -- previous value, so this is ultimately the same as multiplying the |
| 127 | + -- initial value by 2 ** 5 = 32. |
| 128 | + CPU_Total := CPU_Total + Single (I) * 32.0; |
| 129 | + GPU_Total := GPU_Total + Value_Ptr.all; |
| 130 | + Single_Pointers.Increment (Value_Ptr); |
| 131 | + end loop; |
| 132 | + |
| 133 | + Unmap (Shader_Storage_Buffer); |
| 134 | + |
| 135 | + Ada.Text_IO.Put_Line ("CPU Total : " & CPU_Total'Image); |
| 136 | + Ada.Text_IO.Put_Line ("GPU Total : " & GPU_Total'Image); |
| 137 | + end; |
| 138 | + |
| 139 | + Display_Backend.Shutdown; |
| 140 | +end GL_Test.Compute; |
0 commit comments