
    kj!A                         d Z ddlZddlmZmZmZmZ ddlZddlZddl	m
Z
 dZdZdZdZd	Zd
Z G d d          Z G d d          Z G d de          Z G d d          ZdS )z#
Helper classes for loading shader
    N)AnyCallableOptionalUnion)ProgramDescriptionVERTEX_SHADERGEOMETRY_SHADERFRAGMENT_SHADERTESS_CONTROL_SHADERTESS_EVALUATION_SHADERCOMPUTE_SHADERc                   |   e Zd ZdZdefdZedej        fd            Z	e
ded          dededd fd            Ze
	 	 	 	 dded          ded
edee         dee         dee         dee         dd fd            Ze
	 dded          dededd fd            Zdej        fdZdej        fdZdeegef         dd	fdZd	S )ProgramShadersz:Helper class preparing shader source strings for a programmetac                 h    || _         d | _        d | _        d | _        d | _        d | _        d | _        d S N)r   vertex_sourcegeometry_sourcefragment_sourcetess_control_sourcetess_evaluation_sourcecompute_shader_source)selfr   s     Y/home/agentuser/manim-venv/lib/python3.11/site-packages/moderngl_window/opengl/program.py__init__zProgramShaders.__init__   s?    	597;7;;? >B#=A"""    returnc                 (    t          j                    S )zThe moderngl context)moderngl_windowctxr   s    r   r    zProgramShaders.ctx!   s     "$$$r   clssourcec                 0    | |          }t          t          |j        p|j        ||j                  |_        t          |v r.t          t          |j        p|j        ||j                  |_        t          |v r.t          t          |j        p|j
        ||j                  |_        t          |v r.t          t          |j        p|j        ||j                  |_        t          |v r.t          t          |j        p|j        ||j                  |_        |S )z6Initialize a single glsl string containing all shadersdefines)ShaderSourcer   pathvertex_shaderr&   r   r	   geometry_shaderr   r
   fragment_shaderr   r   tess_control_shaderr   r   tess_evaluation_shaderr   )r"   r   r#   instances       r   from_singlezProgramShaders.from_single&   s7   
 3t99!-I++L	"
 "
 "
 f$$'3	1T1	( ( (H$ f$$'3	1T1	( ( (H$ &((+7#	5T5	, , ,H( "V++.:&	8T8	/ / /H+ r   Nr   r   r   r   r   c                     | |          }t          t          |j        p|j        ||j                  |_        |.t          t          |j        p|j        ||j                  |_        |.t          t          |j        p|j
        ||j                  |_        |.t          t          |j        p|j        ||j                  |_        |.t          t          |j        p|j        ||j                  |_        |S )z"Initialize multiple shader stringsr%   )r'   r   r(   r)   r&   r   r	   r*   r   r
   r+   r   r   r,   r   r   r   )r"   r   r   r   r   r   r   r.   s           r   from_separatezProgramShaders.from_separateU   s#    3t99!-I++L	"
 "
 "
 &'3	1T1	( ( (H$ &'3	1T1	( ( (H$ *+7#	5T5#	, , ,H( "-.:&	5T5&	/ / /H+ r    r   c                 |     | |          }t          t          |j        dn|j        ||j                  |_        |S )Nr2   r%   )r'   r   compute_shaderr&   r   )r"   r   r   r.   s       r   r4   zProgramShaders.compute_shader   sL     3t99)5%-BB43F!L	*
 *
 *
& r   c                 l    | j         
J d            | j                            | j         j                  S )Nz%There is not compute_shader to create)r   r    r4   r#   r!   s    r   create_compute_shaderz$ProgramShaders.create_compute_shader   s6    )557^555x&&t'A'HIIIr   c           
          g }| j         
J d            | j        sR| j        r&| j        j        p| j                                        }n%| j        j        p| j                                         }| j                            | j         j        | j        r| j        j        nd| j        r| j        j        nd| j	        r| j	        j        nd| j
        r| j
        j        ndt          |                    }d| j        i|_        |S )zc
        Creates a shader program.

        Returns:
            ModernGL Program instance
        Nz There is no vertex_source to use)r)   r*   r+   r,   r-   varyingsr   )r   r   r   r   r8   find_out_attribsr    programr#   r   r   tupleextra)r   out_attribsr:   s      r   createzProgramShaders.create   s    !--/Q--- # 	Z# Z"i0[D4H4Y4Y4[4[ #i0YD4F4W4W4Y4Y("",3<@<PZT188VZ<@<PZT188VZ373KU(//QU 7;6Q[+22W[;'' # 
 
  +r   load_source_funcc                    | j         r| j                             |           | j        r| j                            |           | j        r| j                            |           | j        r| j                            |           | j        r| j                            |           | j        r| j                            |           dS dS )zResolves ``#include`` preprocessors

        Args:
            load_source_func (func): A function for finding and loading a source
        N)r   handle_includesr   r   r   r   r   )r   r?   s     r   rA   zProgramShaders.handle_includes   s      	A../?@@@ 	C 001ABBB 	C 001ABBB# 	G$445EFFF& 	J'778HIII% 	I&667GHHHHH	I 	Ir   )NNNN)r2   )__name__
