
    \
jO                     ^   d Z ddlZddlZddlZddlT ej        d         Zdaee	dfdZ
ee	dfdZ G d	 d
          Z G d d          Z G d dee          Z G d de          Z G d dee          Z G d d          Z G d de          Z G d de          Z G d de          ZdS )a  Byte abstractions of Vertex Buffer Objects and vertex arrays.

Use :py:func:`create_buffer` or :py:func:`create_mappable_buffer` to create a
Vertex Buffer Object, or a vertex array if VBOs are not supported by the
current context.

Buffers can optionally be created "mappable" (incorporating the
:py:class:`AbstractMappable` mix-in).  In this case the buffer provides a
:py:meth:`~AbstractMappable.get_region` method which provides the most
efficient path for updating partial data within the buffer.
    N)*graphics_vboFTc                     ddl m} |r?t                              dd          r$t          r|j        j        st          | ||          S t          |           S )a  Create a buffer of vertex data.

    :Parameters:
        `size` : int
            Size of the buffer, in bytes
        `target` : int
            OpenGL target buffer
        `usage` : int
            OpenGL usage constant
        `vbo` : bool
            True if a `VertexBufferObject` should be created if the driver
            supports it; otherwise only a `VertexArray` is created.

    :rtype: `AbstractBuffer`
    r   gl      )	pygletr   gl_infohave_version_enable_vbocurrent_context_workaround_vboVertexBufferObjectVertexArraysizetargetusagevbor   s        V/DATA/AppData/hermes/venv/lib/python3.11/site-packages/pyglet/graphics/vertexbuffer.pycreate_bufferr   ?   sp       !Q""!! .! "$6664       c                     ddl m} |r?t                              dd          r$t          r|j        j        st          | ||          S t          |           S )a  Create a mappable buffer of vertex data.

    :Parameters:
        `size` : int
            Size of the buffer, in bytes
        `target` : int
            OpenGL target buffer
        `usage` : int
            OpenGL usage constant
        `vbo` : bool
            True if a :py:class:`VertexBufferObject` should be created if the driver
            supports it; otherwise only a :py:class:`VertexArray` is created.

    :rtype: :py:class:`AbstractBuffer` with :py:class:`AbstractMappable`
    r   r   r   r	   )	r
   r   r   r   r   r   r   MappableVertexBufferObjectr   r   s        r   create_mappable_bufferr   Y   sp       !Q""!! .! *$>>>4   r   c                   L    e Zd ZdZdZdZd Zd Zd Zd Z	ddZ
d	 Zd
 Zd ZdS )AbstractBuffera  Abstract buffer of byte data.

    :Ivariables:
        `size` : int
            Size of buffer, in bytes
        `ptr` : int
            Memory offset of the buffer, as used by the ``glVertexPointer``
            family of functions
        `target` : int
            OpenGL buffer target, for example ``GL_ARRAY_BUFFER``
        `usage` : int
            OpenGL buffer usage, for example ``GL_DYNAMIC_DRAW``

    r   c                      t          d          )z&Bind this buffer to its OpenGL target.abstractNotImplementedErrorselfs    r   bindzAbstractBuffer.bind       !*---r   c                      t          d          )z!Reset the buffer's OpenGL target.r    r!   r#   s    r   unbindzAbstractBuffer.unbind   r&   r   c                      t          d          )zSet the entire contents of the buffer.

        :Parameters:
            `data` : sequence of int or ctypes pointer
                The byte array to set.

        r    r!   r$   datas     r   set_datazAbstractBuffer.set_data   s     "*---r   c                      t          d          )a5  Set part of the buffer contents.

        :Parameters:
            `data` : sequence of int or ctypes pointer
                The byte array of data to set
            `start` : int
                Offset to start replacing data
            `length` : int
                Length of region to replace

        r    r!   r$   r+   startlengths       r   set_data_regionzAbstractBuffer.set_data_region   s     "*---r   Fc                      t          d          )a  Map the entire buffer into system memory.

        The mapped region must be subsequently unmapped with `unmap` before
        performing any other operations on the buffer.

        :Parameters:
            `invalidate` : bool
                If True, the initial contents of the mapped block need not
                reflect the actual contents of the buffer.

        :rtype: ``POINTER(ctypes.c_ubyte)``
        :return: Pointer to the mapped block in memory
        r    r!   r$   
