glGraphicsBuffer_src.cxx 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file glGraphicsBuffer_src.cxx
  10. * @author jyelon
  11. * @date 2006-01-15
  12. */
  13. #include "depthWriteAttrib.h"
  14. using std::max;
  15. using std::min;
  16. TypeHandle CLP(GraphicsBuffer)::_type_handle;
  17. /**
  18. *
  19. */
  20. CLP(GraphicsBuffer)::
  21. CLP(GraphicsBuffer)(GraphicsEngine *engine, GraphicsPipe *pipe,
  22. const std::string &name,
  23. const FrameBufferProperties &fb_prop,
  24. const WindowProperties &win_prop,
  25. int flags,
  26. GraphicsStateGuardian *gsg,
  27. GraphicsOutput *host) :
  28. GraphicsBuffer(engine, pipe, name, fb_prop, win_prop, flags, gsg, host),
  29. _bind_texture_pcollector(_draw_window_pcollector, "Bind textures"),
  30. _generate_mipmap_pcollector(_draw_window_pcollector, "Generate mipmaps"),
  31. _resolve_multisample_pcollector(_draw_window_pcollector, "Resolve multisamples"),
  32. _requested_multisamples(0),
  33. _requested_coverage_samples(0),
  34. _rb_context(nullptr)
  35. {
  36. // A FBO doesn't have a back buffer.
  37. _draw_buffer_type = RenderBuffer::T_front;
  38. _screenshot_buffer_type = RenderBuffer::T_front;
  39. // Initialize these.
  40. _fbo_multisample = 0;
  41. _initial_clear = true;
  42. _needs_rebuild = true;
  43. _rb_size_x = 0;
  44. _rb_size_y = 0;
  45. _rb_size_z = 0;
  46. for (int i = 0; i < RTP_COUNT; ++i) {
  47. _rb[i] = 0;
  48. _rbm[i] = 0;
  49. }
  50. _rb_data_size_bytes = 0;
  51. _shared_depth_buffer = 0;
  52. _bound_tex_page = -1;
  53. }
  54. /**
  55. *
  56. */
  57. CLP(GraphicsBuffer)::
  58. ~CLP(GraphicsBuffer)() {
  59. // unshare shared depth buffer if any
  60. this->unshare_depth_buffer();
  61. // unshare all buffers that are sharing this object's depth buffer
  62. {
  63. CLP(GraphicsBuffer) *graphics_buffer;
  64. std::list <CLP(GraphicsBuffer) *>::iterator graphics_buffer_iterator;
  65. graphics_buffer_iterator = _shared_depth_buffer_list.begin();
  66. while (graphics_buffer_iterator != _shared_depth_buffer_list.end()) {
  67. graphics_buffer = (*graphics_buffer_iterator);
  68. if (graphics_buffer) {
  69. // this call removes the entry from the list
  70. graphics_buffer->unshare_depth_buffer();
  71. }
  72. graphics_buffer_iterator = _shared_depth_buffer_list.begin();
  73. }
  74. }
  75. }
  76. #ifndef OPENGLES
  77. /**
  78. * Clears the entire framebuffer before rendering, according to the settings
  79. * of get_color_clear_active() and get_depth_clear_active() (inherited from
  80. * DrawableRegion).
  81. *
  82. * This function is called only within the draw thread.
  83. */
  84. void CLP(GraphicsBuffer)::
  85. clear(Thread *current_thread) {
  86. if (!is_any_clear_active()) {
  87. return;
  88. }
  89. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  90. if (glgsg->_glClearBufferfv == nullptr) {
  91. // We can't efficiently clear the buffer. Fall back to the inefficient
  92. // default implementation for now.
  93. GraphicsOutput::clear(current_thread);
  94. return;
  95. }
  96. if (display_cat.is_spam()) {
  97. display_cat.spam()
  98. << "clear(): " << get_type() << " "
  99. << get_name() << " " << (void *)this << "\n";
  100. }
  101. // Disable the scissor test, so we can clear the whole buffer.
  102. glDisable(GL_SCISSOR_TEST);
  103. glgsg->_scissor_enabled = false;
  104. glgsg->_scissor_array.clear();
  105. glgsg->_scissor_attrib_active = false;
  106. if (GLCAT.is_spam()) {
  107. GLCAT.spam()
  108. << "glDisable(GL_SCISSOR_TEST)\n";
  109. }
  110. // Set the buffers into which we'll be indexing with glClearBuffer.
  111. int draw_buffer_type = _draw_buffer_type & _fb_properties.get_buffer_mask();
  112. draw_buffer_type |= _fb_properties.get_aux_mask();
  113. glgsg->_color_write_mask = ColorWriteAttrib::C_all;
  114. glgsg->set_draw_buffer(draw_buffer_type);
  115. int index = 0;
  116. if (_fb_properties.get_color_bits() > 0) {
  117. if (_fb_properties.is_stereo()) {
  118. // Clear both left and right attachments.
  119. if (get_clear_active(RTP_color)) {
  120. LColorf v = LCAST(float, get_clear_value(RTP_color));
  121. glgsg->_glClearBufferfv(GL_COLOR, index, v.get_data());
  122. glgsg->_glClearBufferfv(GL_COLOR, index + 1, v.get_data());
  123. }
  124. index += 2;
  125. } else {
  126. if (get_clear_active(RTP_color)) {
  127. LColorf v = LCAST(float, get_clear_value(RTP_color));
  128. glgsg->_glClearBufferfv(GL_COLOR, index, v.get_data());
  129. }
  130. ++index;
  131. }
  132. }
  133. for (int i = 0; i < _fb_properties.get_aux_rgba(); ++i) {
  134. int layerid = RTP_aux_rgba_0 + i;
  135. if (get_clear_active(layerid)) {
  136. LColorf v = LCAST(float, get_clear_value(layerid));
  137. glgsg->_glClearBufferfv(GL_COLOR, index, v.get_data());
  138. }
  139. ++index;
  140. }
  141. for (int i = 0; i < _fb_properties.get_aux_hrgba(); ++i) {
  142. int layerid = RTP_aux_hrgba_0 + i;
  143. if (get_clear_active(layerid)) {
  144. LColorf v = LCAST(float, get_clear_value(layerid));
  145. glgsg->_glClearBufferfv(GL_COLOR, index, v.get_data());
  146. }
  147. ++index;
  148. }
  149. for (int i = 0; i < _fb_properties.get_aux_float(); ++i) {
  150. int layerid = RTP_aux_float_0 + i;
  151. if (get_clear_active(layerid)) {
  152. LColorf v = LCAST(float, get_clear_value(layerid));
  153. glgsg->_glClearBufferfv(GL_COLOR, index, v.get_data());
  154. }
  155. ++index;
  156. }
  157. if (get_clear_depth_active()) {
  158. glDepthMask(GL_TRUE);
  159. glgsg->_state_mask.clear_bit(DepthWriteAttrib::get_class_slot());
  160. if (get_clear_stencil_active()) {
  161. glStencilMask(~0);
  162. glgsg->_glClearBufferfi(GL_DEPTH_STENCIL, 0, get_clear_depth(), get_clear_stencil());
  163. } else {
  164. GLfloat depth = get_clear_depth();
  165. glgsg->_glClearBufferfv(GL_DEPTH, 0, &depth);
  166. }
  167. } else if (get_clear_stencil_active()) {
  168. GLint stencil = get_clear_stencil();
  169. glgsg->_glClearBufferiv(GL_STENCIL, 0, &stencil);
  170. }
  171. report_my_gl_errors();
  172. }
  173. #endif
  174. /**
  175. * This function will be called within the draw thread before beginning
  176. * rendering for a given frame. It should do whatever setup is required, and
  177. * return true if the frame should be rendered, or false if it should be
  178. * skipped.
  179. */
  180. bool CLP(GraphicsBuffer)::
  181. begin_frame(FrameMode mode, Thread *current_thread) {
  182. begin_frame_spam(mode);
  183. check_host_valid();
  184. _bound_tex_page = -1;
  185. if (!_is_valid) {
  186. if (GLCAT.is_debug()) {
  187. GLCAT.debug()
  188. << get_name() << " is not valid\n";
  189. }
  190. return false;
  191. }
  192. if (_host != nullptr) {
  193. if (!_host->begin_frame(FM_parasite, current_thread)) {
  194. if (GLCAT.is_debug()) {
  195. GLCAT.debug()
  196. << get_name() << "'s host is not ready\n";
  197. }
  198. return false;
  199. }
  200. } else {
  201. // We don't have a host window, which is possible for CocoaGraphicsBuffer.
  202. _gsg->set_current_properties(&get_fb_properties());
  203. if (!_gsg->begin_frame(current_thread)) {
  204. return false;
  205. }
  206. }
  207. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  208. glgsg->push_group_marker(std::string(CLASSPREFIX_QUOTED "GraphicsBuffer ") + get_name());
  209. // Figure out the desired size of the buffer.
  210. if (mode == FM_render) {
  211. clear_cube_map_selection();
  212. {
  213. // If the set of render-to-textures has recently changed, we need to
  214. // rebuild bitplanes.
  215. CDReader cdata(_cycler);
  216. if (cdata->_textures_seq != _last_textures_seq) {
  217. _last_textures_seq = cdata->_textures_seq;
  218. _needs_rebuild = true;
  219. }
  220. }
  221. if (_creation_flags & GraphicsPipe::BF_size_track_host) {
  222. if (_host != nullptr && _host->get_size() != _size) {
  223. // We also need to rebuild if we need to change size.
  224. _needs_rebuild = true;
  225. }
  226. }
  227. rebuild_bitplanes();
  228. if (_needs_rebuild) {
  229. // If we still need rebuild, something went wrong with
  230. // rebuild_bitplanes().
  231. glgsg->pop_group_marker();
  232. return false;
  233. }
  234. // In case of multisample rendering, we don't need to issue the barrier
  235. // until we call glBlitFramebuffer.
  236. #ifndef OPENGLES_1
  237. if (gl_enable_memory_barriers && _fbo_multisample == 0) {
  238. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  239. TextureContexts::iterator it;
  240. for (it = _texture_contexts.begin(); it != _texture_contexts.end(); ++it) {
  241. CLP(TextureContext) *gtc = *it;
  242. if (gtc != nullptr && gtc->needs_barrier(GL_FRAMEBUFFER_BARRIER_BIT)) {
  243. glgsg->issue_memory_barrier(GL_FRAMEBUFFER_BARRIER_BIT);
  244. // If we've done it for one, we've done it for all.
  245. break;
  246. }
  247. }
  248. }
  249. #endif
  250. } else if (mode == FM_refresh) {
  251. // Just bind the FBO.
  252. rebuild_bitplanes();
  253. }
  254. // The host window may not have had sRGB enabled, so we need to do this.
  255. #ifndef OPENGLES
  256. if (get_fb_properties().get_srgb_color()) {
  257. glEnable(GL_FRAMEBUFFER_SRGB);
  258. }
  259. #endif
  260. _gsg->set_current_properties(&get_fb_properties());
  261. report_my_gl_errors();
  262. return true;
  263. }
  264. /**
  265. * Calls 'glCheckFramebufferStatus'. On error, prints out an appropriate
  266. * error message and unbinds the fbo. Returns true for OK or false for error.
  267. */
  268. bool CLP(GraphicsBuffer)::
  269. check_fbo() {
  270. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  271. GLenum status = glgsg->_glCheckFramebufferStatus(GL_FRAMEBUFFER_EXT);
  272. if (status != GL_FRAMEBUFFER_COMPLETE_EXT) {
  273. GLCAT.error() << "EXT_framebuffer_object reports non-framebuffer-completeness:\n";
  274. switch(status) {
  275. case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
  276. GLCAT.error() << "FRAMEBUFFER_UNSUPPORTED"; break;
  277. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
  278. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_ATTACHMENT"; break;
  279. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
  280. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"; break;
  281. case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
  282. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_DIMENSIONS"; break;
  283. #ifndef OPENGLES_2
  284. case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
  285. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_FORMATS"; break;
  286. #endif
  287. #ifndef OPENGLES
  288. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
  289. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER"; break;
  290. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
  291. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_READ_BUFFER"; break;
  292. case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
  293. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_MULTISAMPLE"; break;
  294. case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS_ARB:
  295. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS"; break;
  296. case GL_FRAMEBUFFER_INCOMPLETE_LAYER_COUNT_ARB:
  297. GLCAT.error() << "FRAMEBUFFER_INCOMPLETE_LAYER_COUNT"; break;
  298. #endif
  299. default:
  300. GLCAT.error() << "UNKNOWN PROBLEM " << (int)status; break;
  301. }
  302. GLCAT.error(false) << " for " << get_name() << "\n";
  303. glgsg->bind_fbo(0);
  304. report_my_gl_errors();
  305. return false;
  306. }
  307. report_my_gl_errors();
  308. return true;
  309. }
  310. /**
  311. * This function will be called within the draw thread to allocate/reallocate
  312. * the fbo and all the associated renderbuffers, just before rendering a
  313. * frame.
  314. */
  315. void CLP(GraphicsBuffer)::
  316. rebuild_bitplanes() {
  317. check_host_valid();
  318. if (_gsg == 0) {
  319. return;
  320. }
  321. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  322. if (!_needs_rebuild) {
  323. if (_fbo_multisample != 0) {
  324. glgsg->bind_fbo(_fbo_multisample);
  325. } else if (_fbo.size() > 0) {
  326. glgsg->bind_fbo(_fbo[0]);
  327. } else {
  328. glgsg->bind_fbo(0);
  329. }
  330. _rb_context->set_active(true);
  331. return;
  332. }
  333. PStatGPUTimer timer(glgsg, _bind_texture_pcollector);
  334. // Calculate bitplane size. This can be larger than the buffer.
  335. if (_creation_flags & GraphicsPipe::BF_size_track_host) {
  336. if (_host != nullptr && _host->get_size() != _size) {
  337. set_size_and_recalc(_host->get_x_size(),
  338. _host->get_y_size());
  339. }
  340. }
  341. int bitplane_x = get_x_size();
  342. int bitplane_y = get_y_size();
  343. if (Texture::get_textures_power_2() != ATS_none) {
  344. bitplane_x = Texture::up_to_power_2(bitplane_x);
  345. bitplane_y = Texture::up_to_power_2(bitplane_y);
  346. }
  347. bool rb_resize = false;
  348. if ((bitplane_x != _rb_size_x)||
  349. (bitplane_y != _rb_size_y)) {
  350. _rb_size_x = bitplane_x;
  351. _rb_size_y = bitplane_y;
  352. rb_resize = true;
  353. }
  354. _rb_size_z = 1;
  355. _rb_data_size_bytes = 0;
  356. size_t num_fbos = 1;
  357. // These variables indicate what should be bound to each bitplane.
  358. Texture *attach[RTP_COUNT];
  359. memset(attach, 0, sizeof(Texture *) * RTP_COUNT);
  360. _texture_contexts.clear();
  361. // Sort the textures list into appropriate slots.
  362. {
  363. CDReader cdata(_cycler);
  364. // Determine whether this will be a layered or a regular FBO. If layered,
  365. // the number of _rb_size_z will be higher than 1.
  366. for (size_t i = 0; i != cdata->_textures.size(); ++i) {
  367. const RenderTexture &rt = cdata->_textures[i];
  368. RenderTextureMode rtm_mode = rt._rtm_mode;
  369. Texture *tex = rt._texture;
  370. if (rtm_mode == RTM_bind_layered) {
  371. _rb_size_z = max(_rb_size_z, tex->get_z_size());
  372. }
  373. }
  374. for (size_t i = 0; i != cdata->_textures.size(); ++i) {
  375. const RenderTexture &rt = cdata->_textures[i];
  376. RenderTextureMode rtm_mode = rt._rtm_mode;
  377. RenderTexturePlane plane = rt._plane;
  378. Texture *tex = rt._texture;
  379. if (rtm_mode == RTM_bind_layered && glgsg->_supports_geometry_shaders) {
  380. if (tex->get_z_size() != _rb_size_z) {
  381. GLCAT.warning()
  382. << "All textures attached to layered FBO should have the same layer count!\n";
  383. }
  384. // Assign the texture to this slot.
  385. attach[plane] = tex;
  386. continue;
  387. }
  388. if (rtm_mode != RTM_bind_or_copy) {
  389. continue;
  390. }
  391. // If we can't bind this type of texture, punt it.
  392. if ((tex->get_texture_type() != Texture::TT_2d_texture) &&
  393. (tex->get_texture_type() != Texture::TT_3d_texture) &&
  394. (tex->get_texture_type() != Texture::TT_2d_texture_array) &&
  395. (tex->get_texture_type() != Texture::TT_cube_map)) {
  396. ((CData *)cdata.p())->_textures[i]._rtm_mode = RTM_copy_texture;
  397. continue;
  398. }
  399. if (_rb_size_z > 1 && tex->get_texture_type() == Texture::TT_2d_texture) {
  400. // We can't bind a 2D texture to a layered FBO. If the user happened
  401. // to request RTM_bind_layered for a 2D texture, that's just silly,
  402. // and we can't render to anything but the first layer anyway.
  403. ((CData *)cdata.p())->_textures[i]._rtm_mode = RTM_copy_texture;
  404. continue;
  405. }
  406. // If I can't find an appropriate slot, or if there's already a texture
  407. // bound to this slot, then punt this texture.
  408. if (attach[plane]) {
  409. ((CData *)cdata.p())->_textures[i]._rtm_mode = RTM_copy_texture;
  410. continue;
  411. }
  412. if (tex->get_z_size() > 1) {
  413. num_fbos = max(num_fbos, (size_t)tex->get_z_size());
  414. }
  415. // Assign the texture to this slot.
  416. attach[plane] = tex;
  417. }
  418. }
  419. // Decide whether we should use a depth stencil or just a regular depth
  420. // attachment. If nothing was attached to either RTP_depth_stencil or
  421. // RTP_depth, we use a depth-stencil renderbuffer.
  422. _use_depth_stencil = false;
  423. if (_gsg->get_supports_depth_stencil()) {
  424. if (attach[RTP_depth_stencil]) {
  425. // This is the obvious case, of course.
  426. _use_depth_stencil = true;
  427. } else if (attach[RTP_depth]) {
  428. // We won't use a depth stencil texture as the user explicitly bound
  429. // something to RTP_depth.
  430. _use_depth_stencil = false;
  431. } else if (_fb_properties.get_stencil_bits() > 0) {
  432. // The user requested stencil bits. Let's take the hint.
  433. _use_depth_stencil = true;
  434. } else if (_fb_properties.get_depth_bits() > 24 ||
  435. _fb_properties.get_float_depth()) {
  436. // 32-bit float depth is supported in conjunction with depth stencil,
  437. // but it's a waste. Let's not do it unless the user requested stencil.
  438. _use_depth_stencil = false;
  439. } else if (_fb_properties.get_depth_bits() > 0) {
  440. // Let's use a depth stencil buffer by default, if a depth buffer was
  441. // requested.
  442. _use_depth_stencil = true;
  443. }
  444. } else if (attach[RTP_depth_stencil] != nullptr && attach[RTP_depth] == nullptr) {
  445. // The depth stencil slot was assigned a texture, but we don't support it.
  446. // Downgrade to a regular depth texture.
  447. std::swap(attach[RTP_depth], attach[RTP_depth_stencil]);
  448. }
  449. // Knowing this, we can already be a tiny bit more accurate about the
  450. // framebuffer properties.
  451. if (_use_depth_stencil) {
  452. _fb_properties.set_stencil_bits(8);
  453. } else {
  454. _fb_properties.set_stencil_bits(0);
  455. }
  456. // Having both a depth texture and a depth_stencil texture is invalid:
  457. // depth_stencil implies depth, and we can't bind them both. Detect that
  458. // case, normalize it, and complain.
  459. if (_use_depth_stencil && attach[RTP_depth] && attach[RTP_depth_stencil]) {
  460. attach[RTP_depth] = nullptr;
  461. GLCAT.warning() << "Attempt to bind both RTP_depth and RTP_depth_stencil bitplanes.\n";
  462. }
  463. // Now create the FBO's.
  464. _have_any_color = false;
  465. bool have_any_depth = false;
  466. if (num_fbos > _fbo.size()) {
  467. // Generate more FBO handles.
  468. size_t start = _fbo.size();
  469. GLuint zero = 0;
  470. _fbo.resize(num_fbos, zero);
  471. glgsg->_glGenFramebuffers(num_fbos - start, &_fbo[start]);
  472. }
  473. for (int layer = 0; layer < (int)num_fbos; ++layer) {
  474. // Bind the FBO
  475. if (_fbo[layer] == 0) {
  476. report_my_gl_errors();
  477. return;
  478. }
  479. glgsg->bind_fbo(_fbo[layer]);
  480. if (glgsg->_use_object_labels) {
  481. // Assign a label for OpenGL to use when displaying debug messages.
  482. if (num_fbos > 1) {
  483. std::ostringstream strm;
  484. strm << _name << '[' << layer << ']';
  485. std::string name = strm.str();
  486. glgsg->_glObjectLabel(GL_FRAMEBUFFER, _fbo[layer], name.size(), name.data());
  487. } else {
  488. glgsg->_glObjectLabel(GL_FRAMEBUFFER, _fbo[layer], _name.size(), _name.data());
  489. }
  490. }
  491. // For all slots, update the slot.
  492. if (_use_depth_stencil) {
  493. bind_slot(layer, rb_resize, attach, RTP_depth_stencil, GL_DEPTH_ATTACHMENT_EXT);
  494. have_any_depth = true;
  495. } else if (attach[RTP_depth] || _fb_properties.get_depth_bits() > 0) {
  496. bind_slot(layer, rb_resize, attach, RTP_depth, GL_DEPTH_ATTACHMENT_EXT);
  497. have_any_depth = true;
  498. }
  499. int next = GL_COLOR_ATTACHMENT0_EXT;
  500. if (attach[RTP_color] || _fb_properties.get_color_bits() > 0) {
  501. bind_slot(layer, rb_resize, attach, RTP_color, next++);
  502. if (_fb_properties.is_stereo()) {
  503. // The second tex view has already been initialized, so bind it
  504. // straight away.
  505. if (attach[RTP_color] != nullptr) {
  506. attach_tex(layer, 1, attach[RTP_color], next++);
  507. } else {
  508. // XXX hack: I needed a slot to use, and we don't currently use
  509. // RTP_stencil and it's treated as a color attachment below, so this
  510. // fits the bill. Eventually, we might want to add RTP_color_left
  511. // and RTP_color_right.
  512. bind_slot(layer, rb_resize, attach, RTP_stencil, next++);
  513. }
  514. }
  515. _have_any_color = true;
  516. }
  517. #ifndef OPENGLES_1
  518. for (int i=0; i<_fb_properties.get_aux_rgba(); i++) {
  519. bind_slot(layer, rb_resize, attach, (RenderTexturePlane)(RTP_aux_rgba_0+i), next++);
  520. _have_any_color = true;
  521. }
  522. for (int i=0; i<_fb_properties.get_aux_hrgba(); i++) {
  523. bind_slot(layer, rb_resize, attach, (RenderTexturePlane)(RTP_aux_hrgba_0+i), next++);
  524. _have_any_color = true;
  525. }
  526. for (int i=0; i<_fb_properties.get_aux_float(); i++) {
  527. bind_slot(layer, rb_resize, attach, (RenderTexturePlane)(RTP_aux_float_0+i), next++);
  528. _have_any_color = true;
  529. }
  530. #endif // OPENGLES
  531. if (_have_any_color || have_any_depth) {
  532. // Clear if the fbo was just created, regardless of the clear settings per
  533. // frame.
  534. if (_initial_clear) {
  535. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  536. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  537. }
  538. #ifndef OPENGLES_1
  539. } else if (glgsg->_supports_empty_framebuffer) {
  540. // Set the "default" width and height, which is required to have an FBO
  541. // without any attachments.
  542. glgsg->_glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_WIDTH, _rb_size_x);
  543. glgsg->_glFramebufferParameteri(GL_FRAMEBUFFER, GL_FRAMEBUFFER_DEFAULT_HEIGHT, _rb_size_y);
  544. #endif
  545. } else {
  546. // If all else fails, just bind a "dummy" attachment.
  547. bind_slot(layer, rb_resize, attach, RTP_color, next++);
  548. }
  549. }
  550. #ifndef OPENGLES
  551. // Setup any required multisample buffers. Does not work for layered
  552. // buffers.
  553. if (_requested_multisamples && _rb_size_z == 1) {
  554. if (_fbo_multisample == 0) {
  555. glgsg->_glGenFramebuffers(1, &_fbo_multisample);
  556. }
  557. glgsg->bind_fbo(_fbo_multisample);
  558. if (_use_depth_stencil || attach[RTP_depth] || _fb_properties.get_depth_bits() > 0) {
  559. bind_slot_multisample(rb_resize, attach, RTP_depth, GL_DEPTH_ATTACHMENT_EXT);
  560. }
  561. int next = GL_COLOR_ATTACHMENT0_EXT;
  562. if (attach[RTP_color] || _fb_properties.get_color_bits() > 0) {
  563. bind_slot_multisample(rb_resize, attach, RTP_color, next++);
  564. if (_fb_properties.is_stereo()) {
  565. // TODO: figure out how multisample is supposed to work with stereo
  566. // buffers.
  567. }
  568. }
  569. for (int i=0; i<_fb_properties.get_aux_rgba(); i++) {
  570. bind_slot_multisample(rb_resize, attach, (RenderTexturePlane)(RTP_aux_rgba_0+i), next++);
  571. }
  572. for (int i=0; i<_fb_properties.get_aux_hrgba(); i++) {
  573. bind_slot_multisample(rb_resize, attach, (RenderTexturePlane)(RTP_aux_hrgba_0+i), next++);
  574. }
  575. for (int i=0; i<_fb_properties.get_aux_float(); i++) {
  576. bind_slot_multisample(rb_resize, attach, (RenderTexturePlane)(RTP_aux_float_0+i), next++);
  577. }
  578. glEnable(GL_MULTISAMPLE);
  579. if (_initial_clear) {
  580. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  581. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  582. }
  583. } else {
  584. glDisable(GL_MULTISAMPLE);
  585. }
  586. #endif // OPENGLES
  587. if (!_have_any_color) {
  588. _fb_properties.set_rgba_bits(0, 0, 0, 0);
  589. }
  590. _rb_context->set_active(true);
  591. _rb_context->update_data_size_bytes(_rb_data_size_bytes);
  592. _initial_clear = false;
  593. report_my_gl_errors();
  594. #ifndef OPENGLES
  595. if (_have_any_color) {
  596. glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  597. glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  598. } else {
  599. glDrawBuffer(GL_NONE);
  600. glReadBuffer(GL_NONE);
  601. }
  602. #endif
  603. _needs_rebuild = false;
  604. report_my_gl_errors();
  605. if (!check_fbo()) {
  606. if (GLCAT.is_debug()) {
  607. GLCAT.debug()
  608. << get_name() << " check_fbo() returns false\n";
  609. }
  610. return;
  611. }
  612. }
  613. /**
  614. * Attaches either a texture or a renderbuffer to the specified bitplane.
  615. */
  616. void CLP(GraphicsBuffer)::
  617. bind_slot(int layer, bool rb_resize, Texture **attach, RenderTexturePlane slot, GLenum attachpoint) {
  618. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  619. Texture *tex = attach[slot];
  620. if (tex && layer >= tex->get_z_size()) {
  621. // If the requested layer index exceeds the number of layers in the
  622. // texture, we will not bind this layer.
  623. tex = nullptr;
  624. }
  625. if (!tex && _rb_size_z > 1) {
  626. // Since there is no such thing as a layered renderbuffer (to my
  627. // knowledge), we have to create a dummy texture to render to if no
  628. // texture was provided.
  629. tex = new Texture();
  630. if (_rb_size_z > 1) {
  631. // Apparently, it doesn't make a difference whether we use
  632. // setup_cube_map or setup_2d_texture_array here, since it's the same
  633. // internal storage.
  634. tex->setup_2d_texture_array(_rb_size_z);
  635. } else {
  636. tex->setup_2d_texture();
  637. }
  638. }
  639. if (tex) {
  640. // Bind the texture to the slot.
  641. tex->set_x_size(_rb_size_x);
  642. tex->set_y_size(_rb_size_y);
  643. if (tex->get_texture_type() != Texture::TT_cube_map && _rb_size_z > 1) {
  644. tex->set_z_size(_rb_size_z);
  645. }
  646. tex->set_pad_size(_rb_size_x - get_x_size(), _rb_size_y - get_y_size());
  647. // Adjust the texture format based on the requested framebuffer settings.
  648. switch (slot) {
  649. case RTP_depth:
  650. _fb_properties.setup_depth_texture(tex);
  651. break;
  652. case RTP_depth_stencil:
  653. tex->set_format(Texture::F_depth_stencil);
  654. if (_fb_properties.get_float_depth()) {
  655. tex->set_component_type(Texture::T_float);
  656. } else {
  657. tex->set_component_type(Texture::T_unsigned_int_24_8);
  658. }
  659. break;
  660. case RTP_aux_hrgba_0:
  661. case RTP_aux_hrgba_1:
  662. case RTP_aux_hrgba_2:
  663. case RTP_aux_hrgba_3:
  664. tex->set_format(Texture::F_rgba16);
  665. tex->set_component_type(Texture::T_float);
  666. break;
  667. case RTP_aux_float_0:
  668. case RTP_aux_float_1:
  669. case RTP_aux_float_2:
  670. case RTP_aux_float_3:
  671. tex->set_format(Texture::F_rgba32);
  672. tex->set_component_type(Texture::T_float);
  673. break;
  674. default:
  675. _fb_properties.setup_color_texture(tex);
  676. }
  677. GLenum target = glgsg->get_texture_target(tex->get_texture_type());
  678. if (target == GL_TEXTURE_CUBE_MAP) {
  679. target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
  680. }
  681. if (attachpoint == GL_DEPTH_ATTACHMENT_EXT) {
  682. GLCAT.debug() << "Binding texture " << *tex << " to depth attachment.\n";
  683. attach_tex(layer, 0, tex, GL_DEPTH_ATTACHMENT_EXT);
  684. #ifndef OPENGLES
  685. GLint depth_size = 0;
  686. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_DEPTH_SIZE, &depth_size);
  687. _fb_properties.set_depth_bits(depth_size);
  688. #endif
  689. if (slot == RTP_depth_stencil) {
  690. GLCAT.debug() << "Binding texture " << *tex << " to stencil attachment.\n";
  691. attach_tex(layer, 0, tex, GL_STENCIL_ATTACHMENT_EXT);
  692. #ifndef OPENGLES
  693. GLint stencil_size = 0;
  694. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_STENCIL_SIZE, &stencil_size);
  695. _fb_properties.set_stencil_bits(stencil_size);
  696. #endif
  697. }
  698. } else {
  699. GLCAT.debug() << "Binding texture " << *tex << " to color attachment.\n";
  700. attach_tex(layer, 0, tex, attachpoint);
  701. #ifndef OPENGLES
  702. if (attachpoint == GL_COLOR_ATTACHMENT0_EXT) {
  703. GLint red_size = 0, green_size = 0, blue_size = 0, alpha_size = 0;
  704. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_RED_SIZE, &red_size);
  705. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_GREEN_SIZE, &green_size);
  706. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_BLUE_SIZE, &blue_size);
  707. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_ALPHA_SIZE, &alpha_size);
  708. _fb_properties.set_rgba_bits(red_size, green_size, blue_size, alpha_size);
  709. }
  710. #endif
  711. }
  712. // If there was a renderbuffer bound to this slot, delete it.
  713. if (_rb[slot] != 0) {
  714. glgsg->_glDeleteRenderbuffers(1, &(_rb[slot]));
  715. _rb[slot] = 0;
  716. }
  717. report_my_gl_errors();
  718. } else {
  719. // No texture to bind. Instead, create a renderbuffer. Choose a suitable
  720. // renderbuffer format based on the requirements.
  721. #ifdef OPENGLES
  722. // OpenGL ES case.
  723. GLuint gl_format = GL_RGBA4;
  724. switch (slot) {
  725. case RTP_depth_stencil:
  726. gl_format = GL_DEPTH24_STENCIL8_OES;
  727. break;
  728. case RTP_depth:
  729. if (_fb_properties.get_depth_bits() > 24 && glgsg->_supports_depth32) {
  730. gl_format = GL_DEPTH_COMPONENT32_OES;
  731. } else if (_fb_properties.get_depth_bits() > 16 && glgsg->_supports_depth24) {
  732. gl_format = GL_DEPTH_COMPONENT24_OES;
  733. } else {
  734. gl_format = GL_DEPTH_COMPONENT16;
  735. }
  736. break;
  737. // NB: we currently use RTP_stencil to store the right eye for stereo.
  738. // case RTP_stencil: gl_format = GL_STENCIL_INDEX8; break
  739. default:
  740. if (_fb_properties.get_alpha_bits() == 0) {
  741. if (_fb_properties.get_color_bits() <= 16) {
  742. gl_format = GL_RGB565_OES;
  743. } else if (_fb_properties.get_color_bits() <= 24) {
  744. gl_format = GL_RGB8_OES;
  745. } else {
  746. gl_format = GL_RGB10_EXT;
  747. }
  748. } else if (_fb_properties.get_color_bits() == 0) {
  749. gl_format = GL_ALPHA8_EXT;
  750. } else if (_fb_properties.get_color_bits() <= 12
  751. && _fb_properties.get_alpha_bits() <= 4) {
  752. gl_format = GL_RGBA4_OES;
  753. } else if (_fb_properties.get_color_bits() <= 15
  754. && _fb_properties.get_alpha_bits() == 1) {
  755. gl_format = GL_RGB5_A1_OES;
  756. } else if (_fb_properties.get_color_bits() <= 30
  757. && _fb_properties.get_alpha_bits() <= 2) {
  758. gl_format = GL_RGB10_A2_EXT;
  759. } else {
  760. gl_format = GL_RGBA8_OES;
  761. }
  762. }
  763. #else
  764. GLuint gl_format = GL_RGBA;
  765. switch (slot) {
  766. case RTP_depth_stencil:
  767. if (_fb_properties.get_depth_bits() > 24 ||
  768. _fb_properties.get_float_depth()) {
  769. if (!glgsg->_use_remapped_depth_range) {
  770. gl_format = GL_DEPTH32F_STENCIL8;
  771. } else {
  772. gl_format = GL_DEPTH32F_STENCIL8_NV;
  773. }
  774. } else {
  775. gl_format = GL_DEPTH24_STENCIL8;
  776. }
  777. break;
  778. case RTP_depth:
  779. if (_fb_properties.get_float_depth()) {
  780. if (!glgsg->_use_remapped_depth_range) {
  781. gl_format = GL_DEPTH_COMPONENT32F;
  782. } else {
  783. gl_format = GL_DEPTH_COMPONENT32F_NV;
  784. }
  785. } else if (_fb_properties.get_depth_bits() > 24) {
  786. gl_format = GL_DEPTH_COMPONENT32;
  787. } else if (_fb_properties.get_depth_bits() > 16) {
  788. gl_format = GL_DEPTH_COMPONENT24;
  789. } else if (_fb_properties.get_depth_bits() > 1) {
  790. gl_format = GL_DEPTH_COMPONENT16;
  791. } else {
  792. gl_format = GL_DEPTH_COMPONENT;
  793. }
  794. break;
  795. case RTP_aux_rgba_0:
  796. case RTP_aux_rgba_1:
  797. case RTP_aux_rgba_2:
  798. case RTP_aux_rgba_3:
  799. gl_format = GL_RGBA;
  800. break;
  801. case RTP_aux_hrgba_0:
  802. case RTP_aux_hrgba_1:
  803. case RTP_aux_hrgba_2:
  804. case RTP_aux_hrgba_3:
  805. gl_format = GL_RGBA16F_ARB;
  806. break;
  807. case RTP_aux_float_0:
  808. case RTP_aux_float_1:
  809. case RTP_aux_float_2:
  810. case RTP_aux_float_3:
  811. gl_format = GL_RGBA32F_ARB;
  812. break;
  813. default:
  814. if (_fb_properties.get_alpha_bits() == 0) {
  815. if (_fb_properties.get_srgb_color()) {
  816. gl_format = GL_SRGB8;
  817. } else if (_fb_properties.get_float_color()) {
  818. if (_fb_properties.get_color_bits() > 16 * 3) {
  819. gl_format = GL_RGB32F_ARB;
  820. } else {
  821. gl_format = GL_RGB16F_ARB;
  822. }
  823. } else {
  824. if (_fb_properties.get_color_bits() > 16 * 3) {
  825. gl_format = GL_RGB32F_ARB;
  826. } else if (_fb_properties.get_color_bits() > 8 * 3) {
  827. gl_format = GL_RGB16_EXT;
  828. } else {
  829. gl_format = GL_RGB;
  830. }
  831. }
  832. } else {
  833. if (_fb_properties.get_srgb_color()) {
  834. gl_format = GL_SRGB8_ALPHA8;
  835. } else if (_fb_properties.get_float_color()) {
  836. if (_fb_properties.get_color_bits() > 16 * 3) {
  837. gl_format = GL_RGBA32F_ARB;
  838. } else {
  839. gl_format = GL_RGBA16F_ARB;
  840. }
  841. } else {
  842. if (_fb_properties.get_color_bits() > 16 * 3) {
  843. gl_format = GL_RGBA32F_ARB;
  844. } else if (_fb_properties.get_color_bits() > 8 * 3) {
  845. gl_format = GL_RGBA16_EXT;
  846. } else {
  847. gl_format = GL_RGBA;
  848. }
  849. }
  850. }
  851. };
  852. #endif
  853. // If there's no renderbuffer for this slot, create one.
  854. if (_rb[slot] == 0) {
  855. glgsg->_glGenRenderbuffers(1, &(_rb[slot]));
  856. }
  857. // Allocate and bind the renderbuffer.
  858. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, _rb[slot]);
  859. if (slot == RTP_depth_stencil) {
  860. GLCAT.debug() << "Creating depth stencil renderbuffer.\n";
  861. // Allocate renderbuffer storage for depth stencil.
  862. GLint depth_size = 0, stencil_size = 0;
  863. glgsg->_glRenderbufferStorage(GL_RENDERBUFFER_EXT, gl_format, _rb_size_x, _rb_size_y);
  864. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &depth_size);
  865. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_STENCIL_SIZE_EXT, &stencil_size);
  866. _fb_properties.set_depth_bits(depth_size);
  867. _fb_properties.set_stencil_bits(stencil_size);
  868. _rb_data_size_bytes += _rb_size_x * _rb_size_y * ((depth_size + stencil_size) / 8);
  869. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  870. GLuint rb = _rb[slot];
  871. if (_shared_depth_buffer) {
  872. rb = _shared_depth_buffer->_rb[slot];
  873. }
  874. glgsg->_glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  875. GL_RENDERBUFFER_EXT, rb);
  876. glgsg->_glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  877. GL_RENDERBUFFER_EXT, rb);
  878. report_my_gl_errors();
  879. } else if (slot == RTP_depth) {
  880. GLCAT.debug() << "Creating depth renderbuffer.\n";
  881. // Allocate renderbuffer storage for regular depth.
  882. GLint depth_size = 0;
  883. glgsg->_glRenderbufferStorage(GL_RENDERBUFFER_EXT, gl_format, _rb_size_x, _rb_size_y);
  884. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &depth_size);
  885. #ifndef OPENGLES
  886. // Are we getting only 24 bits of depth when we requested 32? It may be
  887. // because GL_DEPTH_COMPONENT32 is not a required format, while 32F is.
  888. if (gl_format == GL_DEPTH_COMPONENT32 && depth_size < 32) {
  889. if (!glgsg->_use_remapped_depth_range) {
  890. gl_format = GL_DEPTH_COMPONENT32F;
  891. } else {
  892. gl_format = GL_DEPTH_COMPONENT32F_NV;
  893. }
  894. glgsg->_glRenderbufferStorage(GL_RENDERBUFFER_EXT, gl_format, _rb_size_x, _rb_size_y);
  895. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &depth_size);
  896. _fb_properties.set_float_depth(true);
  897. }
  898. #endif
  899. _fb_properties.set_depth_bits(depth_size);
  900. _rb_data_size_bytes += _rb_size_x * _rb_size_y * (depth_size / 8);
  901. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  902. GLuint rb = _rb[slot];
  903. if (_shared_depth_buffer) {
  904. rb = _shared_depth_buffer->_rb[slot];
  905. }
  906. glgsg->_glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  907. GL_RENDERBUFFER_EXT, rb);
  908. report_my_gl_errors();
  909. } else {
  910. GLCAT.debug() << "Creating color renderbuffer.\n";
  911. glgsg->_glRenderbufferStorage(GL_RENDERBUFFER_EXT, gl_format, _rb_size_x, _rb_size_y);
  912. GLint red_size = 0, green_size = 0, blue_size = 0, alpha_size = 0;
  913. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_RED_SIZE_EXT, &red_size);
  914. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_GREEN_SIZE_EXT, &green_size);
  915. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_BLUE_SIZE_EXT, &blue_size);
  916. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_ALPHA_SIZE_EXT, &alpha_size);
  917. if (attachpoint == GL_COLOR_ATTACHMENT0_EXT) {
  918. _fb_properties.set_rgba_bits(red_size, green_size, blue_size, alpha_size);
  919. }
  920. _rb_data_size_bytes += _rb_size_x * _rb_size_y * ((red_size + green_size + blue_size + alpha_size) / 8);
  921. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  922. glgsg->_glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, attachpoint,
  923. GL_RENDERBUFFER_EXT, _rb[slot]);
  924. report_my_gl_errors();
  925. }
  926. }
  927. }
  928. /**
  929. * Attaches incoming Texture or renderbuffer to the required bitplanes for the
  930. * 2 FBOs comprising a multisample graphics buffer.
  931. */
  932. void CLP(GraphicsBuffer)::
  933. bind_slot_multisample(bool rb_resize, Texture **attach, RenderTexturePlane slot, GLenum attachpoint) {
  934. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  935. if ((_rbm[slot] != 0)&&(!rb_resize)) {
  936. return;
  937. }
  938. if (_rbm[slot] != 0) {
  939. glgsg->_glDeleteRenderbuffers(1, &(_rbm[slot]));
  940. _rbm[slot] = 0;
  941. }
  942. glgsg->_glBindFramebuffer(GL_FRAMEBUFFER_EXT, _fbo_multisample);
  943. glgsg->_glGenRenderbuffers(1, &(_rbm[slot]));
  944. // Allocate and bind the renderbuffer.
  945. Texture *tex = attach[slot]; // If there is a texture map, use it's format as needed.
  946. if (attachpoint == GL_DEPTH_ATTACHMENT_EXT) {
  947. #ifndef OPENGLES_2
  948. if (_use_depth_stencil) {
  949. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, _rbm[slot]);
  950. if (_requested_coverage_samples) {
  951. glgsg->_glRenderbufferStorageMultisampleCoverage(GL_RENDERBUFFER_EXT, _requested_coverage_samples,
  952. _requested_multisamples, GL_DEPTH_STENCIL_EXT,
  953. _rb_size_x, _rb_size_y);
  954. } else {
  955. glgsg->_glRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, _requested_multisamples, GL_DEPTH_STENCIL_EXT,
  956. _rb_size_x, _rb_size_y);
  957. }
  958. #ifndef OPENGLES
  959. GLint givenSamples = -1;
  960. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &givenSamples);
  961. #endif
  962. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  963. glgsg->_glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  964. GL_RENDERBUFFER_EXT, _rbm[slot]);
  965. glgsg->_glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  966. GL_RENDERBUFFER_EXT, _rbm[slot]);
  967. } else {
  968. #endif
  969. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, _rbm[slot]);
  970. GLuint format = GL_DEPTH_COMPONENT;
  971. #ifndef OPENGLES
  972. if (_fb_properties.get_float_depth()) {
  973. if (!glgsg->_use_remapped_depth_range) {
  974. format = GL_DEPTH_COMPONENT32F;
  975. } else {
  976. format = GL_DEPTH_COMPONENT32F_NV;
  977. }
  978. } else
  979. #endif
  980. if (tex) {
  981. switch (tex->get_format()) {
  982. case Texture::F_depth_component16:
  983. format = GL_DEPTH_COMPONENT16;
  984. break;
  985. case Texture::F_depth_component24:
  986. format = GL_DEPTH_COMPONENT24;
  987. break;
  988. case Texture::F_depth_component32:
  989. format = GL_DEPTH_COMPONENT32;
  990. break;
  991. default:
  992. break;
  993. }
  994. }
  995. if (_requested_coverage_samples) {
  996. glgsg->_glRenderbufferStorageMultisampleCoverage(GL_RENDERBUFFER_EXT, _requested_coverage_samples,
  997. _requested_multisamples, format,
  998. _rb_size_x, _rb_size_y);
  999. } else {
  1000. glgsg->_glRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, _requested_multisamples, format,
  1001. _rb_size_x, _rb_size_y);
  1002. #ifndef OPENGLES_2
  1003. }
  1004. #endif
  1005. #ifndef OPENGLES
  1006. GLint givenSamples = -1;
  1007. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &givenSamples);
  1008. #endif
  1009. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  1010. glgsg->_glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  1011. GL_RENDERBUFFER_EXT, _rbm[slot]);
  1012. }
  1013. } else {
  1014. GLuint gl_format = GL_RGBA;
  1015. #ifndef OPENGLES
  1016. switch (slot) {
  1017. case RTP_aux_rgba_0:
  1018. case RTP_aux_rgba_1:
  1019. case RTP_aux_rgba_2:
  1020. case RTP_aux_rgba_3:
  1021. gl_format = GL_RGBA;
  1022. break;
  1023. case RTP_aux_hrgba_0:
  1024. case RTP_aux_hrgba_1:
  1025. case RTP_aux_hrgba_2:
  1026. case RTP_aux_hrgba_3:
  1027. gl_format = GL_RGBA16F_ARB;
  1028. break;
  1029. case RTP_aux_float_0:
  1030. case RTP_aux_float_1:
  1031. case RTP_aux_float_2:
  1032. case RTP_aux_float_3:
  1033. gl_format = GL_RGBA32F_ARB;
  1034. break;
  1035. default:
  1036. gl_format = GL_RGBA;
  1037. break;
  1038. }
  1039. #endif
  1040. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, _rbm[slot]);
  1041. if (_requested_coverage_samples) {
  1042. glgsg->_glRenderbufferStorageMultisampleCoverage(GL_RENDERBUFFER_EXT, _requested_coverage_samples,
  1043. _requested_multisamples, gl_format, _rb_size_x, _rb_size_y);
  1044. } else {
  1045. glgsg->_glRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, _requested_multisamples, gl_format,
  1046. _rb_size_x, _rb_size_y);
  1047. }
  1048. #ifndef OPENGLES
  1049. GLint givenSamples = -1;
  1050. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &givenSamples);
  1051. #endif
  1052. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  1053. glgsg->_glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, attachpoint,
  1054. GL_RENDERBUFFER_EXT, _rbm[slot]);
  1055. }
  1056. glgsg->report_my_gl_errors();
  1057. }
  1058. /**
  1059. * This function attaches the given texture to the given attachment point.
  1060. */
  1061. void CLP(GraphicsBuffer)::
  1062. attach_tex(int layer, int view, Texture *attach, GLenum attachpoint) {
  1063. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1064. if (view >= attach->get_num_views()) {
  1065. attach->set_num_views(view + 1);
  1066. }
  1067. // Create the OpenGL texture object.
  1068. TextureContext *tc = attach->prepare_now(view, glgsg->get_prepared_objects(), glgsg);
  1069. nassertv(tc != nullptr);
  1070. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), tc);
  1071. glgsg->update_texture(gtc, true);
  1072. gtc->set_active(true);
  1073. _texture_contexts.push_back(gtc);
  1074. // It seems that binding the texture is necessary before binding to a
  1075. // framebuffer attachment.
  1076. glgsg->apply_texture(gtc);
  1077. #if !defined(OPENGLES) && defined(SUPPORT_FIXED_FUNCTION)
  1078. if (glgsg->has_fixed_function_pipeline()) {
  1079. GLclampf priority = 1.0f;
  1080. glPrioritizeTextures(1, &gtc->_index, &priority);
  1081. }
  1082. #endif
  1083. #ifndef OPENGLES
  1084. if (_rb_size_z != 1) {
  1085. // Bind all of the layers of the texture.
  1086. nassertv(glgsg->_glFramebufferTexture != nullptr);
  1087. glgsg->_glFramebufferTexture(GL_FRAMEBUFFER_EXT, attachpoint,
  1088. gtc->_index, 0);
  1089. return;
  1090. }
  1091. #endif
  1092. GLenum target = glgsg->get_texture_target(attach->get_texture_type());
  1093. if (target == GL_TEXTURE_CUBE_MAP) {
  1094. target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
  1095. }
  1096. switch (target) {
  1097. #ifndef OPENGLES_1
  1098. case GL_TEXTURE_3D:
  1099. glgsg->_glFramebufferTexture3D(GL_FRAMEBUFFER_EXT, attachpoint,
  1100. target, gtc->_index, 0, layer);
  1101. break;
  1102. case GL_TEXTURE_2D_ARRAY:
  1103. glgsg->_glFramebufferTextureLayer(GL_FRAMEBUFFER_EXT, attachpoint,
  1104. gtc->_index, 0, layer);
  1105. break;
  1106. #endif
  1107. default:
  1108. glgsg->_glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, attachpoint,
  1109. target, gtc->_index, 0);
  1110. }
  1111. }
  1112. /**
  1113. * This function will be called within the draw thread after rendering is
  1114. * completed for a given frame. If we've just rendered into level zero of a
  1115. * mipmapped texture, then all subsequent mipmap levels will now be
  1116. * calculated.
  1117. */
  1118. void CLP(GraphicsBuffer)::
  1119. generate_mipmaps() {
  1120. if (gl_ignore_mipmaps && !gl_force_mipmaps) {
  1121. return;
  1122. }
  1123. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1124. // PStatGPUTimer timer(glgsg, _generate_mipmap_pcollector);
  1125. pvector<CLP(TextureContext)*>::iterator it;
  1126. for (it = _texture_contexts.begin(); it != _texture_contexts.end(); ++it) {
  1127. CLP(TextureContext) *gtc = *it;
  1128. if (gtc->_generate_mipmaps) {
  1129. glgsg->generate_mipmaps(gtc);
  1130. }
  1131. }
  1132. report_my_gl_errors();
  1133. }
  1134. /**
  1135. * This function will be called within the draw thread after rendering is
  1136. * completed for a given frame. It should do whatever finalization is
  1137. * required.
  1138. */
  1139. void CLP(GraphicsBuffer)::
  1140. end_frame(FrameMode mode, Thread *current_thread) {
  1141. end_frame_spam(mode);
  1142. nassertv(_gsg != nullptr);
  1143. // Resolve Multisample rendering if using it.
  1144. if (_requested_multisamples && _fbo_multisample) {
  1145. resolve_multisamples();
  1146. }
  1147. if (mode == FM_render) {
  1148. copy_to_textures();
  1149. }
  1150. // Unbind the FBO. TODO: calling bind_fbo is slow, so we should probably
  1151. // move this to begin_frame to prevent unnecessary calls.
  1152. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1153. glgsg->bind_fbo(0);
  1154. _bound_tex_page = -1;
  1155. if (mode == FM_render) {
  1156. generate_mipmaps();
  1157. }
  1158. if (_host != nullptr) {
  1159. _host->end_frame(FM_parasite, current_thread);
  1160. } else {
  1161. glgsg->end_frame(current_thread);
  1162. }
  1163. if (mode == FM_render) {
  1164. trigger_flip();
  1165. clear_cube_map_selection();
  1166. }
  1167. report_my_gl_errors();
  1168. glgsg->pop_group_marker();
  1169. }
  1170. /**
  1171. *
  1172. */
  1173. void CLP(GraphicsBuffer)::
  1174. set_size(int x, int y) {
  1175. if (_size.get_x() != x || _size.get_y() != y) {
  1176. _needs_rebuild = true;
  1177. }
  1178. set_size_and_recalc(x, y);
  1179. }
  1180. /**
  1181. * Called internally when the window is in render-to-a-texture mode and we are
  1182. * in the process of rendering the six faces of a cube map, or any other
  1183. * multi-page texture. This should do whatever needs to be done to switch the
  1184. * buffer to the indicated page.
  1185. */
  1186. void CLP(GraphicsBuffer)::
  1187. select_target_tex_page(int page) {
  1188. nassertv(page >= 0 && (size_t)page < _fbo.size());
  1189. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1190. bool switched_page = (_bound_tex_page != page);
  1191. if (switched_page) {
  1192. if (_bound_tex_page != -1) {
  1193. // Resolve the multisample rendering for the previous face.
  1194. if (_requested_multisamples && _fbo_multisample) {
  1195. resolve_multisamples();
  1196. }
  1197. }
  1198. if (_fbo_multisample != 0) {
  1199. // TODO: re-issue clears?
  1200. } else {
  1201. glgsg->bind_fbo(_fbo[page]);
  1202. }
  1203. _bound_tex_page = page;
  1204. }
  1205. report_my_gl_errors();
  1206. }
  1207. /**
  1208. * Opens the window right now. Called from the window thread. Returns true
  1209. * if the window is successfully opened, or false if there was a problem.
  1210. */
  1211. bool CLP(GraphicsBuffer)::
  1212. open_buffer() {
  1213. report_my_gl_errors();
  1214. // Double check that we have a valid gsg
  1215. nassertr(_gsg != nullptr, false);
  1216. if (!_gsg->is_valid()) {
  1217. return false;
  1218. }
  1219. // Count total color buffers.
  1220. int totalcolor =
  1221. (_fb_properties.get_stereo() ? 2 : 1) +
  1222. _fb_properties.get_aux_rgba() +
  1223. _fb_properties.get_aux_hrgba() +
  1224. _fb_properties.get_aux_float();
  1225. // Check for support of relevant extensions.
  1226. CLP(GraphicsStateGuardian) *glgsg;
  1227. DCAST_INTO_R(glgsg, _gsg, false);
  1228. if (!glgsg->_supports_framebuffer_object) {
  1229. return false;
  1230. }
  1231. if (_rb_context == nullptr) {
  1232. _rb_context = new BufferContext(&(glgsg->_renderbuffer_residency));
  1233. }
  1234. /*
  1235. * Describe the framebuffer properties of the FBO. Unfortunately, we can't
  1236. * currently predict which formats the implementation will allow us to use at
  1237. * this point, so we'll just have to make some guesses and parrot the rest of
  1238. * the properties back to the user. When we actually attach the textures,
  1239. * we'll update the properties more appropriately.
  1240. */
  1241. // Rounding the depth bits is not spectacular, but at least we're telling
  1242. // the user *something* about what we're going to get.
  1243. // A lot of code seems to depend on being able to get a color buffer by just
  1244. // setting the rgb_color bit.
  1245. if (_fb_properties.get_color_bits() == 0 &&
  1246. _fb_properties.get_rgb_color()) {
  1247. _fb_properties.set_color_bits(1);
  1248. _fb_properties.set_red_bits(1);
  1249. _fb_properties.set_green_bits(1);
  1250. _fb_properties.set_blue_bits(1);
  1251. }
  1252. // Actually, let's always get a colour buffer for now until we figure out
  1253. // why Intel HD Graphics cards complain otherwise.
  1254. if (gl_force_fbo_color && _fb_properties.get_color_bits() == 0) {
  1255. _fb_properties.set_color_bits(1);
  1256. }
  1257. if (_fb_properties.get_depth_bits() > 24) {
  1258. _fb_properties.set_depth_bits(32);
  1259. } else if (_fb_properties.get_depth_bits() > 16) {
  1260. _fb_properties.set_depth_bits(24);
  1261. } else if (_fb_properties.get_depth_bits() > 0) {
  1262. _fb_properties.set_depth_bits(16);
  1263. } else {
  1264. _fb_properties.set_depth_bits(0);
  1265. }
  1266. // We're not going to get more than this, ever. At least not until OpenGL
  1267. // introduces 64-bit texture formats.
  1268. if (_fb_properties.get_color_bits() > 96) {
  1269. _fb_properties.set_color_bits(96);
  1270. }
  1271. if (_fb_properties.get_red_bits() > 32) {
  1272. _fb_properties.set_red_bits(32);
  1273. }
  1274. if (_fb_properties.get_green_bits() > 32) {
  1275. _fb_properties.set_green_bits(32);
  1276. }
  1277. if (_fb_properties.get_blue_bits() > 32) {
  1278. _fb_properties.set_blue_bits(32);
  1279. }
  1280. if (_fb_properties.get_alpha_bits() > 32) {
  1281. _fb_properties.set_alpha_bits(32);
  1282. }
  1283. if (_fb_properties.get_float_depth()) {
  1284. // GL_DEPTH_COMPONENT32F seems the only depth float format.
  1285. _fb_properties.set_depth_bits(32);
  1286. }
  1287. // We currently only support color formats this big as float.
  1288. if (_fb_properties.get_color_bits() > 16 * 3) {
  1289. _fb_properties.set_float_color(true);
  1290. }
  1291. if (_fb_properties.get_srgb_color()) {
  1292. // This is the only sRGB color format OpenGL supports.
  1293. _fb_properties.set_rgba_bits(8, 8, 8,
  1294. (_fb_properties.get_alpha_bits() > 0) ? 8 : 0);
  1295. _fb_properties.set_float_color(false);
  1296. }
  1297. if (!_gsg->get_supports_depth_stencil()) {
  1298. // At least we know we won't be getting stencil bits.
  1299. _fb_properties.set_stencil_bits(0);
  1300. } else if (_fb_properties.get_stencil_bits() > 0) {
  1301. // We don't currently support stencil-only targets.
  1302. _fb_properties.set_stencil_bits(8);
  1303. if (_fb_properties.get_depth_bits() < 24) {
  1304. _fb_properties.set_depth_bits(24);
  1305. }
  1306. }
  1307. // Accumulation buffers aren't supported for FBOs.
  1308. _fb_properties.set_accum_bits(0);
  1309. if (glgsg->get_supports_framebuffer_multisample() && glgsg->get_supports_framebuffer_blit()) {
  1310. _requested_multisamples = _fb_properties.get_multisamples();
  1311. } else {
  1312. _requested_multisamples = 0;
  1313. }
  1314. #ifndef OPENGLES
  1315. if (glgsg->get_supports_framebuffer_multisample_coverage_nv() && glgsg->get_supports_framebuffer_blit()) {
  1316. _requested_coverage_samples = _fb_properties.get_coverage_samples();
  1317. // Note: Only 4 and 8 actual samples are supported by the extension, with
  1318. // 8 or 16 coverage samples.
  1319. if ((_requested_coverage_samples <= 8) && (_requested_coverage_samples > 0)) {
  1320. _requested_multisamples = 4;
  1321. _requested_coverage_samples = 8;
  1322. } else if (_requested_coverage_samples > 8) {
  1323. if (_requested_multisamples < 8) {
  1324. _requested_multisamples = 4;
  1325. } else {
  1326. _requested_multisamples = 8;
  1327. }
  1328. _requested_coverage_samples = 16;
  1329. }
  1330. } else {
  1331. _requested_coverage_samples = 0;
  1332. }
  1333. #endif
  1334. if (_requested_multisamples > glgsg->_max_fb_samples) {
  1335. _requested_multisamples = glgsg->_max_fb_samples;
  1336. }
  1337. _fb_properties.set_multisamples(_requested_multisamples);
  1338. _fb_properties.set_coverage_samples(_requested_coverage_samples);
  1339. // Update aux settings to reflect the GL_MAX_DRAW_BUFFERS limit, if we
  1340. // exceed it, that is.
  1341. int availcolor = glgsg->_max_color_targets;
  1342. if (totalcolor > availcolor) {
  1343. int aux_rgba = _fb_properties.get_aux_rgba();
  1344. int aux_hrgba = _fb_properties.get_aux_hrgba();
  1345. int aux_float = _fb_properties.get_aux_float();
  1346. if (_fb_properties.get_color_bits() > 0 && availcolor > 0) {
  1347. --availcolor;
  1348. if (_fb_properties.is_stereo()) {
  1349. if (availcolor > 0) {
  1350. --availcolor;
  1351. } else {
  1352. _fb_properties.set_stereo(0);
  1353. }
  1354. }
  1355. }
  1356. aux_rgba = min(aux_rgba, availcolor);
  1357. availcolor -= aux_rgba;
  1358. aux_hrgba = min(aux_hrgba, availcolor);
  1359. availcolor -= aux_hrgba;
  1360. aux_float = min(aux_float, availcolor);
  1361. availcolor -= aux_float;
  1362. _fb_properties.set_aux_rgba(aux_rgba);
  1363. _fb_properties.set_aux_hrgba(aux_hrgba);
  1364. _fb_properties.set_aux_float(aux_float);
  1365. }
  1366. _fb_properties.set_back_buffers(0);
  1367. _fb_properties.set_indexed_color(0);
  1368. _fb_properties.set_rgb_color(1);
  1369. if (_host != nullptr) {
  1370. _fb_properties.set_force_hardware(_host->get_fb_properties().get_force_hardware());
  1371. _fb_properties.set_force_software(_host->get_fb_properties().get_force_software());
  1372. }
  1373. _is_valid = true;
  1374. _needs_rebuild = true;
  1375. report_my_gl_errors();
  1376. return true;
  1377. }
  1378. /**
  1379. * This is normally called only from within make_texture_buffer(). When
  1380. * called on a ParasiteBuffer, it returns the host of that buffer; but when
  1381. * called on some other buffer, it returns the buffer itself.
  1382. */
  1383. GraphicsOutput *CLP(GraphicsBuffer)::
  1384. get_host() {
  1385. return (_host != nullptr) ? _host : this;
  1386. }
  1387. /**
  1388. * Closes the buffer right now. Called from the window thread.
  1389. */
  1390. void CLP(GraphicsBuffer)::
  1391. close_buffer() {
  1392. _rb_data_size_bytes = 0;
  1393. if (_rb_context != nullptr) {
  1394. _rb_context->update_data_size_bytes(0);
  1395. delete _rb_context;
  1396. _rb_context = nullptr;
  1397. }
  1398. check_host_valid();
  1399. if (_gsg == 0) {
  1400. return;
  1401. }
  1402. // Get the glgsg.
  1403. CLP(GraphicsStateGuardian) *glgsg;
  1404. DCAST_INTO_V(glgsg, _gsg);
  1405. report_my_gl_errors();
  1406. // Delete the renderbuffers.
  1407. for (int i=0; i<RTP_COUNT; i++) {
  1408. if (_rb[i] != 0) {
  1409. glgsg->_glDeleteRenderbuffers(1, &(_rb[i]));
  1410. _rb[i] = 0;
  1411. }
  1412. }
  1413. // Delete the multisample renderbuffers.
  1414. for (int i=0; i<RTP_COUNT; i++) {
  1415. if (_rbm[i] != 0) {
  1416. glgsg->_glDeleteRenderbuffers(1, &(_rbm[i]));
  1417. _rb[i] = 0;
  1418. }
  1419. }
  1420. _rb_size_x = 0;
  1421. _rb_size_y = 0;
  1422. report_my_gl_errors();
  1423. // Delete the FBO itself.
  1424. if (!_fbo.empty()) {
  1425. glgsg->_glDeleteFramebuffers(_fbo.size(), _fbo.data());
  1426. _fbo.clear();
  1427. }
  1428. report_my_gl_errors();
  1429. // Release the Gsg
  1430. _gsg.clear();
  1431. _is_valid = false;
  1432. }
  1433. /**
  1434. * Will attempt to use the depth buffer of the input graphics_output. The
  1435. * buffer sizes must be exactly the same.
  1436. */
  1437. bool CLP(GraphicsBuffer)::
  1438. share_depth_buffer(GraphicsOutput *graphics_output) {
  1439. bool state;
  1440. CLP(GraphicsBuffer) *input_graphics_output;
  1441. state = false;
  1442. input_graphics_output = DCAST(CLP(GraphicsBuffer), graphics_output);
  1443. if (this != input_graphics_output && input_graphics_output) {
  1444. state = true;
  1445. this->unshare_depth_buffer();
  1446. // Make sure that the buffers are both FBOs.
  1447. if (!input_graphics_output->is_of_type(CLP(GraphicsBuffer)::get_class_type())) {
  1448. GLCAT.error() << "share_depth_buffer: non-matching type\n";
  1449. state = false;
  1450. }
  1451. // Check buffer size.
  1452. if (this->get_x_size() != input_graphics_output->get_x_size()) {
  1453. GLCAT.error() << "share_depth_buffer: non-matching width\n";
  1454. state = false;
  1455. }
  1456. if (this->get_y_size() != input_graphics_output->get_y_size()) {
  1457. GLCAT.error() << "share_depth_buffer: non-matching height\n";
  1458. state = false;
  1459. }
  1460. // Check multisample compatibility.
  1461. if (this->get_multisample_count() != input_graphics_output->get_multisample_count()) {
  1462. GLCAT.error() << "share_depth_buffer: non-matching multisamples\n";
  1463. state = false;
  1464. }
  1465. if (this->get_coverage_sample_count() != input_graphics_output->get_coverage_sample_count()) {
  1466. GLCAT.error() << "share_depth_buffer: non-matching coverage samples\n";
  1467. state = false;
  1468. }
  1469. if (state) {
  1470. // let the input GraphicsOutput know that there is an object sharing its
  1471. // depth buffer
  1472. input_graphics_output->register_shared_depth_buffer(this);
  1473. _shared_depth_buffer = input_graphics_output;
  1474. state = true;
  1475. }
  1476. _needs_rebuild = true;
  1477. }
  1478. report_my_gl_errors();
  1479. return state;
  1480. }
  1481. /**
  1482. * Discontinue sharing the depth buffer.
  1483. */
  1484. void CLP(GraphicsBuffer)::
  1485. unshare_depth_buffer() {
  1486. if (_shared_depth_buffer) {
  1487. // let the GraphicsOutput know that this object is no longer sharing its
  1488. // depth buffer
  1489. _shared_depth_buffer->unregister_shared_depth_buffer(this);
  1490. _shared_depth_buffer = 0;
  1491. _needs_rebuild = true;
  1492. }
  1493. }
  1494. /**
  1495. * Returns true if this particular GraphicsOutput can render directly into a
  1496. * texture, or false if it must always copy-to-texture at the end of each
  1497. * frame to achieve this effect.
  1498. */
  1499. bool CLP(GraphicsBuffer)::
  1500. get_supports_render_texture() const {
  1501. // FBO-based buffers, by their nature, can always bind-to-texture.
  1502. return true;
  1503. }
  1504. /**
  1505. * Register/save who is sharing the depth buffer.
  1506. */
  1507. void CLP(GraphicsBuffer)::
  1508. register_shared_depth_buffer(GraphicsOutput *graphics_output) {
  1509. CLP(GraphicsBuffer) *input_graphics_output;
  1510. input_graphics_output = DCAST (CLP(GraphicsBuffer), graphics_output);
  1511. if (input_graphics_output) {
  1512. // add to list
  1513. _shared_depth_buffer_list.push_back(input_graphics_output);
  1514. }
  1515. }
  1516. /**
  1517. * Unregister who is sharing the depth buffer.
  1518. */
  1519. void CLP(GraphicsBuffer)::
  1520. unregister_shared_depth_buffer(GraphicsOutput *graphics_output) {
  1521. CLP(GraphicsBuffer) *input_graphics_output;
  1522. input_graphics_output = DCAST (CLP(GraphicsBuffer), graphics_output);
  1523. if (input_graphics_output) {
  1524. // remove from list
  1525. _shared_depth_buffer_list.remove(input_graphics_output);
  1526. }
  1527. }
  1528. /**
  1529. * Unregister who is sharing the depth buffer.
  1530. */
  1531. void CLP(GraphicsBuffer)::
  1532. report_my_errors(int line, const char *file) {
  1533. if (_gsg == 0) {
  1534. GLenum error_code = glGetError();
  1535. if (error_code != GL_NO_ERROR) {
  1536. GLCAT.error() << file << ", line " << line << ": GL error " << (int)error_code << "\n";
  1537. }
  1538. } else {
  1539. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1540. glgsg->report_my_errors(line, file);
  1541. }
  1542. }
  1543. /**
  1544. * If the host window has been closed, then this buffer is dead too.
  1545. */
  1546. void CLP(GraphicsBuffer)::
  1547. check_host_valid() {
  1548. if (_host != nullptr && !_host->is_valid()) {
  1549. _rb_data_size_bytes = 0;
  1550. if (_rb_context != nullptr) {
  1551. // We must delete this object first, because when the GSG destructs, so
  1552. // will the tracker that this context is attached to.
  1553. _rb_context->update_data_size_bytes(0);
  1554. delete _rb_context;
  1555. _rb_context = nullptr;
  1556. }
  1557. _is_valid = false;
  1558. _gsg.clear();
  1559. _host.clear();
  1560. }
  1561. }
  1562. /**
  1563. * After the frame has been rendered into the multisample buffer, filters it
  1564. * down into the final render buffer.
  1565. */
  1566. void CLP(GraphicsBuffer)::
  1567. resolve_multisamples() {
  1568. nassertv(_fbo.size() > 0);
  1569. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1570. PStatGPUTimer timer(glgsg, _resolve_multisample_pcollector);
  1571. #ifndef OPENGLES_1
  1572. if (gl_enable_memory_barriers) {
  1573. // Issue memory barriers as necessary to make sure that the texture memory
  1574. // is synchronized before we blit to it.
  1575. pvector<CLP(TextureContext)*>::iterator it;
  1576. for (it = _texture_contexts.begin(); it != _texture_contexts.end(); ++it) {
  1577. CLP(TextureContext) *gtc = *it;
  1578. if (gtc != nullptr && gtc->needs_barrier(GL_FRAMEBUFFER_BARRIER_BIT)) {
  1579. glgsg->issue_memory_barrier(GL_FRAMEBUFFER_BARRIER_BIT);
  1580. // If we've done it for one, we've done it for all.
  1581. break;
  1582. }
  1583. }
  1584. }
  1585. #endif
  1586. glgsg->report_my_gl_errors();
  1587. GLuint fbo = _fbo[0];
  1588. if (_bound_tex_page != -1) {
  1589. fbo = _fbo[_bound_tex_page];
  1590. }
  1591. glgsg->_glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, fbo);
  1592. glgsg->_glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, _fbo_multisample);
  1593. glgsg->_current_fbo = fbo;
  1594. // If the depth buffer is shared, resolve it only on the last to render FBO.
  1595. bool do_depth_blit = false;
  1596. if (_rbm[RTP_depth_stencil] != 0 || _rbm[RTP_depth] != 0) {
  1597. if (_shared_depth_buffer) {
  1598. CLP(GraphicsBuffer) *graphics_buffer = nullptr;
  1599. //CLP(GraphicsBuffer) *highest_sort_graphics_buffer = NULL;
  1600. std::list <CLP(GraphicsBuffer) *>::iterator graphics_buffer_iterator;
  1601. int max_sort_order = 0;
  1602. for (graphics_buffer_iterator = _shared_depth_buffer_list.begin();
  1603. graphics_buffer_iterator != _shared_depth_buffer_list.end();
  1604. graphics_buffer_iterator++) {
  1605. graphics_buffer = (*graphics_buffer_iterator);
  1606. if (graphics_buffer) {
  1607. // this call removes the entry from the list
  1608. if (graphics_buffer->get_sort() >= max_sort_order) {
  1609. max_sort_order = graphics_buffer->get_sort();
  1610. //highest_sort_graphics_buffer = graphics_buffer;
  1611. }
  1612. }
  1613. }
  1614. if (max_sort_order == this->get_sort()) {
  1615. do_depth_blit = true;
  1616. }
  1617. } else {
  1618. do_depth_blit = true;
  1619. }
  1620. }
  1621. if (do_depth_blit) {
  1622. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1623. GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT,
  1624. GL_NEAREST);
  1625. } else {
  1626. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1627. GL_COLOR_BUFFER_BIT,
  1628. GL_NEAREST);
  1629. }
  1630. // Now handle the other color buffers.
  1631. #ifndef OPENGLES
  1632. int next = GL_COLOR_ATTACHMENT1_EXT;
  1633. if (_fb_properties.is_stereo()) {
  1634. glReadBuffer(next);
  1635. glDrawBuffer(next);
  1636. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1637. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1638. next += 1;
  1639. }
  1640. for (int i = 0; i < _fb_properties.get_aux_rgba(); ++i) {
  1641. glReadBuffer(next);
  1642. glDrawBuffer(next);
  1643. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1644. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1645. next += 1;
  1646. }
  1647. for (int i = 0; i < _fb_properties.get_aux_hrgba(); ++i) {
  1648. glReadBuffer(next);
  1649. glDrawBuffer(next);
  1650. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1651. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1652. next += 1;
  1653. }
  1654. for (int i = 0; i < _fb_properties.get_aux_float(); ++i) {
  1655. glReadBuffer(next);
  1656. glDrawBuffer(next);
  1657. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1658. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1659. next += 1;
  1660. }
  1661. #endif
  1662. report_my_gl_errors();
  1663. #ifndef OPENGLES
  1664. if (_have_any_color) {
  1665. glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  1666. glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  1667. } else {
  1668. glDrawBuffer(GL_NONE);
  1669. glReadBuffer(GL_NONE);
  1670. }
  1671. #endif
  1672. report_my_gl_errors();
  1673. }