__module____qualname____doc__r   r   propertymodernglContextr    classmethodtypestrr/   r   r1   r4   ComputeShaderr6   Programr>   r   r   rA    r   r   r   r      s       DDB/ B B B B %X% % % % X% ,"#,+=,GJ,	, , , [,\ 
 *.)--1042 2"#2 2 2 "#	2
 "#2 &c]2 !)2 
2 2 2 [2h \^
 
"#
+=
VY
	
 
 
 [
Jx'= J J J J"( " " " "HI#0D I I I I I I Ir   r   c                      e Zd ZdZ	 	 	 ddee         dee         dedeeeef                  d	ed
efdZ	e
defd            Ze
defd            Ze
ded          fd            Ze
dee         fd            Ze
dee         fd            Ze
defd            Ze
deeef         fd            Z	 ddeegef         dededdfdZdeeef         ddfdZdee         fdZddZdefdZdS )r'   a  
    Helper class representing a single shader type.

    It ensures the source has the right format, injects ``#define`` pre-processors,
    resolves ``#include`` pre-processors etc.

    A ``ShaderSource`` can be the base/root shader or a source referenced in an ``#include``.
    Nr   Tshader_typenamer#   r&   idrootc                 .   || _         || _        | g| _        || _        || _        |i n|| _        |r|                                }|                    d          | _        | j        rL| j        d         	                    d          s,| 
                                 t          d| j         d          |                     | j                   | j        rA| j                            dd| j         d	           | j                            d
d           dS dS )a:  Create shader source.

        Args:
            shader_type (str):
                A preprocessor name for setting the shader type
            name (str):
                A string (usually the path) so we can give useful error messages to the user
            source (str):
                The raw source for the shader
        Keyword Args:
            id (int):
                The source number. Used when shader consists of multiple sources through includes
            root (bool):
                If this shader source is the root shader (Not an include)
        N
r   z#versionzMissing #version in z-. A version must be defined in the first line   #define z 1   z#line 2)_id_root_source_list_type_name_definesstripsplit_lines
startswithprintShaderErrorapply_definesinsert)r   rP   rQ   r#   r&   rR   rS   s          r   r   zShaderSource.__init__   s.   0 

 !

%o7 	$\\^^Fll4(( : 	dk!n77
CC 	JJLLL`tz```   	4=))) : 	-Kq";TZ";";";<<<Kq),,,,,	- 	-r   r   c                     | j         S )zint: The shader number/id)rY   r!   s    r   rR   zShaderSource.id  s     xr   c                 6    d                     | j                  S )z!str: The source lines as a stringrU   )joinra   r!   s    r   r#   zShaderSource.source  s     yy%%%r   c                     | j         S )z.list[ShaderSource]: List of all shader sources)r[   r!   s    r   source_listzShaderSource.source_list  s       r   c                     | j         S )z#str: a path or name for this shader)r]   r!   s    r   rQ   zShaderSource.name  s     zr   c                     | j         S )z#list[str]: The lines in this shader)ra   r!   s    r   lineszShaderSource.lines#  s     {r   c                 *    t          | j                  S )z.int: Number of lines in this source (stripped))lenra   r!   s    r   
line_countzShaderSource.line_count(  s     4;r   c                     | j         S )z(dict: Defines configured for this shader)r^   r!   s    r   r&   zShaderSource.defines-  s     }r   r?   depth	source_idc           	      f   |dk    rt          d          |}	 t          | j                  D ] \  }}|                                }|                    d          rt          j        d|          }|t          d|           |d         }|dz  } ||          \  }	}
t          d||
| j        |d	
          }
