
    ^j;                       d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	m
Z
mZ ddlmZmZmZ ddlZddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ e	rddl%m&Z& dd	l'm(Z( ee         Z)eZ* G d
 d          Z+ G d de+          Z, G d de,          Z- G d de-          Z. G d de-          Z/ G d de+          Z0dS )a  OpenGL Buffer Objects.

:py:class:`~BufferObject` and a :py:class:`~BackedBufferObject` are provided.
The first is a lightweight abstraction over an OpenGL buffer, as created
with ``glGenBuffers``. The backed buffer object is similar, but provides a
full mirror of the data in CPU memory. This allows for delayed uploading of
changes to GPU memory, which can improve performance is some cases.
    )annotationsN)	lru_cache)TYPE_CHECKINGSequenceType)Array_Pointer_SimpleCData)GL_ARRAY_BUFFERGL_DYNAMIC_DRAWGL_ELEMENT_ARRAY_BUFFERGL_MAP_READ_BITGL_MAP_WRITE_BITGL_MAP_COHERENT_BITGL_MAP_PERSISTENT_BITGL_WRITE_ONLYGLubyteGLuintglBindBufferglBufferDataglBufferStorageglBufferSubDataglDeleteBuffersglGenBuffersglMapBufferglMapBufferRangeglUnmapBuffer)Context)	Attributec                  *   e Zd ZU dZdZded<   dZded<   ej        e	fdd	            Z
ej        dd
            Zej        dd            Zej        dd            Zej        dd            Zej        dd            ZddZej        dd            ZdS )AbstractBufferzAbstract buffer of byte data.

    Attributes:
        size:
            Size of buffer, in bytes
        ptr:
            Memory offset of the buffer, as used by the ``glVertexPointer`` family of functions
    r   intptrsizetargetreturnNonec                    dS )z%Bind this buffer to an OpenGL target.N selfr%   s     W/home/agentuser/manim-venv/lib/python3.11/site-packages/pyglet/graphics/vertexbuffer.pybindzAbstractBuffer.bind?             c                    dS )z!Reset the buffer's OpenGL target.Nr)   r+   s    r,   unbindzAbstractBuffer.unbindC   r.   r/   dataSequence[int] | CTypesPointerc                    dS )zxSet the entire contents of the buffer.

        Args:
            data:
                The byte array to set.

        Nr)   r+   r3   s     r,   set_datazAbstractBuffer.set_dataG   r.   r/   startlengthc                    dS )zSet part of the buffer contents.

        Args:
            data:
                The byte array of data to set
            start:
                Offset to start replacing data
            length:
                Length of region to replace

        Nr)   r+   r3   r8   r9   s       r,   set_data_regionzAbstractBuffer.set_data_regionQ   r.   r/   CTypesPointer[ctypes.c_ubyte]c                    dS )zMap the entire buffer into system memory.

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

        Returns:
            Pointer to the mapped block in memory
        Nr)   r1   s    r,   mapzAbstractBuffer.map_   r.   r/   c                    dS )z'Unmap a previously mapped memory block.Nr)   r1   s    r,   unmapzAbstractBuffer.unmapj   r.   r/   c                    dS )z|Resize the buffer to a new size.

        Args:
            size:
                New size of the buffer, in bytes

        Nr)   r+   r$   s     r,   resizezAbstractBuffer.resizen   r.   r/   c                    dS )z3Delete this buffer, reducing system resource usage.Nr)   r1   s    r,   deletezAbstractBuffer.deletew   r.   r/   Nr%   r"   r&   r'   r&   r'   r3   r4   r&   r'   r3   r4   r8   r"   r9   r"   r&   r'   r&   r=   r$   r"   r&   r'   )__name__
