1. ------------------------------------------------------------------------- 
  2. --  GL.Geometry - GL vertex buffer Object 
  3. -- 
  4. --  Copyright (c) Rod Kay 2007 
  5. --  AUSTRALIA 
  6. --  Permission granted to use this software, without any warranty, 
  7. --  for any purpose, provided this copyright note remains attached 
  8. --  and unmodified if sources are distributed further. 
  9. ------------------------------------------------------------------------- 
  10.  
  11. with GL.Errors; 
  12.  
  13. --  with Ada.Numerics.Generic_Elementary_Functions; 
  14. --  with Ada.Text_IO; use Ada.Text_IO; 
  15.  
  16. --  with System; 
  17.  
  18. package body GL.Buffer.general is 
  19.  
  20.    use Element_Pointers; 
  21.  
  22.    function to_gl_Pointer      is new Ada.Unchecked_Conversion (Element_Pointers.Pointer, GL.pointer); 
  23.    function to_element_Pointer is new Ada.Unchecked_Conversion (GL.pointer,               Element_Pointers.Pointer); 
  24.  
  25.    -- vertex buffer object 
  26.    -- 
  27.  
  28.    function To_Buffer (From  : access Element_Array;   Usage  : VBO_Usage) return General_Object is 
  29.  
  30.       use type GL.sizeiPtr; 
  31.  
  32.       new_Buffer  : General_Object; 
  33.  
  34.    begin 
  35.       Verify_Name (new_Buffer); 
  36.       new_Buffer.Length := From'Length; 
  37.  
  38.       Enable (new_Buffer); 
  39.       GL.Buffer_Data (Extract_VBO_Target (new_Buffer),  From.all'Size / 8, 
  40.                       to_gl_Pointer (From.all (From'First)'Access), 
  41.                       Usage); 
  42.       return new_Buffer; 
  43.    end To_Buffer; 
  44.  
  45.    procedure Set (Self          : in out General_Object; 
  46.                   Set_Position  :        Positive := 1; 
  47.                   To            :        Element_Array) is 
  48.  
  49.       use type GL.sizeiPtr; 
  50.  
  51.       new_Vertices         : aliased Element_Array := To; 
  52.       Vertex_Size_in_bits  : constant Natural                 := To (To'First)'Size; 
  53.  
  54.    begin 
  55.       Enable (Self); 
  56.       GL.BufferSubData (Extract_VBO_Target (Self),  offset => GL.intPtr ((Set_Position - 1) * Vertex_Size_in_bits / 8), 
  57.                         size   => new_Vertices'Size / 8, 
  58.                         data   => to_gl_Pointer (new_Vertices (new_Vertices'First)'Unchecked_Access)); 
  59.       GL.Errors.log; 
  60.    end Set; 
  61.  
  62.    function Get (Self : access General_Object) return Element_Array is 
  63.  
  64.       --        use GL.Geometry, GL.Buffer; 
  65.       use GL.Buffer; 
  66.  
  67.       the_Map      : read_only_Map'Class renames Map (Self); 
  68.       the_Vertices : constant Element_Array := Get (the_Map, Index'First, Self.all.Length); 
  69.  
  70.    begin 
  71.       Release (the_Map); 
  72.       return the_Vertices; 
  73.    end Get; 
  74.  
  75.    -- memory Maps 
  76.    -- 
  77.  
  78.    procedure Release (Self : memory_Map) is 
  79.  
  80.       Status  : constant GL_Boolean := UnmapBuffer (Self.vbo_Target); 
  81.  
  82.    begin 
  83.       if Status /= GL_True then 
  84.          raise Corrupt_Buffer; 
  85.       end if; 
  86.    end Release; 
  87.  
  88.    function Get (Self : memory_Map; Get_Position : Index) return Element is 
  89.  
  90.       use Interfaces.C; 
  91.  
  92.       Start : constant Element_Pointers.Pointer := Self.Data + ptrdiff_t (Get_Position - 1); 
  93.  
  94.    begin 
  95.       return Value (Start, 1) (1); 
  96.    end Get; 
  97.  
  98.    function Get (Self         : memory_Map; 
  99.                  Get_Position : Index; 
  100.                  Count        : Positive) return Element_Array is 
  101.  
  102.       use Interfaces.C; 
  103.  
  104.       Start  : constant Element_Pointers.Pointer := Self.Data + ptrdiff_t (Get_Position - 1); 
  105.  
  106.    begin 
  107.       return Value (Start, ptrdiff_t (Count)); 
  108.    end Get; 
  109.  
  110.    procedure Set (Self         :        memory_Map; 
  111.                   Set_Position :        Index; 
  112.                   To           : access Element) is 
  113.  
  114.       --        use GL.Geometry, Element_Pointers, interfaces.C; 
  115.       use Interfaces.C; 
  116.  
  117.    begin 
  118.       Copy_Array (Element_Pointers.Pointer (To),  Self.Data + ptrdiff_t (Set_Position - 1),  1); 
  119.    end Set; 
  120.  
  121.    procedure Set (Self         : memory_Map; 
  122.                   Set_Position : Index; 
  123.                   To           : Element) is 
  124.  
  125.       the_Vertex : aliased Element := To; 
  126.  
  127.    begin 
  128.       Set (Self, Set_Position, To => the_Vertex'Unchecked_Access); 
  129.    end Set; 
  130.  
  131.    -- read - only 
  132.  
  133.    function Map (Self : access General_Object) return read_only_Map'Class is 
  134.  
  135.       --        use GL.Geometry; 
  136.  
  137.       the_Map  : read_only_Map; 
  138.  
  139.    begin 
  140.       Enable (Self.all); 
  141.  
  142.       the_Map.Data := to_element_Pointer (MapBuffer (Extract_VBO_Target (Self.all),  GL.READ_ONLY)); 
  143.       if the_Map.Data = null then 
  144.          raise GL.Buffer.no_platform_Support; 
  145.       end if; 
  146.  
  147.       the_Map.Last       := Index (Self.all.Length); 
  148.       the_Map.vbo_Target := Extract_VBO_Target (Self.all); 
  149.  
  150.       return the_Map; 
  151.    end Map; 
  152.  
  153.    function Get (Self : read_only_Map; Get_Position : Index) return Element is (Get (memory_Map (Self), Get_Position)); 
  154.  
  155.    function Get (Self         : read_only_Map; 
  156.                  Get_Position : Index; 
  157.                  Count        : Positive) return Element_Array is (Get (memory_Map (Self), Get_Position, Count)); 
  158.  
  159.    -- write - only 
  160.  
  161.    function Map (Self : access General_Object) return write_only_Map'Class is 
  162.  
  163.       --        use GL.Geometry; 
  164.  
  165.       the_Map  : write_only_Map; 
  166.  
  167.    begin 
  168.       Enable (Self.all); 
  169.  
  170.       the_Map.Data := to_element_Pointer (MapBuffer (Extract_VBO_Target (Self.all), GL.WRITE_ONLY)); 
  171.       if the_Map.Data = null then 
  172.          raise GL.Buffer.no_platform_Support; 
  173.       end if; 
  174.  
  175.       the_Map.Last       := Index (Self.all.Length); 
  176.       the_Map.vbo_Target := Extract_VBO_Target (Self.all); 
  177.  
  178.       return the_Map; 
  179.    end Map; 
  180.  
  181.    procedure Set (Self         :        write_only_Map; 
  182.                   Set_Position :        Index; 
  183.                   To           : access Element) is 
  184.  
  185.    begin 
  186.       Set (memory_Map (Self), Set_Position, To); 
  187.    end Set; 
  188.  
  189.    procedure Set (Self         : write_only_Map; 
  190.                   Set_Position : Index; 
  191.                   To           : Element) is 
  192.  
  193.    begin 
  194.       Set (memory_Map (Self), Set_Position, To); 
  195.    end Set; 
  196.  
  197.    -- read - write 
  198.  
  199.    function Map (Self  : access General_Object) return read_write_Map'Class is 
  200.  
  201.       --        use GL.Geometry; 
  202.  
  203.       the_Map  : read_write_Map; 
  204.  
  205.    begin 
  206.       Enable (Self.all); 
  207.  
  208.       the_Map.Data := to_element_Pointer (MapBuffer (Extract_VBO_Target (Self.all), GL.READ_WRITE)); 
  209.       if the_Map.Data = null then 
  210.          raise GL.Buffer.no_platform_Support; 
  211.       end if; 
  212.  
  213.       the_Map.Last       := Index (Self.all.Length); 
  214.       the_Map.vbo_Target := Extract_VBO_Target (Self.all); 
  215.  
  216.       return the_Map; 
  217.    end Map; 
  218.  
  219.    function Get (Self : read_write_Map; Get_Position : Index) return Element is (Get (memory_Map (Self), Get_Position)); 
  220.  
  221.    function Get (Self          : read_write_Map; 
  222.                   Get_Position : Index; 
  223.                   Count        : Positive) return Element_Array is (Get (memory_Map (Self), Get_Position, Count)); 
  224.  
  225.    procedure Set (Self         :        read_write_Map; 
  226.                   Set_Position :        Index; 
  227.                   To           : access Element) is 
  228.  
  229.    begin 
  230.       Set (memory_Map (Self), Set_Position, To); 
  231.    end Set; 
  232.  
  233.    procedure Set (Self         : read_write_Map; 
  234.                   Set_Position : Index; 
  235.                   To           : Element) is 
  236.  
  237.    begin 
  238.       Set (memory_Map (Self), Set_Position, To); 
  239.    end Set; 
  240.  
  241. end GL.Buffer.general;