|
	                    ||dz   |           | j
        d|         |
j
        z   | j
        |dz   d         z   | _        | xj        |
j        z  c_        | j        d         j        } ndS )a  Inject includes into the shader source.
        This happens recursively up to a max level in case the users has
        circular includes. We also build up a list of all the included
        sources in the root shader.

        Args:
            load_source_func (func): A function for finding and loading a source
            depth (int): The current include depth (increase by 1 for every call)
        d   zEReaching an include depth of 100. You probably have circular includesTz#includez#include\s+"?([^"]+)Nz/Could not match '#include\s+"?([^"]+)' in line rV   F)r&   rR   rS   )rs   rt   )rd   	enumeratera   r_   rb   researchr'   r^   rA   rn   r[   rk   rR   )r   r?   rs   rt   
current_idnrlinematchr(   _r#   s              r   rA   zShaderSource.handle_includes2  s~    3;;W   
	%dk22  Dzz||??:.. I&=tDDE})WQUWW   !8D!OJ 0 0 6 6IAv) $%"  F **+;519Xb*ccc"&*SbS/FL"@4:bSTfhhCW"WDK%%);;%%!%!22!6!9JE+. 5	r   c                 <   |sdS t          | j                  D ]\  }}|                                }|                    d          rT	 |                                d         }|                    |          }|sad| d| | j        |<   r# t          $ r Y ~w xY wdS )z"Apply the configured define valuesNz#definerV   rW    )rx   ra   r_   rb   r`   getrn   