invalidates     r   mapzAbstractBuffer.map   s     "*---r   c                      t          d          )z'Unmap a previously mapped memory block.r    r!   r#   s    r   unmapzAbstractBuffer.unmap   r&   r   c                     dS )zResize the buffer to a new size.

        :Parameters:
            `size` : int
                New size of the buffer, in bytes

        N r$   r   s     r   resizezAbstractBuffer.resize   s      r   c                      t          d          )z3Delete this buffer, reducing system resource usage.r    r!   r#   s    r   deletezAbstractBuffer.delete   r&   r   NF)__name__
__module____qualname____doc__ptrr   r%   r(   r,   r1   r5   r7   r;   r=   r9   r   r   r   r   s   s          CD. . .. . .. . .. . .. . . . . . .  . . . . .r   r   c                       e Zd Zd ZdS )AbstractMappablec                      t          d          )a  Map a region of the buffer into a ctypes array of the desired
        type.  This region does not need to be unmapped, but will become
        invalid if the buffer is resized.

        Note that although a pointer type is required, an array is mapped.
        For example::

            get_region(0, ctypes.sizeof(c_int) * 20, ctypes.POINTER(c_int * 20))

        will map bytes 0 to 80 of the buffer to an array of 20 ints.

        Changes to the array may not be recognised until the region's
        :py:meth:`AbstractBufferRegion.invalidate` method is called.

        :Parameters:
            `start` : int
                Offset into the buffer to map from, in bytes
            `size` : int
                Size of the buffer region to map, in bytes
            `ptr_type` : ctypes pointer type
                Pointer type describing the array format to create

        :rtype: :py:class:`AbstractBufferRegion`
        r    r!   )r$   r/   r   ptr_types       r   
get_regionzAbstractMappable.get_region   s    2 "*---r   N)r?   r@   rA   rH   r9   r   r   rE   rE      s#        . . . . .r   rE   c                   P    e Zd ZdZd Zd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd ZdS )r   a  A ctypes implementation of a vertex array.

    Many of the methods on this class are effectively no-op's, such as
    :py:meth:`bind`, :py:meth:`unbind`, :py:meth:`map`, :py:meth:`unmap` and
    :py:meth:`delete`; they exist in order to present
    a consistent interface with :py:class:`VertexBufferObject`.

    This buffer type is also mappable, and so :py:meth:`get_region` can be used.
    c                     || _         t          j        |z              | _        t          j        | j        t          j                  j        | _        d S N)r   ctypesc_bytearraycastc_void_pvaluerC   r:   s     r   __init__zVertexArray.__init__   s;    	md*--
;tz6?;;Ar   c                     d S rK   r9   r#   s    r   r%   zVertexArray.bind       r   c                     d S rK   r9   r#   s    r   r(   zVertexArray.unbind   rT   r   c                 F    t          j        | j        || j                   d S rK   )rL   memmoverC   r   r*   s     r   r,   zVertexArray.set_data   s     txty11111r   c                 B    t          j        | j        |z   ||           d S rK   )rL   rW   rC   r.   s       r   r1   zVertexArray.set_data_region   s#    tx%'v66666r   Fc                     | j         S rK   rN   r3   s     r   r5   zVertexArray.map  s
    zr   c                     d S rK   r9   r#   s    r   r7   zVertexArray.unmap  rT   r   c                 d    t          j        | j        |z   |          j        }t	          |          S rK   )rL   rO   rC   contentsVertexArrayRegionr$   r/   r   rG   rN   s        r   rH   zVertexArray.get_region  s+    DHu,h77@ '''r   c                     d S rK   r9   r#   s    r   r=   zVertexArray.delete  rT   r   c                    t          j        |z              }t          j        || j        t	          || j                             || _        || _        t          j        | j        t           j                  j        | _	        d S rK   )
