Panda3D
|
00001 // Filename: tinyGraphicsBuffer.cxx 00002 // Created by: drose (08Aug08) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "pandabase.h" 00016 00017 #include "tinyGraphicsBuffer.h" 00018 #include "config_tinydisplay.h" 00019 #include "tinyGraphicsStateGuardian.h" 00020 #include "pStatTimer.h" 00021 00022 TypeHandle TinyGraphicsBuffer::_type_handle; 00023 00024 //////////////////////////////////////////////////////////////////// 00025 // Function: TinyGraphicsBuffer::Constructor 00026 // Access: Public 00027 // Description: 00028 //////////////////////////////////////////////////////////////////// 00029 TinyGraphicsBuffer:: 00030 TinyGraphicsBuffer(GraphicsEngine *engine, GraphicsPipe *pipe, 00031 const string &name, 00032 const FrameBufferProperties &fb_prop, 00033 const WindowProperties &win_prop, 00034 int flags, 00035 GraphicsStateGuardian *gsg, 00036 GraphicsOutput *host) : 00037 GraphicsBuffer(engine, pipe, name, fb_prop, win_prop, flags, gsg, host) 00038 { 00039 _frame_buffer = NULL; 00040 } 00041 00042 //////////////////////////////////////////////////////////////////// 00043 // Function: TinyGraphicsBuffer::Destructor 00044 // Access: Public, Virtual 00045 // Description: 00046 //////////////////////////////////////////////////////////////////// 00047 TinyGraphicsBuffer:: 00048 ~TinyGraphicsBuffer() { 00049 } 00050 00051 //////////////////////////////////////////////////////////////////// 00052 // Function: TinyGraphicsBuffer::begin_frame 00053 // Access: Public, Virtual 00054 // Description: This function will be called within the draw thread 00055 // before beginning rendering for a given frame. It 00056 // should do whatever setup is required, and return true 00057 // if the frame should be rendered, or false if it 00058 // should be skipped. 00059 //////////////////////////////////////////////////////////////////// 00060 bool TinyGraphicsBuffer:: 00061 begin_frame(FrameMode mode, Thread *current_thread) { 00062 begin_frame_spam(mode); 00063 if (_gsg == (GraphicsStateGuardian *)NULL) { 00064 return false; 00065 } 00066 00067 TinyGraphicsStateGuardian *tinygsg; 00068 DCAST_INTO_R(tinygsg, _gsg, false); 00069 00070 tinygsg->_current_frame_buffer = _frame_buffer; 00071 tinygsg->reset_if_new(); 00072 00073 _gsg->set_current_properties(&get_fb_properties()); 00074 return _gsg->begin_frame(current_thread); 00075 } 00076 00077 //////////////////////////////////////////////////////////////////// 00078 // Function: TinyGraphicsBuffer::end_frame 00079 // Access: Public, Virtual 00080 // Description: This function will be called within the draw thread 00081 // after rendering is completed for a given frame. It 00082 // should do whatever finalization is required. 00083 //////////////////////////////////////////////////////////////////// 00084 void TinyGraphicsBuffer:: 00085 end_frame(FrameMode mode, Thread *current_thread) { 00086 end_frame_spam(mode); 00087 nassertv(_gsg != (GraphicsStateGuardian *)NULL); 00088 00089 if (mode == FM_render) { 00090 // end_render_texture(); 00091 copy_to_textures(); 00092 } 00093 00094 _gsg->end_frame(current_thread); 00095 00096 if (mode == FM_render) { 00097 trigger_flip(); 00098 clear_cube_map_selection(); 00099 } 00100 } 00101 00102 //////////////////////////////////////////////////////////////////// 00103 // Function: TinyGraphicsBuffer::close_buffer 00104 // Access: Protected, Virtual 00105 // Description: Closes the buffer right now. Called from the buffer 00106 // thread. 00107 //////////////////////////////////////////////////////////////////// 00108 void TinyGraphicsBuffer:: 00109 close_buffer() { 00110 if (_gsg != (GraphicsStateGuardian *)NULL) { 00111 TinyGraphicsStateGuardian *tinygsg; 00112 DCAST_INTO_V(tinygsg, _gsg); 00113 tinygsg->_current_frame_buffer = NULL; 00114 _gsg.clear(); 00115 } 00116 00117 _is_valid = false; 00118 } 00119 00120 //////////////////////////////////////////////////////////////////// 00121 // Function: TinyGraphicsBuffer::open_buffer 00122 // Access: Protected, Virtual 00123 // Description: Opens the buffer right now. Called from the buffer 00124 // thread. Returns true if the buffer is successfully 00125 // opened, or false if there was a problem. 00126 //////////////////////////////////////////////////////////////////// 00127 bool TinyGraphicsBuffer:: 00128 open_buffer() { 00129 // GSG Creation/Initialization 00130 TinyGraphicsStateGuardian *tinygsg; 00131 if (_gsg == 0) { 00132 // There is no old gsg. Create a new one. 00133 tinygsg = new TinyGraphicsStateGuardian(_engine, _pipe, NULL); 00134 _gsg = tinygsg; 00135 } else { 00136 DCAST_INTO_R(tinygsg, _gsg, false); 00137 } 00138 00139 create_frame_buffer(); 00140 if (_frame_buffer == NULL) { 00141 tinydisplay_cat.error() 00142 << "Could not create frame buffer.\n"; 00143 return false; 00144 } 00145 00146 tinygsg->_current_frame_buffer = _frame_buffer; 00147 00148 tinygsg->reset_if_new(); 00149 if (!tinygsg->is_valid()) { 00150 close_buffer(); 00151 return false; 00152 } 00153 00154 _is_valid = true; 00155 return true; 00156 } 00157 00158 //////////////////////////////////////////////////////////////////// 00159 // Function: TinyGraphicsBuffer::create_frame_buffer 00160 // Access: Private 00161 // Description: Creates a suitable frame buffer for the current 00162 // window size. 00163 //////////////////////////////////////////////////////////////////// 00164 void TinyGraphicsBuffer:: 00165 create_frame_buffer() { 00166 if (_frame_buffer != NULL) { 00167 ZB_close(_frame_buffer); 00168 _frame_buffer = NULL; 00169 } 00170 00171 _frame_buffer = ZB_open(get_fb_x_size(), get_fb_y_size(), ZB_MODE_RGBA, 0, 0, 0, 0); 00172 } 00173