<s> """<STR_LIT>""" <EOL> from __future__ import print_function , division <EOL> from panda3d . core import GraphicsOutput , Texture , AuxBitplaneAttrib , NodePath <EOL> from panda3d . core import Vec4 , TransparencyAttrib , ColorWriteAttrib , SamplerState <EOL> from panda3d . core import WindowProperties , FrameBufferProperties , GraphicsPipe <EOL> from panda3d . core import LVecBase2i <EOL> from rplibs . six . moves import range <EOL> from rplibs . six import iterkeys , itervalues <EOL> from rpcore . globals import Globals <EOL> from rpcore . rpobject import RPObject <EOL> from rpcore . util . post_process_region import PostProcessRegion <EOL> __all__ = "<STR_LIT>" , <EOL> __version__ = "<STR_LIT>" <EOL> class setter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func ) : <EOL> self . __func = func <EOL> self . __doc__ = func . __doc__ <EOL> def __set__ ( self , name , value ) : <EOL> return self . __func ( name , value ) <EOL> class RenderTarget ( RPObject ) : <EOL> """<STR_LIT>""" <EOL> NUM_ALLOCATED_BUFFERS = <NUM_LIT:0> <EOL> USE_R11G11B10 = True <EOL> REGISTERED_TARGETS = [ ] <EOL> CURRENT_SORT = - <NUM_LIT> <EOL> def __init__ ( self , name = "<STR_LIT:target>" ) : <EOL> RPObject . __init__ ( self , name ) <EOL> self . _targets = { } <EOL> self . _color_bits = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . _aux_bits = <NUM_LIT:8> <EOL> self . _aux_count = <NUM_LIT:0> <EOL> self . _depth_bits = <NUM_LIT:0> <EOL> self . _size = LVecBase2i ( - <NUM_LIT:1> ) <EOL> self . _size_constraint = LVecBase2i ( - <NUM_LIT:1> ) <EOL> self . _source_window = Globals . base . win <EOL> self . _source_region = None <EOL> self . _active = False <EOL> self . _internal_buffer = None <EOL> self . sort = None <EOL> self . engine = Globals . base . graphicsEngine <EOL> self . support_transparency = False <EOL> self . create_default_region = True <EOL> for region in Globals . base . win . get_display_regions ( ) : <EOL> region . disable_clears ( ) <EOL> def add_color_attachment ( self , bits = <NUM_LIT:8> , alpha = False ) : <EOL> """<STR_LIT>""" <EOL> self . _targets [ "<STR_LIT>" ] = Texture ( self . debug_name + "<STR_LIT>" ) <EOL> if isinstance ( bits , ( list , tuple ) ) : <EOL> self . _color_bits = ( bits [ <NUM_LIT:0> ] , bits [ <NUM_LIT:1> ] , bits [ <NUM_LIT:2> ] , bits [ <NUM_LIT:3> ] if len ( bits ) == <NUM_LIT:4> else <NUM_LIT:0> ) <EOL> else : <EOL> self . _color_bits = ( ( bits , bits , bits , ( bits if alpha else <NUM_LIT:0> ) ) ) <EOL> def add_depth_attachment ( self , bits = <NUM_LIT:32> ) : <EOL> """<STR_LIT>""" <EOL> self . _targets [ "<STR_LIT>" ] = Texture ( self . debug_name + "<STR_LIT>" ) <EOL> self . _depth_bits = bits <EOL> def add_aux_attachment ( self , bits = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> self . _aux_bits = bits <EOL> self . _aux_count += <NUM_LIT:1> <EOL> def add_aux_attachments ( self , bits = <NUM_LIT:8> , count = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> self . _aux_bits = bits <EOL> self . _aux_count += count <EOL> @ setter <EOL> def size ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . _size_constraint = LVecBase2i ( * args ) <EOL> @ property <EOL> def active ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _active <EOL> @ active . setter <EOL> def active ( self , flag ) : <EOL> """<STR_LIT>""" <EOL> for region in self . _internal_buffer . get_display_regions ( ) : <EOL> region . set_active ( flag ) <EOL> @ property <EOL> def color_tex ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _targets [ "<STR_LIT>" ] <EOL> @ property <EOL> def depth_tex ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _targets [ "<STR_LIT>" ] <EOL> @ property <EOL> def aux_tex ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . _targets [ i ] for i in sorted ( iterkeys ( self . _targets ) ) if i . startswith ( "<STR_LIT>" ) ] <EOL> def set_shader_input ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . create_default_region : <EOL> self . _source_region . set_shader_input ( * args , ** kwargs ) <EOL> @ setter <EOL> def shader ( self , shader_obj ) : <EOL> """<STR_LIT>""" <EOL> if not shader_obj : <EOL> self . error ( "<STR_LIT>" ) <EOL> return <EOL> self . _source_region . set_shader ( shader_obj ) <EOL> @ property <EOL> def internal_buffer ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _internal_buffer <EOL> @ property <EOL> def targets ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _targets <EOL> @ property <EOL> def region ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _source_region <EOL> def prepare_render ( self , camera_np ) : <EOL> """<STR_LIT>""" <EOL> self . create_default_region = False <EOL> self . _create_buffer ( ) <EOL> self . _source_region = self . _internal_buffer . get_display_region ( <NUM_LIT:0> ) <EOL> if camera_np : <EOL> initial_state = NodePath ( "<STR_LIT>" ) <EOL> initial_state . set_state ( camera_np . node ( ) . get_initial_state ( ) ) <EOL> if self . _aux_count : <EOL> initial_state . set_attrib ( AuxBitplaneAttrib . make ( self . _aux_bits ) , <NUM_LIT:20> ) <EOL> initial_state . set_attrib ( TransparencyAttrib . make ( TransparencyAttrib . M_none ) , <NUM_LIT:20> ) <EOL> if max ( self . _color_bits ) == <NUM_LIT:0> : <EOL> initial_state . set_attrib ( ColorWriteAttrib . make ( ColorWriteAttrib . C_off ) , <NUM_LIT:20> ) <EOL> for region in camera_np . node ( ) . get_display_regions ( ) : <EOL> region . set_active ( False ) <EOL> for region in self . _source_window . get_display_regions ( ) : <EOL> if region . get_camera ( ) == camera_np : <EOL> self . _source_window . remove_display_region ( region ) <EOL> camera_np . node ( ) . set_initial_state ( initial_state . get_state ( ) ) <EOL> self . _source_region . set_camera ( camera_np ) <EOL> self . _internal_buffer . disable_clears ( ) <EOL> self . _source_region . disable_clears ( ) <EOL> self . _source_region . set_active ( True ) <EOL> self . _source_region . set_sort ( <NUM_LIT:20> ) <EOL> self . _source_region . set_clear_depth_active ( True ) <EOL> self . _source_region . set_clear_depth ( <NUM_LIT:1.0> ) <EOL> self . _active = True <EOL> def prepare_buffer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _create_buffer ( ) <EOL> self . _active = True <EOL> def present_on_screen ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _source_region = PostProcessRegion . make ( self . _source_window ) <EOL> self . _source_region . set_sort ( <NUM_LIT:5> ) <EOL> def remove ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _internal_buffer . clear_render_textures ( ) <EOL> self . engine . remove_window ( self . _internal_buffer ) <EOL> self . _active = False <EOL> for target in itervalues ( self . _targets ) : <EOL> target . release_all ( ) <EOL> RenderTarget . REGISTERED_TARGETS . remove ( self ) <EOL> def set_clear_color ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . _internal_buffer . set_clear_color_active ( True ) <EOL> self . _internal_buffer . set_clear_color ( Vec4 ( * args ) ) <EOL> @ setter <EOL> def instance_count ( self , count ) : <EOL> """<STR_LIT>""" <EOL> self . _source_region . set_instance_count ( count ) <EOL> def _create_buffer ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _compute_size_from_constraint ( ) <EOL> if not self . _create ( ) : <EOL> self . error ( "<STR_LIT>" ) <EOL> return False <EOL> if self . create_default_region : <EOL> self . _source_region = PostProcessRegion . make ( self . _internal_buffer ) <EOL> if max ( self . _color_bits ) == <NUM_LIT:0> : <EOL> self . _source_region . set_attrib ( ColorWriteAttrib . make ( ColorWriteAttrib . M_none ) , <NUM_LIT:1000> ) <EOL> def _compute_size_from_constraint ( self ) : <EOL> """<STR_LIT>""" <EOL> w , h = Globals . resolution . x , Globals . resolution . y <EOL> self . _size = LVecBase2i ( self . _size_constraint ) <EOL> if self . _size_constraint . x < <NUM_LIT:0> : <EOL> self . _size . x = ( w - self . _size_constraint . x - <NUM_LIT:1> ) // ( - self . _size_constraint . x ) <EOL> if self . _size_constraint . y < <NUM_LIT:0> : <EOL> self . _size . y = ( h - self . _size_constraint . y - <NUM_LIT:1> ) // ( - self . _size_constraint . y ) <EOL> def _setup_textures ( self ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( self . _aux_count ) : <EOL> self . _targets [ "<STR_LIT>" . format ( i ) ] = Texture ( <EOL> self . debug_name + "<STR_LIT>" . format ( i ) ) <EOL> for tex in itervalues ( self . _targets ) : <EOL> tex . set_wrap_u ( SamplerState . WM_clamp ) <EOL> tex . set_wrap_v ( SamplerState . WM_clamp ) <EOL> tex . set_anisotropic_degree ( <NUM_LIT:0> ) <EOL> tex . set_x_size ( self . _size . x ) <EOL> tex . set_y_size ( self . _size . y ) <EOL> tex . set_minfilter ( SamplerState . FT_linear ) <EOL> tex . set_magfilter ( SamplerState . FT_linear ) <EOL> def _make_properties ( self ) : <EOL> """<STR_LIT>""" <EOL> window_props = WindowProperties . size ( self . _size . x , self . _size . y ) <EOL> buffer_props = FrameBufferProperties ( ) <EOL> if self . _color_bits == ( <NUM_LIT:16> , <NUM_LIT:16> , <NUM_LIT:16> , <NUM_LIT:0> ) : <EOL> if RenderTarget . USE_R11G11B10 : <EOL> buffer_props . set_rgba_bits ( <NUM_LIT:11> , <NUM_LIT:11> , <NUM_LIT:10> , <NUM_LIT:0> ) <EOL> else : <EOL> buffer_props . set_rgba_bits ( * self . _color_bits ) <EOL> elif <NUM_LIT:8> in self . _color_bits : <EOL> buffer_props . set_rgba_bits ( * [ i if i != <NUM_LIT:8> else <NUM_LIT:1> for i in self . _color_bits ] ) <EOL> else : <EOL> buffer_props . set_rgba_bits ( * self . _color_bits ) <EOL> buffer_props . set_accum_bits ( <NUM_LIT:0> ) <EOL> buffer_props . set_stencil_bits ( <NUM_LIT:0> ) <EOL> buffer_props . set_back_buffers ( <NUM_LIT:0> ) <EOL> buffer_props . set_coverage_samples ( <NUM_LIT:0> ) <EOL> buffer_props . set_depth_bits ( self . _depth_bits ) <EOL> if self . _depth_bits == <NUM_LIT:32> : <EOL> buffer_props . set_float_depth ( True ) <EOL> buffer_props . set_float_color ( max ( self . _color_bits ) > <NUM_LIT:8> ) <EOL> buffer_props . set_force_hardware ( True ) <EOL> buffer_props . set_multisamples ( <NUM_LIT:0> ) <EOL> buffer_props . set_srgb_color ( False ) <EOL> buffer_props . set_stereo ( False ) <EOL> buffer_props . set_stencil_bits ( <NUM_LIT:0> ) <EOL> if self . _aux_bits == <NUM_LIT:8> : <EOL> buffer_props . set_aux_rgba ( self . _aux_count ) <EOL> elif self . _aux_bits == <NUM_LIT:16> : <EOL> buffer_props . set_aux_hrgba ( self . _aux_count ) <EOL> elif self . _aux_bits == <NUM_LIT:32> : <EOL> buffer_props . set_aux_float ( self . _aux_count ) <EOL> else : <EOL> self . error ( "<STR_LIT>" ) <EOL> return window_props , buffer_props <EOL> def _create ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _setup_textures ( ) <EOL> window_props , buffer_props = self . _make_properties ( ) <EOL> self . _internal_buffer = self . engine . make_output ( <EOL> self . _source_window . get_pipe ( ) , self . debug_name , <NUM_LIT:1> , <EOL> buffer_props , window_props , GraphicsPipe . BF_refuse_window | GraphicsPipe . BF_resizeable , <EOL> self . _source_window . gsg , self . _source_window ) <EOL> if not self . _internal_buffer : <EOL> self . error ( "<STR_LIT>" ) <EOL> return <EOL> if self . _depth_bits : <EOL> self . _internal_buffer . add_render_texture ( <EOL> self . depth_tex , GraphicsOutput . RTM_bind_or_copy , <EOL> GraphicsOutput . RTP_depth ) <EOL> if max ( self . _color_bits ) > <NUM_LIT:0> : <EOL> self . _internal_buffer . add_render_texture ( <EOL> self . color_tex , GraphicsOutput . RTM_bind_or_copy , <EOL> GraphicsOutput . RTP_color ) <EOL> aux_prefix = { <EOL> <NUM_LIT:8> : "<STR_LIT>" , <EOL> <NUM_LIT:16> : "<STR_LIT>" , <EOL> <NUM_LIT:32> : "<STR_LIT>" , <EOL> } [ self . _aux_bits ] <EOL> for i in range ( self . _aux_count ) : <EOL> target_mode = getattr ( GraphicsOutput , aux_prefix . format ( i ) ) <EOL> self . _internal_buffer . add_render_texture ( <EOL> self . aux_tex [ i ] , GraphicsOutput . RTM_bind_or_copy , target_mode ) <EOL> if not self . sort : <EOL> RenderTarget . CURRENT_SORT += <NUM_LIT:20> <EOL> self . sort = RenderTarget . CURRENT_SORT <EOL> RenderTarget . NUM_ALLOCATED_BUFFERS += <NUM_LIT:1> <EOL> self . _internal_buffer . set_sort ( self . sort ) <EOL> self . _internal_buffer . disable_clears ( ) <EOL> self . _internal_buffer . get_display_region ( <NUM_LIT:0> ) . disable_clears ( ) <EOL> self . _internal_buffer . get_overlay_display_region ( ) . disable_clears ( ) <EOL> self . _internal_buffer . get_overlay_display_region ( ) . set_active ( False ) <EOL> RenderTarget . REGISTERED_TARGETS . append ( self ) <EOL> return True <EOL> def consider_resize ( self ) : <EOL> """<STR_LIT>""" <EOL> current_size = LVecBase2i ( self . _size ) <EOL> self . _compute_size_from_constraint ( ) <EOL> if current_size != self . _size : <EOL> if self . _internal_buffer : <EOL> self . _internal_buffer . set_size ( self . _size . x , self . _size . y ) </s>