rL   rM   rW   rN   minr   rO   rP   rQ   rC   )r$   r   rN   s      r   r;   zVertexArray.resize  sb    %((udj#dDI*>*>???	
;tz6?;;Ar   Nr>   )r?   r@   rA   rB   rR   r%   r(   r,   r1   r5   r7   rH   r=   r;   r9   r   r   r   r      s         B B B    2 2 27 7 7     ( ( (  B B B B Br   r   c                   P    e Zd ZdZd Zd Zd Zd Zd ZddZ	d	 Z
d
 Zd Zd ZdS )r   a  Lightweight representation of an OpenGL VBO.

    The data in the buffer is not replicated in any system memory (unless it
    is done so by the video driver).  While this can improve memory usage and
    possibly performance, updates to the buffer are relatively slow.

    This class does not implement :py:class:`AbstractMappable`, and so has no
    :py:meth:`~AbstractMappable.get_region` method.  See 
    :py:class:`MappableVertexBufferObject` for a VBO class
    that does implement :py:meth:`~AbstractMappable.get_region`.
    c                    || _         || _        || _        t          j        j        | _        t                      }t          d|           |j	        | _
        t          t                     t          || j
                   t          || j         d | j                   t                       t          j        j        j        rdad S d S )Nr   T)r   r   r   r
   r   r   _contextGLuintglGenBuffersrQ   idglPushClientAttribGL_CLIENT_VERTEX_ARRAY_BITglBindBufferglBufferDataglPopClientAttrib_workaround_vbo_finish)r$   r   r   r   rh   s        r   rR   zVertexBufferObject.__init__$  s    	
	1XXQ(5666VTW%%%VTYdj999 9$; 	*%)"""	* 	*r   c                 :    t          | j        | j                   d S rK   )rk   r   rh   r#   s    r   r%   zVertexBufferObject.bind6  s    T[$'*****r   c                 0    t          | j        d           d S Nr   )rk   r   r#   s    r   r(   zVertexBufferObject.unbind9  s    T[!$$$$$r   c                     t          t                     t          | j        | j                   t          | j        | j        || j                   t                       d S rK   )	ri   rj   rk   r   rh   rl   r   r   rm   r*   s     r   r,   zVertexBufferObject.set_data<  sQ    5666T[$'***T[$)T4:>>>r   c                     t          t                     t          | j        | j                   t          | j        |||           t                       d S rK   )ri   rj   rk   r   rh   glBufferSubDatarm   r.   s       r   r1   z"VertexBufferObject.set_data_regionB  sM    5666T[$'***UFD999r   Fc                 p   t          t                     t          | j        | j                   |r!t          | j        | j        d | j                   t          j	        t          | j        t                    t          j        t          j        | j        z                      j        }t                       |S rK   )ri   rj   rk   r   rh   rl   r   r   rL   rO   glMapBufferGL_WRITE_ONLYPOINTERrM   r]   rm   )r$   r4   rC   s      r   r5   zVertexBufferObject.mapH  s    5666T[$'*** 	CditzBBBk+dk=AA .)BCCE EEM 	
r   c                 r    t          t                     t          | j                   t	                       d S rK   )ri   rj   glUnmapBufferr   rm   r#   s    r   r7   zVertexBufferObject.unmapR  s3    5666dk"""r   c                 h    	 | j         !| j                            | j                    d S d S #  Y d S xY wrK   )rh   re   delete_bufferr#   s    r   __del__zVertexBufferObject.__del__W  sE    	w"++DG44444 #"	DDs   &, 1c                 \    t          | j                  }t          d|           d | _        d S )Nr   )rf   rh   glDeleteBuffers)r$   rh   s     r   r=   zVertexBufferObject.delete^  s*    DG__2r   c                    t          j        |z              }t          t                     t	          | j        | j                   t          | j        t                    }t          j	        ||t          || j                             t          | j                   || _        t          | j        | j        || j                   t                       d S rK   )rL   rM   ri   rj   rk   r   rh   rv   GL_READ_ONLYrW   rb   r   rz   rl   r   rm   )r$   r   tempr+   s       r   r;   zVertexBufferObject.resizec  s    $''5666T[$'***4;55tT3tTY#7#7888dk"""	T[$)T4:>>>r   Nr>   )r?   r@   rA   rB   rR   r%   r(   r,   r1   r5   r7   r}   r=   r;   r9   r   r   r   r     s        
 
* * *$+ + +% % %         
    
    r   r   c                   V     e Zd ZdZ fdZ fdZ fdZd ZddZd Z	d	 Z
d
 Z xZS )r   a  A VBO with system-memory backed store.

    Updates to the data via :py:meth:`set_data`, :py:meth:`set_data_region` and
    :py:meth:`map` will be held in local memory until :py:meth:`bind` is
    called.  The advantage is that fewer OpenGL calls are needed, increasing
    performance.

    There may also be less performance penalty for resizing this buffer.

    Updates to data via :py:meth:`map` are committed immediately.
    c                    t          t          |                               |||           t          j        |z              | _        t          j        | j        t          j                  j        | _	        t          j        | _        d| _        d S rq   )superr   rR   rL   rM   r+   rO   rP   rQ   data_ptrsysmaxsize