__module____qualname____doc__r#   __annotations__r$   abcabstractmethodr   r-   r2   r7   r<   r?   rA   rD   rF   r)   r/   r,   r!   r!   2   sX          CLLLLDMMMM!0 4 4 4 4 4 	0 0 0 0 	    	    	    	6 6 6 6    	B B B B B Br/   r!   c                      e Zd ZU dZded<   ded<   ded<   ded<   efd#d
Zd$dZefd%dZ	d$dZ
d$dZd&dZd'dZd(dZd)dZd$dZd$dZd$dZd*dZd+d!Zd"S ),BufferObjecta  Lightweight representation of an OpenGL Buffer Object.

    The data in the buffer is not replicated in any system memory (unless it
    is done so by the video driver).  While this can reduce system memory usage,
    performing multiple small updates to the buffer can be relatively slow.
    The target of the buffer is ``GL_ARRAY_BUFFER`` internally to avoid
    accidentally overriding other states when altering the buffer contents.
    The intended target can be set when binding the buffer.
    r"   idr$   usagezContext | None_contextr&   r'   c                D   || _         || _        t          j        j        | _        t                      }t          d|           |j        | _	        t          t          | j	                   t          | j         z              }t          t          | j         || j                   dS )zuInitialize the BufferObject with the given size and draw usage.

        Buffer data is cleared on creation.
           N)r$   rW   pygletglcurrent_contextrX   r   r   valuerV   r   r   r   r   )r+   r$   rW   	buffer_idr3   s        r,   __init__zBufferObject.__init__   s    
 	
	1HH	Q	"""/_dg...$)#&&_ditzBBBBBr/   c                H    t          t          | j        d | j                   d S N)r   r   r$   rW   r1   s    r,   
invalidatezBufferObject.invalidate   s     _ditzBBBBBr/   r%   c                0    t          || j                   d S rb   r   rV   r*   s     r,   r-   zBufferObject.bind       VTW%%%%%r/   c                0    t          t          d           d S Nr   r   r   r1   s    r,   r2   zBufferObject.unbind       _a(((((r/   c                :    t          t          | j                   dS )z@Binds this buffer as an index buffer on the active vertex array.N)r   r   rV   r1   s    r,   bind_to_index_bufferz!BufferObject.bind_to_index_buffer   s    ,dg66666r/   r3   r4   c                |    t          t          | j                   t          t          | j        || j                   d S rb   )r   r   rV   r   r$   rW   r6   s     r,   r7   zBufferObject.set_data   s2    _dg..._ditzBBBBBr/   r8   r9   c                h    t          t          | j                   t          t          |||           d S rb   )r   r   rV   r   r;   s       r,   r<   zBufferObject.set_data_region   s.    _dg...=====r/   CTypesPointer[ctypes.c_byte]c                    t          t          | j                   t          j        t          t          t                    t          j        t          j        | j	        z                      j
        S rb   )r   r   rV   ctypescastr   r   POINTERc_byter$   contentsr1   s    r,   r?   zBufferObject.map   sP    _dg...{;FF!>&-$)*CDDF FFN	Or/   ptr_typetype[CTypesPointer]CTypesPointerc                    t          t          | j                   t          j        t          t          ||t                    |          j        S rb   )r   r   rV   rq   rr   r   r   ru   )r+   r8   r$   rv   s       r,   	map_rangezBufferObject.map_range   s:    _dg...{+OUDJZ[[]effoor/   c                .    t          t                     d S rb   )r   r   r1   s    r,   rA   zBufferObject.unmap   s    o&&&&&r/   c                X    t          dt          | j                             d | _        d S NrZ   r   r   rV   r1   s    r,   rF   zBufferObject.delete   %    6$'??+++r/   c                    | j         A	 | j                            | j                    d | _         d S # t          t          f$ r Y d S w xY wd S rb   )rV   rX   delete_bufferAttributeErrorImportErrorr1   s    r,   __del__zBufferObject.__del__   sa    7++DG444"K0   	 s   &1 AAc                r   t          j        |z              }t          t          | j                   t          t          d| j        t                    }t          j        ||t          || j                             t          t                     || _        t          t          | j        || j                   d S rh   )rq   rt   r   r   rV   r   r$   r   memmoveminr   r   rW   )r+   r$   tempr3   s       r,   rD   zBufferObject.resize   s    $''_dg...DIOOtT3tTY#7#7888o&&&	_ditzBBBBBr/   strc                @    | j         j         d| j         d| j         dS )Nz(id=z, size=))	__class__rM   rV   r$   r1   s    r,   __repr__zBufferObject.__repr__   s*    .)KKtwKKtyKKKKr/   N)r$   r"   rW   r"   r&   r'   rH   rG   rI   rJ   )r&   ro   )r8   r"   r$   r"   rv   rw   r&   rx   rL   )r&   r   )rM   rN   rO   rP   rQ   r   r`   rc   r   r-   r2   rl   r7   r<   r?   rz   rA   rF   r   rD   r   r)   r/   r,   rU   rU   |   s          GGGIIIJJJ/> C C C C C"C C C C "1 & & & & &) ) ) )7 7 7 7C C C C> > > >O O O O
p p p p' ' ' '      
C 
C 
C 
CL L L L L Lr/   rU   c                       e Zd ZU dZded<   ded<   ded<   ded<   ded	<   ded
<   ded<   ded<   efd fdZddZ ed          d d            Z	d!dZ
d"dZd fdZd#dZ xZS )$BackedBufferObjecta,  A buffer with system-memory backed store.

    Updates to the data via ``set_data`` and ``set_data_region`` will be held
    in system memory until ``commit`` is called.  The advantage is that fewer
    OpenGL calls are needed, which can increase performance at the expense of
    system memory.
    CTypesDataTyper3   r"   data_ptr
_dirty_min
_dirty_maxbool_dirtystridecountctyper$   c_typerW   r&   r'   c                Z   t                                          ||           || _        t          j        |          | _        || j        z  } ||z              | _        t          j        | j                  | _        t          j
        | _        d| _        d| _        || _        || _        d S )Nr   F)superr`   r   rq   sizeof_ctypes_sizer3   	addressofr   sysmaxsizer   r   r   r   r   )r+   r$   r   r   r   rW   numberr   s          r,   r`   zBackedBufferObject.__init__   s    u%%%"M&11**$Vf_''	(33+


r/   c                n   | j         sdS t          t          | j                   | j        | j        z
  }|dk    r||| j        k    r't          t          | j        | j        | j	                   n)t          t          | j        || j        | j        z              t          j        | _        d| _        d| _         dS dS )zCommits all saved changes to the underlying buffer before drawing.

        Allows submitting multiple changes at once, rather than having to call glBufferSubData for every change.
        Nr   F)r   r   r   rV   r   r   r$   r   r3   rW   r   r   r   r   rC   s     r,   commitzBackedBufferObject.commit   s    
 { 	F_dg...0!88ty  _diDJOOOO$X\XgHghhh!kDODODKKK 8r/   Nr   r8   Array[CTypesDataType]c                    | j         |z  }| j        |z  }t          j        | j        |z            }t          j        | j        |z   |          j        S rb   )r   r   rq   rs   r   rr   r   ru   r+   r8   r   
byte_startarray_countrv   s         r,   
get_regionzBackedBufferObject.get_region  sL    [5(
j5(>$+";<<{4=:5x@@IIr/   Sequence[float]c                    | j         |z  }| j         |z  |z   }|| j        ||<   | j        |z  }|| j        |z  z   }|| j        k     r|| _        || j        k    r|| _        d| _        d S NT)r   r3   r   r   r   r   )r+   r8   r   r3   array_start	array_endr   byte_ends           r,   
set_regionzBackedBufferObject.set_region  s    j5(J&4	+/	+i'( [5(
e 33''(DOdo%%&DOr/   c                r   |t          j        | j                  z  } | j        |z              }t          j        || j        t          || j                             || _        t          j        |          | _        || _        d| _	        | j        | _
        d| _        | j                                         d S )Nr   T)rq   r   r   r   r3   r   r$   r   r   r   r   r   r   cache_clear)r+   r$   r   r3   s       r,   rD   zBackedBufferObject.resize'  s    t{333$f$''tTYD$)(<(<===	(..	 )##%%%%%r/   c                V    t                                                       d| _        d S r   )r   rc   r   )r+   r   s    r,   rc   zBackedBufferObject.invalidate7  s$    r/   c                    | j         |z  }|| j         |z  z   }|| j        k     r|| _        || j        k    r|| _        d| _        d S r   )r   r   r   r   )r+   r8   r   r   r   s        r,   invalidate_regionz$BackedBufferObject.invalidate_region;  sR    [5(
e 33''(DOdo%%&DOr/   r$   r"   r   r   r   r"   r   r"   rW   r"   r&   r'   rH   )r8   r"   r   r"   r&   r   )r8   r"   r   r"   r3   r   r&   r'   rL   )r8   r"   r   r"   r&   r'   )rM   rN   rO   rP   rQ   r   r`   r   r   r   r   rD   rc   r   __classcell__r   s   @r,   r   r      sC          MMMOOOOOOLLLKKKJJJ .      "       ( YtJ J J J    & & & &             r/   r   c                  $     e Zd ZdZd	 fdZ xZS )
AttributeBufferObjectz3A backed buffer used for Shader Program attributes.r$   r"   	attributer   r&   r'   c                n    t                                          ||j        |j        |j                   d S rb   )r   r`   r   r   r   )r+   r$   r   r   s      r,   r`   zAttributeBufferObject.__init__I  s/    y/1A9?SSSSSr/   )r$   r"   r   r   r&   r'   )rM   rN   rO   rP   r`   r   r   s   @r,   r   r   F  sM        ==T T T T T T T T T Tr/   r   c                  (     e Zd ZdZefd fdZ xZS )IndexedBufferObjectz!A backed buffer used for indices.r$   r"   r   r   r   r   rW   r&   r'   c                R    t                                          |||||           d S rb   )r   r`   )r+   r$   r   r   r   rW   r   s         r,   r`   zIndexedBufferObject.__init__Q  s)    vvue<<<<<r/   r   )rM   rN   rO   rP   r   r`   r   r   s   @r,   r   r   N  sM        ++ .= = = = = = = = = = =r/   r   c                      e Zd ZdZd ZddZddZefdZd Z	ddZ
efdZddZddZ ed          d             Zd Zd Zd Zd Zd ZdS )PersistentBufferObjectzA persistently mapped buffer.

    Available in OpenGL 4.3+ contexts. Persistently mapped buffers
    are mapped one time on creation, and can be updated at any time
    without the need to map or unmap.
    c                |   || _         || _        |j        | _        |j        | _        |j        | _        || _        t          j
        j        | _        t                      }t          d|           |j        | _        t#          t$          | j                   t&          t(          z  t*          z  | _        t/          |z              }t1          t$          ||| j                   ||j        z  }t5          j        |j        |z            }t5          j        t;          t$          d|| j                  |          j        | _        d S NrZ   r   ) r$   r   r   attribute_strider   attribute_countr   attribute_ctypevaor[   r\   r]   rX   r   r   r^   rV   r   r   r   r   r   flagsr   r   element_sizerq   rs   rr   r   ru   r3   )r+   r$   r   r   r_   r3   r   r#   s           r,   r`   zPersistentBufferObject.__init__^  s    	" ) 0((/	1HH	Q	"""/_dg...%(==@SS
$!!tTZ@@@ //nY-677K 0!T4: V VX[\\e			r/   r3   r4   r&   r'   c                     t          d          NzNot yet implementedNotImplementedErrorr6   s     r,   r7   zPersistentBufferObject.set_data{      !"7888r/   r8   r"   r9   c                     t          d          r   r   r;   s       r,   r<   z&PersistentBufferObject.set_data_region~  r   r/   c                0    t          || j                   d S rb   re   r*   s     r,   r-   zPersistentBufferObject.bind  rf   r/   c                0    t          t          d           d S rh   ri   r1   s    r,   r2   zPersistentBufferObject.unbind  rj   r/   r=   c                     t          d          Nz*PersistentBufferObjects are always mapped.r   r1   s    r,   r?   zPersistentBufferObject.map      !"NOOOr/   c                     t          d          r   r   )r+   r8   r$   rv   r   s        r,   rz   z PersistentBufferObject.map_range  r   r/   c                     t          d          )Nz+PersistentBufferObjects cannot be unmapped.r   r1   s    r,   rA   zPersistentBufferObject.unmap  s    !"OPPPr/   c                X    t          dt          | j                             d | _        d S r}   r~   r1   s    r,   rF   zPersistentBufferObject.delete  r   r/   Nr   c                    | j         |z  }| j        |z  }t          j        | j        |z            }t          j        t          j        | j                  |z   |          j        S rb   )	r   r   rq   rs   r   rr   r   r3   ru   r   s         r,   r   z!PersistentBufferObject.get_region  sZ    *U2
*U2>$"6"DEE{6+DI66CXNNWWr/   c                L    | j         |z  }| j         |z  |z   }|| j        ||<   d S rb   )r   r3   )r+   r8   r   r3   r   r   s         r,   r   z!PersistentBufferObject.set_region  s8    *U2(50;>	+/	+i'(((r/   c                   t          |z              }t          j        || j        t	          || j                             t          dt          | j                             t                      }t          d|           |j
        | _        | j                                         |                                  | j                                         | j                            | j                   t#          t$          ||| j                   | j        j        || j        j        z  z  }|                     d|t          j        |          | j                  | _        || _        | j                                         d S r   )r   rq   r   r3   r   r$   r   r   rV   r   r^   r   r-   r   enableset_pointerr#   r   r   r   r   r   rz   rs   r   r   )r+   r$   r   r_   rv   s        r,   rD   zPersistentBufferObject.resize  s0   $!!tTYD$)(<(<===6$'??+++ HH	Q	"""/ 			""48,,, 	tTZ@@@>(DDN4O,OPNN1dFN8,D,DdjQQ		##%%%%%r/   c                    d S rb   r)   r1   s    r,   sub_datazPersistentBufferObject.sub_data      r/   c                    d S rb   r)   r1   s    r,   rc   z!PersistentBufferObject.invalidate  r   r/   c                    d S rb   r)   )r+   r8   r   s      r,   r   z(PersistentBufferObject.invalidate_region  r   r/   rI   rJ   rK   rH   )rM   rN   rO   rP   r`   r7   r<   r   r-   r2   r?   r   rz   rA   rF   r   r   r   rD   r   rc   r   r)   r/   r,   r   r   V  sY        f f f:9 9 9 99 9 9 9 * & & & &) ) )P P P P 6F P P P PQ Q Q Q    YtX X X0 0 0
& & &4        r/   r   )1rP   
__future__r   rR   rq   r   	functoolsr   typingr   r   r   _ctypesr   r	   r
   r[   pyglet.gl.glr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   	pyglet.glr   pyglet.graphics.shaderr   r   rx   r!   rU   r   r   r   r   r)   r/   r,   <module>r      s    # " " " " " 



  



       0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1                                          ,  1!!!!!!000000l#GB GB GB GB GB GB GB GBT[L [L [L [L [L> [L [L [L|i i i i i i i iXT T T T T. T T T= = = = =, = = =o o o o o^ o o o o or/   