orka_b455160b/orka_opengl/src/gl-objects-buffers.ads

  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
259
260
261
262
263
264
265
266
--  SPDX-License-Identifier: Apache-2.0
--
--  Copyright (c) 2012 Felix Krause <contact@flyx.org>
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.

with Interfaces.C.Pointers;

private with GL.Enums;
private with GL.Low_Level;

package GL.Objects.Buffers is
   pragma Preelaborate;

   function Minimum_Alignment return Types.Size
     with Post => Minimum_Alignment'Result >= 64;
   --  Minimum byte alignment of pointers returned by Map_Range
   --  (at least 64 bytes to support SIMD CPU instructions)

   function Max_Shader_Storage_Buffer_Bindings return Size
     with Post => Max_Shader_Storage_Buffer_Bindings'Result >= 8;
   --  Maximum number of SSBO buffer bindings

   function Max_Shader_Storage_Block_Size return Size
     with Post => Max_Shader_Storage_Block_Size'Result >= 2 ** 27;
   --  Maximum total storage size of an SSBO in bytes

   function Max_Compute_Shader_Storage_Blocks return Size
     with Post => Max_Compute_Shader_Storage_Blocks'Result >= 8;
   --  Maximum number of SSBO blocks in a compute shader

   type Access_Bits is record
      Read              : Boolean := False;
      Write             : Boolean := False;
      Invalidate_Range  : Boolean := False;
      Invalidate_Buffer : Boolean := False;
      Flush_Explicit    : Boolean := False;
      Unsynchronized    : Boolean := False;
      Persistent        : Boolean := False;
      Coherent          : Boolean := False;
   end record
     with Dynamic_Predicate => (Access_Bits.Read or Access_Bits.Write)
       and (if Access_Bits.Flush_Explicit then Access_Bits.Write)
       and (if Access_Bits.Invalidate_Range or Access_Bits.Invalidate_Buffer then
              not Access_Bits.Read);

   type Storage_Bits is record
      Read              : Boolean := False;
      Write             : Boolean := False;
      Persistent        : Boolean := False;
      Coherent          : Boolean := False;
      Dynamic_Storage   : Boolean := False;
      Client_Storage    : Boolean := False;
   end record
     with Dynamic_Predicate => (if Storage_Bits.Coherent then Storage_Bits.Persistent)
       and (if Storage_Bits.Persistent then Storage_Bits.Read or Storage_Bits.Write);

   type Buffer_Target (<>) is tagged limited private;

   type Indexed_Buffer_Target is (Shader_Storage, Uniform);

   function Kind (Target : Buffer_Target) return Indexed_Buffer_Target;

   type Buffer is new GL_Object with private;

   function Allocated (Object : Buffer) return Boolean;

   function Mapped (Object : Buffer) return Boolean;

   procedure Bind (Target : Buffer_Target; Object : Buffer'Class);
   --  Bind the buffer object to the target
   --
   --  The target must not be one of the targets that should be used with
   --  Bind_Base or Bind_Range.

   procedure Bind_Base (Target : Buffer_Target; Object : Buffer'Class; Index : Natural);
   --  Bind the buffer object to the index of the target as well as to
   --  the target itself.
   --
   --  Target must be one of the following:
   --
   --    * Uniform_Buffer
   --    * Shader_Storage_Buffer

   procedure Allocate_Storage
     (Object : in out Buffer;
      Length : Long;
      Kind   : Numeric_Type;
      Flags  : Storage_Bits)
   with Pre  => not Object.Allocated,
        Post => Object.Allocated;
   --  Use this instead of Allocate_And_Load_From_Data if you don't want
   --  to copy any data

   overriding
   procedure Initialize_Id (Object : in out Buffer);

   overriding
   procedure Delete_Id (Object : in out Buffer);

   overriding
   function Identifier (Object : Buffer) return Types.Debug.Identifier is
     (Types.Debug.Buffer);

   procedure Unmap (Object : in out Buffer);

   procedure Invalidate_Data (Object : in out Buffer);

   generic
      with package Pointers is new Interfaces.C.Pointers (<>);
   package Buffer_Pointers is

      subtype Pointer is Pointers.Pointer;

      procedure Bind_Range
        (Target : Buffer_Target;
         Object : Buffer'Class;
         Index  : Natural;
         Offset, Length : Types.Size);
      --  Bind a part of the buffer object to the index of the target as
      --  well as to the target itself
      --
      --  Target must be one of the following:
      --
      --    * Uniform_Buffer
      --    * Shader_Storage_Buffer

      procedure Allocate_And_Load_From_Data
        (Object : in out Buffer;
         Data   : Pointers.Element_Array;
         Flags  : Storage_Bits)
      with Pre  => not Object.Allocated,
           Post => Object.Allocated;

      procedure Map_Range
        (Object : in out Buffer;
         Flags  : Access_Bits;
         Offset, Length : Types.Size;
         Pointer : out Pointers.Pointer)
      with Pre => Object.Allocated and not Object.Mapped and Length > 0;

      function Get_Mapped_Data
        (Pointer : not null Pointers.Pointer;
         Offset, Length : Types.Size) return Pointers.Element_Array
      with Pre  => Length > 0,
           Post => Get_Mapped_Data'Result'Length = Length;

      procedure Set_Mapped_Data
        (Pointer : not null Pointers.Pointer;
         Offset  : Types.Size;
         Data    : Pointers.Element_Array)
      with Pre => Data'Length > 0;

      procedure Set_Mapped_Data
        (Pointer : not null Pointers.Pointer;
         Offset  : Types.Size;
         Value   : Pointers.Element);

      procedure Flush_Buffer_Range (Object : in out Buffer;
                                    Offset, Length : Types.Size);

      procedure Clear_Sub_Data
        (Object : Buffer;
         Kind   : Numeric_Type;
         Offset, Length : Types.Size;
         Data : in out Pointers.Element_Array)
      with Pre => Data'Length <= 4
        and then Kind /= Double_Type
        and then (if Data'Length = 3 then
          Kind not in Byte_Type | UByte_Type | Short_Type | UShort_Type | Half_Type);

      procedure Copy_Sub_Data (Object, Target_Object : Buffer;
                               Read_Offset, Write_Offset, Length : Types.Size);

      procedure Set_Sub_Data (Object : Buffer;
                              Offset : Types.Size;
                              Data   : Pointers.Element_Array);

      procedure Get_Sub_Data (Object : Buffer;
                              Offset : Types.Size;
                              Data   : out Pointers.Element_Array);

      procedure Invalidate_Sub_Data (Object : Buffer;
                                     Offset, Length : Types.Size);

   end Buffer_Pointers;

   --  Array_Buffer, Texture_Buffer,
   --  Copy_Read_Buffer, and Copy_Write_Buffer are no longer needed
   --  since the GL.Objects.* packages use DSA
   --  Transform_Feedback_Buffer replaced by Shader_Storage_Buffer
   Element_Array_Buffer      : aliased constant Buffer_Target;
   Pixel_Pack_Buffer         : aliased constant Buffer_Target;
   Pixel_Unpack_Buffer       : aliased constant Buffer_Target;
   Draw_Indirect_Buffer      : aliased constant Buffer_Target;
   Parameter_Buffer          : aliased constant Buffer_Target;
   Dispatch_Indirect_Buffer  : aliased constant Buffer_Target;
   Query_Buffer              : aliased constant Buffer_Target;

   --  Buffer targets that must be binded to a specific index
   --  (specified in shaders)
   --  Atomic_Counter_Buffer replaced by Shader_Storage_Buffer
   Uniform_Buffer            : aliased constant Buffer_Target;
   Shader_Storage_Buffer     : aliased constant Buffer_Target;

private

   for Access_Bits use record
      Read              at 0 range 0 .. 0;
      Write             at 0 range 1 .. 1;
      Invalidate_Range  at 0 range 2 .. 2;
      Invalidate_Buffer at 0 range 3 .. 3;
      Flush_Explicit    at 0 range 4 .. 4;
      Unsynchronized    at 0 range 5 .. 5;
      Persistent        at 0 range 6 .. 6;
      Coherent          at 0 range 7 .. 7;
   end record;
   for Access_Bits'Size use Low_Level.Bitfield'Size;

   for Storage_Bits use record
      Read              at 0 range 0 .. 0;
      Write             at 0 range 1 .. 1;
      Persistent        at 0 range 6 .. 6;
      Coherent          at 0 range 7 .. 7;
      Dynamic_Storage   at 0 range 8 .. 8;
      Client_Storage    at 0 range 9 .. 9;
   end record;
   for Storage_Bits'Size use Low_Level.Bitfield'Size;

   type Buffer_Target (Kind : Enums.Buffer_Kind) is
     tagged limited null record;

   type Buffer is new GL_Object with record
      Allocated, Mapped : Boolean := False;
   end record;

   Element_Array_Buffer      : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Element_Array_Buffer);
   Pixel_Pack_Buffer         : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Pixel_Pack_Buffer);
   Pixel_Unpack_Buffer       : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Pixel_Unpack_Buffer);
   Uniform_Buffer            : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Uniform_Buffer);
   Draw_Indirect_Buffer      : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Draw_Indirect_Buffer);
   Parameter_Buffer          : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Parameter_Buffer);
   Shader_Storage_Buffer     : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Shader_Storage_Buffer);
   Dispatch_Indirect_Buffer  : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Dispatch_Indirect_Buffer);
   Query_Buffer              : aliased constant Buffer_Target
     := Buffer_Target'(Kind => Enums.Query_Buffer);

end GL.Objects.Buffers;