_dirty_min
_dirty_max)r$   r   r   r   	__class__s       r   rR   z#MappableVertexBufferObject.__init__  sh    ($//88vuMMM]T),,	DIv??E+r   c                 j   t          t          |                                            | j        | j        z
  }|dk    ru|| j        k    r't          | j        | j        | j        | j	                   n)t          | j        | j        || j        | j        z              t          j        | _        d| _        d S d S rq   )r   r   r%   r   r   r   rl   r   r+   r   rt   r   r   r   )r$   r   r   s     r   r%   zMappableVertexBufferObject.bind  s    ($//446660!88ty  T[$)TY
KKKKT_d $ ?A A A!kDODOOO 8r   c                     t          t          |                               |           t          j        | j        || j                   d| _        | j        | _        d S rq   )	r   r   r,   rL   rW   r+   r   r   r   )r$   r+   r   s     r   r,   z#MappableVertexBufferObject.set_data  sN    ($//88>>>ty$	222)r   c                     t          j        | j        |z   ||           t          || j                  | _        t          ||z   | j                  | _        d S rK   )rL   rW   r   rb   r   maxr   r.   s       r   r1   z*MappableVertexBufferObject.set_data_region  sK    t}u,dF;;;eT_55efndo>>r   Fc                 6    d| _         | j        | _        | j        S rq   )r   r   r   r+   r3   s     r   r5   zMappableVertexBufferObject.map  s    )yr   c                     d S rK   r9   r#   s    r   r7   z MappableVertexBufferObject.unmap  rT   r   c                 p    t          j        | j        |z   |          j        }t	          | |||z   |          S rK   )rL   rO   r   r]   VertexBufferObjectRegionr_   s        r   rH   z%MappableVertexBufferObject.get_region  s5    DME18<<E'eUT\5IIIr   c                    t          j        |z              }t          j        || j        t	          || j                             || _        t          j        | j        t           j                  j        | _	        || _        t          t                     t          | j        | j                   t          | j        | j        | j        | j                   t#                       t$          j        | _        d| _        d S rq   )rL   rM   rW   r+   rb   r   rO   rP   rQ   r   ri   rj   rk   r   rh   rl   r   rm   r   r   r   r   )r$   r   r+   s      r   r;   z!MappableVertexBufferObject.resize  s    $''tTYD$)(<(<===	DIv??E	5666T[$'***T[$)TY
CCC+r   r>   )r?   r@   rA   rB   rR   r%   r,   r1   r5   r7   rH   r;   __classcell__)r   s   @r   r   r   r  s        
 
             $ $ $ $ $? ? ?
   
  J J J      r   r   c                       e Zd ZdZd ZdS )AbstractBufferRegiona  A mapped region of a buffer.

    Buffer regions are obtained using :py:meth:`~AbstractMappable.get_region`.

    :Ivariables:
        `array` : ctypes array
            Array of data, of the type and count requested by
            :py:meth:`~AbstractMappable.get_region`.

    c                     dS )zMark this region as changed.

        The buffer may not be updated with the latest contents of the
        array until this method is called.  (However, it may not be updated
        until the next time the buffer is used, for efficiency).
        Nr9   r#   s    r   r4   zAbstractBufferRegion.invalidate  s	     	r   N)r?   r@   rA   rB   r4   r9   r   r   r   r     s-        	 	    r   r   c                       e Zd ZdZd Zd ZdS )r   zA mapped region of a VBO.c                 >    || _         || _        || _        || _        d S rK   )bufferr/   endrN   )r$   r   r/   r   rN   s        r   rR   z!VertexBufferObjectRegion.__init__  s"    



r   c                     | j         }t          |j        | j                  |_        t	          |j        | j                  |_        d S rK   )r   rb   r   r/   r   r   r   )r$   r   s     r   r4   z#VertexBufferObjectRegion.invalidate  s=     14:>> 148<<r   N)r?   r@   rA   rB   rR   r4   r9   r   r   r   r     s8        ##  = = = = =r   r   c                       e Zd ZdZd ZdS )r^   zA mapped region of a vertex array.

    The :py:meth:`~AbstractBufferRegion.invalidate` method is a no-op but is
    provided in order to present a consistent interface with
    :py:meth:`VertexBufferObjectRegion`.
    c                     || _         d S rK   rZ   )r$   rN   s     r   rR   zVertexArrayRegion.__init__  s    