IndexError)r   r&   r|   r}   rQ   values         r   re   zShaderSource.apply_defines`  s     	F!$+.. 	 	HB::<<Dy)) 	::<<?D#KK--E  ! %>%>%>u%>%>DJrNN!   D		 	s   1B:B
BBc                     g }| j         D ]X}t          j        d|                                          }|r-|                    |                                d                    Y|S )z
        Get all out attributes in the shader source.

        Returns:
            list[str]: List of out attribute names
        z.(layout(.+)\))?(\s+)?(out)(\s+)(\w+)(\s+)(\w+)rw   )rn   ry   r~   r_   appendgroups)r   namesr}   ress       r   r9   zShaderSource.find_out_attribsr  sa     J 	/ 	/D(Ldjjll[[C /SZZ\\"-...r   c                     t          d| j         d           t          | j                  D ]9\  }}t          t	          |                              d           d|            :t          d           dS )z&Print the shader lines (for debugging)z---[ START z ]---   z: z---[ END {self.name} ]---N)rc   rQ   rx   rn   rK   zfill)r   ir}   s      r   rc   zShaderSource.print  s    ,DI,,,--- ,, 	0 	0GAtSVV\\!__....////)*****r   c                 (    d| j          d| j         dS )Nz<ShaderSource:  id=>)rQ   rR   r!   s    r   __repr__zShaderSource.__repr__  s    :::::::r   )Nr   T)r   r   )r   N)rB   rC   rD   rE   r   rK   dictintboolr   rF   rR   r#   listrk   rQ   rn   rq   r&   r   r   rA   re   r9   rc   r   rN   r   r   r'   r'      s|         -10- 0-c]0- sm0- 	0-
 $sCx.)0- 0- 0- 0- 0- 0-d C    X & & & & X& !T.1 ! ! ! X! hsm    X tCy    X  C       X  c3h    X
 XY, , (# 4,=@,QT,	, , , ,\T#s(^     $$s)    + + + +;# ; ; ; ; ; ;r   r'   c                       e Zd ZdZdS )rd   zGeneric shader related errorN)rB   rC   rD   rE   rN   r   r   rd   rd     s        &&&&r   rd   c            	          e Zd ZdZdedej        fdZede	e
         fd            Zedeeef         fd            Zedej        fd            Zd	edeej        ej        ej        ej        ej        f         fd
Zd	ededefdZedefd            Zedej        fd            Zedefd            Zedee
df         fd            Zedefd            Zedefd            Z edefd            Z!de
fdZ"dS )ReloadableProgramzN
    Programs we want to be reloadable must be created with this wrapper.
    r   r:   c                 "    || _         || _        dS )z
        Create a shader using either a file path or a name.

        Args:
            meta: The program meta
            program: The program instance
        N)r:   r   )r   r   r:   s      r   r   zReloadableProgram.__init__  s     			r   r   c                 2    | j         j        p| j         j        S r   )r   r(   r)   r!   s    r   rQ   zReloadableProgram.name  s    y~8!88r   c                     | j         j        S r   )r:   _membersr!   s    r   r   zReloadableProgram._members  s    |$$r   c                     | j         j        S r   )r:   r    r!   s    r   r    zReloadableProgram.ctx  s    |r   keyc                     | j         |         S r   )r:   )r   r   s     r   __getitem__zReloadableProgram.__getitem__  s     |C  r   defaultc                 8    | j                             ||          S r   )r:   r   )r   r   r   s      r   r   zReloadableProgram.get  s    |W---r   c                     | j         j        S r   )r:   r<   r!   s    r   r<   zReloadableProgram.extra  s    |!!r   c                     | j         j        S )zThe ModernGL Program object)r:   mglor!   s    r   r   zReloadableProgram.mglo  s     |  r   c                     | j         j        S )zk
        int: The internal OpenGL object.
        This values is provided for debug purposes only.
        )r:   glor!   s    r   r   zReloadableProgram.glo  s     |r   .c                     | j         j        S )z1
        tuple: The subroutine uniforms.
        )r:   subroutinesr!   s    r   r   zReloadableProgram.subroutines  s    
 |''r   c                     | j         j        S )z
        int: The geometry input primitive.
        The GeometryShader's input primitive if the GeometryShader exists.
        The geometry input primitive will be used for validation.
        )r:   geometry_inputr!   s    r   r   z ReloadableProgram.geometry_input  s     |**r   c                     | j         j        S )z
        int: The geometry output primitive.
        The GeometryShader's output primitive if the GeometryShader exists.
        )r:   geometry_outputr!   s    r   r   z!ReloadableProgram.geometry_output  s     |++r   c                     | j         j        S )zc
        int: The maximum number of vertices that
        the geometry shader will output.
        )r:   geometry_verticesr!   s    r   r   z#ReloadableProgram.geometry_vertices  s     |--r   c                 (    d| j          d| j         dS )Nz<ReloadableProgram: r   r   )rQ   r   r!   s    r   r   zReloadableProgram.__repr__  s    @di@@TX@@@@r   N)#rB   rC   rD   rE   r   rG   rM   r   rF   r   rK   rQ   r   r   r   rH   r    r   UniformUniformBlock
Subroutine	AttributeVaryingr   r   r<   r   r   r   r;   r   r   r   r   r   rN   r   r   r   r     s{        	/ 	(:J 	 	 	 	 9hsm 9 9 9 X9 %$sCx. % % % X%  X%       X 	!	!			
	! 	! 	! 	!.s .S .S . . . . "s " " " X" !h& ! ! ! X!  S       X  (U38_ ( ( ( X( + + + + X+ , , , , X, .3 . . . X.A# A A A A A Ar   r   )rE   ry   typingr   r   r   r   rG   r   moderngl_window.metar   r   r	   r
   r   r   r   r   r'   	Exceptionrd   r   rN   r   r   <module>r      s_    
			 1 1 1 1 1 1 1 1 1 1 1 1      I I I I I I##+ 1 !{I {I {I {I {I {I {I {I|x; x; x; x; x; x; x; x;v' ' ' ' ') ' ' '\A \A \A \A \A \A \A \A \A \Ar   