r   N)r?   r@   rA   rB   rR   r9   r   r   r^   r^     s-             r   r^   c                   0    e Zd ZdZd Zd Zd Zd Zd ZdS )IndirectArrayRegionaI  A mapped region in which data elements are not necessarily contiguous.

    This region class is used to wrap buffer regions in which the data
    must be accessed with some stride.  For example, in an interleaved buffer
    this region can be used to access a single interleaved component as if the
    data was contiguous.
    c                 L    || _         || _        || _        || _        | | _        dS )a  Wrap a buffer region.

        Use the `component_count` and `component_stride` parameters to specify
        the data layout of the encapsulated region.  For example, if RGBA
        data is to be accessed as if it were packed RGB, ``component_count``
        would be set to 3 and ``component_stride`` to 4.  If the region
        contains 10 RGBA tuples, the ``size`` parameter is ``3 * 10 = 30``.

        :Parameters:
            `region` : `AbstractBufferRegion`
                The region with interleaved data
            `size` : int
                The number of elements that this region will provide access to.
            `component_count` : int
                The number of elements that are contiguous before some must
                be skipped.
            `component_stride` : int
                The number of elements of interleaved data separating
                the contiguous sections.

        N)regionr   countstriderN   )r$   r   r   component_countcomponent_strides        r   rR   zIndirectArrayRegion.__init__  s+    , 	$
&


r   c                 0    d| j         | j        | j        fz  S )Nz1IndirectArrayRegion(size=%d, count=%d, stride=%d))r   r   r   r#   s    r   __repr__zIndirectArrayRegion.__repr__  s"    BItz4;F0 0 	0r   c                 :   | j         }t          |t                    s'||z  }||z  }| j        j        || j        z  |z            S |j        pd}|j        }|j        pd}|dk     r
| j	        |z   }|| j	        }n|dk     r
| j	        |z   }|dk    s||z  dk    s
J d            ||z  | j        z  ||z  z   }||z  | j        z  ||z  z   }	|| j        z  }
||z  }dg||z
  |z  z  }| j        }t          |          D ]#}| j        j        ||z   |	|z   |
         ||d |<   $|S Nr   r   z(Step must be multiple of component countr   
isinstanceslicer   rN   r   r/   stopstepr   range)r$   indexr   elemjr/   r   r   
data_start	data_stop	data_step
value_steprQ   r   is                  r   __getitem__zIndirectArrayRegion.__getitem__  st   
%'' 	=E>DA;$TDK%7!%;<< qzzQ199I%E<9DDAXX9t#DqyyD5LA---/Y---un3eemC
U]dk1D5L@	4;&	 E\
u-.u 	] 	]A#';#4Z!^IPQMR[5[#\E!-Z-  r   c                 T   | j         }t          |t                    s&||z  }||z  }|| j        j        || j        z  |z   <   d S |j        pd}|j        }|j        pd}|dk     r
| j	        |z   }|| j	        }n|dk     r
| j	        |z   }|dk    s||z  dk    s
J d            ||z  | j        z  ||z  z   }	||z  | j        z  ||z  z   }
|dk    r>| j        }|}t          |          D ]#}||d |         | j        j        |	|z   |
|z   |<   $d S ||z  | j        z  }|| j        j        |	|
|<   d S r   r   )r$   r   rQ   r   r   r   r/   r   r   r   r   r   r   r   s                 r   __setitem__zIndirectArrayRegion.__setitem__4  s   
%'' 	E>DA8=DKdT[0145F qzzQ199I%E<9DDAXX9t#DqyyD5LA---/Y---un3eemC
U]dk1D5L@	199IJ5\\ a aLQRSR_U_R_L`!*q.Qy"HIIa a $+5I@EDKj9<===r   c                 8    | j                                          d S rK   )r   r4   r#   s    r   r4   zIndirectArrayRegion.invalidateT  s         r   N)	r?   r@   rA   rB   rR   r   r   r   r4   r9   r   r   r   r     sn           80 0 0  @F F F@! ! ! ! !r   r   )rB   r   rL   r
   	pyglet.gloptionsr   rn   GL_ARRAY_BUFFERGL_DYNAMIC_DRAWr   r   r   rE   r   r   r   r   r   r^   r   r9   r   r   <module>r      s>  H
 
 


      n^,    /o4 ! ! ! !4 )8TX ! ! ! !4R. R. R. R. R. R. R. R.j. . . . . . . .:/B /B /B /B /B."2 /B /B /BdX X X X X X X XvE E E E E!35E E E EP       ,= = = = =3 = = =	 	 	 	 	, 	 	 	j! j! j! j! j!. j! j! j! j! j!r   