glGraphicsBuffer_src.cxx 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  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. // Mark the GSG as supporting multisampling, so that it will respect an
  584. // AntialiasAttrib with mode M_multisample.
  585. glgsg->_supports_multisample = true;
  586. } else {
  587. glDisable(GL_MULTISAMPLE);
  588. }
  589. #endif // OPENGLES
  590. if (!_have_any_color) {
  591. _fb_properties.set_rgba_bits(0, 0, 0, 0);
  592. }
  593. _rb_context->set_active(true);
  594. _rb_context->update_data_size_bytes(_rb_data_size_bytes);
  595. _initial_clear = false;
  596. report_my_gl_errors();
  597. #ifndef OPENGLES
  598. if (_have_any_color) {
  599. glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  600. glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  601. } else {
  602. glDrawBuffer(GL_NONE);
  603. glReadBuffer(GL_NONE);
  604. }
  605. #endif
  606. _needs_rebuild = false;
  607. report_my_gl_errors();
  608. if (!check_fbo()) {
  609. if (GLCAT.is_debug()) {
  610. GLCAT.debug()
  611. << get_name() << " check_fbo() returns false\n";
  612. }
  613. return;
  614. }
  615. }
  616. /**
  617. * Attaches either a texture or a renderbuffer to the specified bitplane.
  618. */
  619. void CLP(GraphicsBuffer)::
  620. bind_slot(int layer, bool rb_resize, Texture **attach, RenderTexturePlane slot, GLenum attachpoint) {
  621. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  622. Texture *tex = attach[slot];
  623. if (tex && layer >= tex->get_z_size()) {
  624. // If the requested layer index exceeds the number of layers in the
  625. // texture, we will not bind this layer.
  626. tex = nullptr;
  627. }
  628. if (!tex && _rb_size_z > 1) {
  629. // Since there is no such thing as a layered renderbuffer (to my
  630. // knowledge), we have to create a dummy texture to render to if no
  631. // texture was provided.
  632. tex = new Texture();
  633. if (_rb_size_z > 1) {
  634. // Apparently, it doesn't make a difference whether we use
  635. // setup_cube_map or setup_2d_texture_array here, since it's the same
  636. // internal storage.
  637. tex->setup_2d_texture_array(_rb_size_z);
  638. } else {
  639. tex->setup_2d_texture();
  640. }
  641. }
  642. if (tex) {
  643. // Bind the texture to the slot.
  644. tex->set_x_size(_rb_size_x);
  645. tex->set_y_size(_rb_size_y);
  646. if (tex->get_texture_type() != Texture::TT_cube_map && _rb_size_z > 1) {
  647. tex->set_z_size(_rb_size_z);
  648. }
  649. tex->set_pad_size(_rb_size_x - get_x_size(), _rb_size_y - get_y_size());
  650. // Adjust the texture format based on the requested framebuffer settings.
  651. switch (slot) {
  652. case RTP_depth:
  653. _fb_properties.setup_depth_texture(tex);
  654. break;
  655. case RTP_depth_stencil:
  656. tex->set_format(Texture::F_depth_stencil);
  657. if (_fb_properties.get_float_depth()) {
  658. tex->set_component_type(Texture::T_float);
  659. } else {
  660. tex->set_component_type(Texture::T_unsigned_int_24_8);
  661. }
  662. break;
  663. case RTP_aux_hrgba_0:
  664. case RTP_aux_hrgba_1:
  665. case RTP_aux_hrgba_2:
  666. case RTP_aux_hrgba_3:
  667. tex->set_format(Texture::F_rgba16);
  668. tex->set_component_type(Texture::T_float);
  669. break;
  670. case RTP_aux_float_0:
  671. case RTP_aux_float_1:
  672. case RTP_aux_float_2:
  673. case RTP_aux_float_3:
  674. tex->set_format(Texture::F_rgba32);
  675. tex->set_component_type(Texture::T_float);
  676. break;
  677. default:
  678. _fb_properties.setup_color_texture(tex);
  679. }
  680. GLenum target = glgsg->get_texture_target(tex->get_texture_type());
  681. if (target == GL_TEXTURE_CUBE_MAP) {
  682. target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
  683. }
  684. if (attachpoint == GL_DEPTH_ATTACHMENT_EXT) {
  685. if (GLCAT.is_debug()) {
  686. GLCAT.debug() << "Binding texture " << *tex << " to depth attachment.\n";
  687. }
  688. attach_tex(layer, 0, tex, GL_DEPTH_ATTACHMENT_EXT);
  689. #ifndef OPENGLES
  690. GLint depth_size = 0;
  691. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_DEPTH_SIZE, &depth_size);
  692. _fb_properties.set_depth_bits(depth_size);
  693. #endif
  694. if (slot == RTP_depth_stencil) {
  695. if (GLCAT.is_debug()) {
  696. GLCAT.debug() << "Binding texture " << *tex << " to stencil attachment.\n";
  697. }
  698. attach_tex(layer, 0, tex, GL_STENCIL_ATTACHMENT_EXT);
  699. #ifndef OPENGLES
  700. GLint stencil_size = 0;
  701. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_STENCIL_SIZE, &stencil_size);
  702. _fb_properties.set_stencil_bits(stencil_size);
  703. #endif
  704. }
  705. } else {
  706. if (GLCAT.is_debug()) {
  707. GLCAT.debug() << "Binding texture " << *tex << " to color attachment.\n";
  708. }
  709. attach_tex(layer, 0, tex, attachpoint);
  710. #ifndef OPENGLES
  711. if (attachpoint == GL_COLOR_ATTACHMENT0_EXT) {
  712. GLint red_size = 0, green_size = 0, blue_size = 0, alpha_size = 0;
  713. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_RED_SIZE, &red_size);
  714. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_GREEN_SIZE, &green_size);
  715. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_BLUE_SIZE, &blue_size);
  716. glGetTexLevelParameteriv(target, 0, GL_TEXTURE_ALPHA_SIZE, &alpha_size);
  717. _fb_properties.set_rgba_bits(red_size, green_size, blue_size, alpha_size);
  718. }
  719. #endif
  720. }
  721. // If there was a renderbuffer bound to this slot, delete it.
  722. if (_rb[slot] != 0) {
  723. glgsg->_glDeleteRenderbuffers(1, &(_rb[slot]));
  724. _rb[slot] = 0;
  725. }
  726. report_my_gl_errors();
  727. } else {
  728. // No texture to bind. Instead, create a renderbuffer. Choose a suitable
  729. // renderbuffer format based on the requirements.
  730. #ifdef OPENGLES
  731. // OpenGL ES case.
  732. GLuint gl_format = GL_RGBA4;
  733. switch (slot) {
  734. case RTP_depth_stencil:
  735. gl_format = GL_DEPTH24_STENCIL8_OES;
  736. break;
  737. case RTP_depth:
  738. if (_fb_properties.get_depth_bits() > 24 && glgsg->_supports_depth32) {
  739. gl_format = GL_DEPTH_COMPONENT32_OES;
  740. } else if (_fb_properties.get_depth_bits() > 16 && glgsg->_supports_depth24) {
  741. gl_format = GL_DEPTH_COMPONENT24_OES;
  742. } else {
  743. gl_format = GL_DEPTH_COMPONENT16;
  744. }
  745. break;
  746. // NB: we currently use RTP_stencil to store the right eye for stereo.
  747. // case RTP_stencil: gl_format = GL_STENCIL_INDEX8; break
  748. default:
  749. if (_fb_properties.get_alpha_bits() == 0) {
  750. if (_fb_properties.get_color_bits() <= 16) {
  751. gl_format = GL_RGB565_OES;
  752. } else if (_fb_properties.get_color_bits() <= 24) {
  753. gl_format = GL_RGB8_OES;
  754. } else {
  755. gl_format = GL_RGB10_EXT;
  756. }
  757. } else if (_fb_properties.get_color_bits() == 0) {
  758. gl_format = GL_ALPHA8_EXT;
  759. } else if (_fb_properties.get_color_bits() <= 12
  760. && _fb_properties.get_alpha_bits() <= 4) {
  761. gl_format = GL_RGBA4_OES;
  762. } else if (_fb_properties.get_color_bits() <= 15
  763. && _fb_properties.get_alpha_bits() == 1) {
  764. gl_format = GL_RGB5_A1_OES;
  765. } else if (_fb_properties.get_color_bits() <= 30
  766. && _fb_properties.get_alpha_bits() <= 2) {
  767. gl_format = GL_RGB10_A2_EXT;
  768. } else {
  769. gl_format = GL_RGBA8_OES;
  770. }
  771. }
  772. #else
  773. GLuint gl_format = GL_RGBA;
  774. switch (slot) {
  775. case RTP_depth_stencil:
  776. if (_fb_properties.get_depth_bits() > 24 ||
  777. _fb_properties.get_float_depth()) {
  778. if (!glgsg->_use_remapped_depth_range) {
  779. gl_format = GL_DEPTH32F_STENCIL8;
  780. } else {
  781. gl_format = GL_DEPTH32F_STENCIL8_NV;
  782. }
  783. } else {
  784. gl_format = GL_DEPTH24_STENCIL8;
  785. }
  786. break;
  787. case RTP_depth:
  788. if (_fb_properties.get_float_depth()) {
  789. if (!glgsg->_use_remapped_depth_range) {
  790. gl_format = GL_DEPTH_COMPONENT32F;
  791. } else {
  792. gl_format = GL_DEPTH_COMPONENT32F_NV;
  793. }
  794. } else if (_fb_properties.get_depth_bits() > 24) {
  795. gl_format = GL_DEPTH_COMPONENT32;
  796. } else if (_fb_properties.get_depth_bits() > 16) {
  797. gl_format = GL_DEPTH_COMPONENT24;
  798. } else if (_fb_properties.get_depth_bits() > 1) {
  799. gl_format = GL_DEPTH_COMPONENT16;
  800. } else {
  801. gl_format = GL_DEPTH_COMPONENT;
  802. }
  803. break;
  804. case RTP_aux_rgba_0:
  805. case RTP_aux_rgba_1:
  806. case RTP_aux_rgba_2:
  807. case RTP_aux_rgba_3:
  808. gl_format = GL_RGBA;
  809. break;
  810. case RTP_aux_hrgba_0:
  811. case RTP_aux_hrgba_1:
  812. case RTP_aux_hrgba_2:
  813. case RTP_aux_hrgba_3:
  814. gl_format = GL_RGBA16F_ARB;
  815. break;
  816. case RTP_aux_float_0:
  817. case RTP_aux_float_1:
  818. case RTP_aux_float_2:
  819. case RTP_aux_float_3:
  820. gl_format = GL_RGBA32F_ARB;
  821. break;
  822. default:
  823. if (_fb_properties.get_alpha_bits() == 0) {
  824. if (_fb_properties.get_srgb_color()) {
  825. gl_format = GL_SRGB8;
  826. } else if (_fb_properties.get_color_bits() > 16 * 3 ||
  827. _fb_properties.get_red_bits() > 16 ||
  828. _fb_properties.get_green_bits() > 16 ||
  829. _fb_properties.get_blue_bits() > 16) {
  830. // 32-bit, which is always floating-point.
  831. if (_fb_properties.get_blue_bits() > 0 ||
  832. _fb_properties.get_color_bits() == 1 ||
  833. _fb_properties.get_color_bits() > 32 * 2) {
  834. gl_format = GL_RGB32F;
  835. } else if (_fb_properties.get_green_bits() > 0 ||
  836. _fb_properties.get_color_bits() > 32) {
  837. gl_format = GL_RG32F;
  838. } else {
  839. gl_format = GL_R32F;
  840. }
  841. } else if (_fb_properties.get_float_color()) {
  842. // 16-bit floating-point.
  843. if (_fb_properties.get_blue_bits() > 0 ||
  844. _fb_properties.get_color_bits() == 1 ||
  845. _fb_properties.get_color_bits() > 16 * 2) {
  846. gl_format = GL_RGB16F;
  847. } else if (_fb_properties.get_green_bits() > 0 ||
  848. _fb_properties.get_color_bits() > 16) {
  849. gl_format = GL_RG16F;
  850. } else {
  851. gl_format = GL_R16F;
  852. }
  853. } else if (_fb_properties.get_color_bits() > 8 * 3) {
  854. gl_format = GL_RGB16_EXT;
  855. } else {
  856. gl_format = GL_RGB;
  857. }
  858. } else {
  859. if (_fb_properties.get_srgb_color()) {
  860. gl_format = GL_SRGB8_ALPHA8;
  861. } else if (_fb_properties.get_float_color()) {
  862. if (_fb_properties.get_color_bits() > 16 * 3) {
  863. gl_format = GL_RGBA32F_ARB;
  864. } else {
  865. gl_format = GL_RGBA16F_ARB;
  866. }
  867. } else {
  868. if (_fb_properties.get_color_bits() > 16 * 3) {
  869. gl_format = GL_RGBA32F_ARB;
  870. } else if (_fb_properties.get_color_bits() > 8 * 3) {
  871. gl_format = GL_RGBA16_EXT;
  872. } else {
  873. gl_format = GL_RGBA;
  874. }
  875. }
  876. }
  877. };
  878. #endif
  879. // If there's no renderbuffer for this slot, create one.
  880. if (_rb[slot] == 0) {
  881. glgsg->_glGenRenderbuffers(1, &(_rb[slot]));
  882. }
  883. // Allocate and bind the renderbuffer.
  884. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, _rb[slot]);
  885. if (slot == RTP_depth_stencil) {
  886. if (GLCAT.is_debug()) {
  887. GLCAT.debug() << "Creating depth stencil renderbuffer.\n";
  888. }
  889. // Allocate renderbuffer storage for depth stencil.
  890. GLint depth_size = 0, stencil_size = 0;
  891. glgsg->_glRenderbufferStorage(GL_RENDERBUFFER_EXT, gl_format, _rb_size_x, _rb_size_y);
  892. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &depth_size);
  893. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_STENCIL_SIZE_EXT, &stencil_size);
  894. _fb_properties.set_depth_bits(depth_size);
  895. _fb_properties.set_stencil_bits(stencil_size);
  896. _rb_data_size_bytes += _rb_size_x * _rb_size_y * ((depth_size + stencil_size) / 8);
  897. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  898. GLuint rb = _rb[slot];
  899. if (_shared_depth_buffer) {
  900. rb = _shared_depth_buffer->_rb[slot];
  901. }
  902. glgsg->_glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  903. GL_RENDERBUFFER_EXT, rb);
  904. glgsg->_glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  905. GL_RENDERBUFFER_EXT, rb);
  906. report_my_gl_errors();
  907. } else if (slot == RTP_depth) {
  908. if (GLCAT.is_debug()) {
  909. GLCAT.debug() << "Creating depth renderbuffer.\n";
  910. }
  911. // Allocate renderbuffer storage for regular depth.
  912. GLint depth_size = 0;
  913. glgsg->_glRenderbufferStorage(GL_RENDERBUFFER_EXT, gl_format, _rb_size_x, _rb_size_y);
  914. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &depth_size);
  915. #ifndef OPENGLES
  916. // Are we getting only 24 bits of depth when we requested 32? It may be
  917. // because GL_DEPTH_COMPONENT32 is not a required format, while 32F is.
  918. if (gl_format == GL_DEPTH_COMPONENT32 && depth_size < 32) {
  919. if (!glgsg->_use_remapped_depth_range) {
  920. gl_format = GL_DEPTH_COMPONENT32F;
  921. } else {
  922. gl_format = GL_DEPTH_COMPONENT32F_NV;
  923. }
  924. glgsg->_glRenderbufferStorage(GL_RENDERBUFFER_EXT, gl_format, _rb_size_x, _rb_size_y);
  925. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_DEPTH_SIZE_EXT, &depth_size);
  926. _fb_properties.set_float_depth(true);
  927. }
  928. #endif
  929. _fb_properties.set_depth_bits(depth_size);
  930. _rb_data_size_bytes += _rb_size_x * _rb_size_y * (depth_size / 8);
  931. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  932. GLuint rb = _rb[slot];
  933. if (_shared_depth_buffer) {
  934. rb = _shared_depth_buffer->_rb[slot];
  935. }
  936. glgsg->_glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  937. GL_RENDERBUFFER_EXT, rb);
  938. report_my_gl_errors();
  939. } else {
  940. if (GLCAT.is_debug()) {
  941. GLCAT.debug() << "Creating color renderbuffer.\n";
  942. }
  943. glgsg->_glRenderbufferStorage(GL_RENDERBUFFER_EXT, gl_format, _rb_size_x, _rb_size_y);
  944. GLint red_size = 0, green_size = 0, blue_size = 0, alpha_size = 0;
  945. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_RED_SIZE_EXT, &red_size);
  946. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_GREEN_SIZE_EXT, &green_size);
  947. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_BLUE_SIZE_EXT, &blue_size);
  948. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_ALPHA_SIZE_EXT, &alpha_size);
  949. if (attachpoint == GL_COLOR_ATTACHMENT0_EXT) {
  950. _fb_properties.set_rgba_bits(red_size, green_size, blue_size, alpha_size);
  951. }
  952. _rb_data_size_bytes += _rb_size_x * _rb_size_y * ((red_size + green_size + blue_size + alpha_size) / 8);
  953. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  954. glgsg->_glFramebufferRenderbuffer(GL_FRAMEBUFFER_EXT, attachpoint,
  955. GL_RENDERBUFFER_EXT, _rb[slot]);
  956. report_my_gl_errors();
  957. }
  958. }
  959. }
  960. /**
  961. * Attaches incoming Texture or renderbuffer to the required bitplanes for the
  962. * 2 FBOs comprising a multisample graphics buffer.
  963. */
  964. void CLP(GraphicsBuffer)::
  965. bind_slot_multisample(bool rb_resize, Texture **attach, RenderTexturePlane slot, GLenum attachpoint) {
  966. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  967. if ((_rbm[slot] != 0)&&(!rb_resize)) {
  968. return;
  969. }
  970. if (_rbm[slot] != 0) {
  971. glgsg->_glDeleteRenderbuffers(1, &(_rbm[slot]));
  972. _rbm[slot] = 0;
  973. }
  974. glgsg->_glBindFramebuffer(GL_FRAMEBUFFER_EXT, _fbo_multisample);
  975. glgsg->_glGenRenderbuffers(1, &(_rbm[slot]));
  976. // Allocate and bind the renderbuffer.
  977. Texture *tex = attach[slot]; // If there is a texture map, use it's format as needed.
  978. if (attachpoint == GL_DEPTH_ATTACHMENT_EXT) {
  979. #ifndef OPENGLES_2
  980. if (_use_depth_stencil) {
  981. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, _rbm[slot]);
  982. if (_requested_coverage_samples) {
  983. glgsg->_glRenderbufferStorageMultisampleCoverage(GL_RENDERBUFFER_EXT, _requested_coverage_samples,
  984. _requested_multisamples, GL_DEPTH_STENCIL_EXT,
  985. _rb_size_x, _rb_size_y);
  986. } else {
  987. glgsg->_glRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, _requested_multisamples, GL_DEPTH_STENCIL_EXT,
  988. _rb_size_x, _rb_size_y);
  989. }
  990. #ifndef OPENGLES
  991. GLint givenSamples = -1;
  992. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &givenSamples);
  993. #endif
  994. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  995. glgsg->_glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  996. GL_RENDERBUFFER_EXT, _rbm[slot]);
  997. glgsg->_glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT,
  998. GL_RENDERBUFFER_EXT, _rbm[slot]);
  999. } else {
  1000. #endif
  1001. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, _rbm[slot]);
  1002. GLuint format = GL_DEPTH_COMPONENT;
  1003. #ifndef OPENGLES
  1004. if (_fb_properties.get_float_depth()) {
  1005. if (!glgsg->_use_remapped_depth_range) {
  1006. format = GL_DEPTH_COMPONENT32F;
  1007. } else {
  1008. format = GL_DEPTH_COMPONENT32F_NV;
  1009. }
  1010. } else
  1011. #endif
  1012. if (tex) {
  1013. switch (tex->get_format()) {
  1014. case Texture::F_depth_component16:
  1015. format = GL_DEPTH_COMPONENT16;
  1016. break;
  1017. case Texture::F_depth_component24:
  1018. format = GL_DEPTH_COMPONENT24;
  1019. break;
  1020. case Texture::F_depth_component32:
  1021. format = GL_DEPTH_COMPONENT32;
  1022. break;
  1023. default:
  1024. break;
  1025. }
  1026. }
  1027. if (_requested_coverage_samples) {
  1028. glgsg->_glRenderbufferStorageMultisampleCoverage(GL_RENDERBUFFER_EXT, _requested_coverage_samples,
  1029. _requested_multisamples, format,
  1030. _rb_size_x, _rb_size_y);
  1031. } else {
  1032. glgsg->_glRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, _requested_multisamples, format,
  1033. _rb_size_x, _rb_size_y);
  1034. #ifndef OPENGLES_2
  1035. }
  1036. #endif
  1037. #ifndef OPENGLES
  1038. GLint givenSamples = -1;
  1039. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &givenSamples);
  1040. #endif
  1041. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  1042. glgsg->_glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT,
  1043. GL_RENDERBUFFER_EXT, _rbm[slot]);
  1044. }
  1045. } else {
  1046. GLuint gl_format = GL_RGBA;
  1047. #ifndef OPENGLES
  1048. switch (slot) {
  1049. case RTP_aux_hrgba_0:
  1050. case RTP_aux_hrgba_1:
  1051. case RTP_aux_hrgba_2:
  1052. case RTP_aux_hrgba_3:
  1053. gl_format = GL_RGBA16F_ARB;
  1054. break;
  1055. case RTP_aux_float_0:
  1056. case RTP_aux_float_1:
  1057. case RTP_aux_float_2:
  1058. case RTP_aux_float_3:
  1059. gl_format = GL_RGBA32F_ARB;
  1060. break;
  1061. case RTP_aux_rgba_0:
  1062. case RTP_aux_rgba_1:
  1063. case RTP_aux_rgba_2:
  1064. case RTP_aux_rgba_3:
  1065. default:
  1066. if (_fb_properties.get_srgb_color()) {
  1067. gl_format = GL_SRGB8_ALPHA8;
  1068. } else if (_fb_properties.get_float_color()) {
  1069. gl_format = GL_RGBA32F_ARB;
  1070. } else {
  1071. gl_format = GL_RGBA;
  1072. }
  1073. break;
  1074. }
  1075. #endif
  1076. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, _rbm[slot]);
  1077. if (_requested_coverage_samples) {
  1078. glgsg->_glRenderbufferStorageMultisampleCoverage(GL_RENDERBUFFER_EXT, _requested_coverage_samples,
  1079. _requested_multisamples, gl_format, _rb_size_x, _rb_size_y);
  1080. } else {
  1081. glgsg->_glRenderbufferStorageMultisample(GL_RENDERBUFFER_EXT, _requested_multisamples, gl_format,
  1082. _rb_size_x, _rb_size_y);
  1083. }
  1084. #ifndef OPENGLES
  1085. GLint givenSamples = -1;
  1086. glgsg->_glGetRenderbufferParameteriv(GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &givenSamples);
  1087. #endif
  1088. glgsg->_glBindRenderbuffer(GL_RENDERBUFFER_EXT, 0);
  1089. glgsg->_glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER_EXT, attachpoint,
  1090. GL_RENDERBUFFER_EXT, _rbm[slot]);
  1091. }
  1092. glgsg->report_my_gl_errors();
  1093. }
  1094. /**
  1095. * This function attaches the given texture to the given attachment point.
  1096. */
  1097. void CLP(GraphicsBuffer)::
  1098. attach_tex(int layer, int view, Texture *attach, GLenum attachpoint) {
  1099. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1100. if (view >= attach->get_num_views()) {
  1101. attach->set_num_views(view + 1);
  1102. }
  1103. // Create the OpenGL texture object.
  1104. TextureContext *tc = attach->prepare_now(view, glgsg->get_prepared_objects(), glgsg);
  1105. nassertv(tc != nullptr);
  1106. CLP(TextureContext) *gtc = DCAST(CLP(TextureContext), tc);
  1107. glgsg->update_texture(gtc, true);
  1108. gtc->set_active(true);
  1109. _texture_contexts.push_back(gtc);
  1110. // It seems that binding the texture is necessary before binding to a
  1111. // framebuffer attachment.
  1112. glgsg->apply_texture(gtc);
  1113. #if !defined(OPENGLES) && defined(SUPPORT_FIXED_FUNCTION)
  1114. if (glgsg->has_fixed_function_pipeline()) {
  1115. GLclampf priority = 1.0f;
  1116. glPrioritizeTextures(1, &gtc->_index, &priority);
  1117. }
  1118. #endif
  1119. #ifndef OPENGLES
  1120. if (_rb_size_z != 1) {
  1121. // Bind all of the layers of the texture.
  1122. nassertv(glgsg->_glFramebufferTexture != nullptr);
  1123. glgsg->_glFramebufferTexture(GL_FRAMEBUFFER_EXT, attachpoint,
  1124. gtc->_index, 0);
  1125. return;
  1126. }
  1127. #endif
  1128. GLenum target = glgsg->get_texture_target(attach->get_texture_type());
  1129. if (target == GL_TEXTURE_CUBE_MAP) {
  1130. target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + layer;
  1131. }
  1132. switch (target) {
  1133. #ifndef OPENGLES_1
  1134. case GL_TEXTURE_3D:
  1135. glgsg->_glFramebufferTexture3D(GL_FRAMEBUFFER_EXT, attachpoint,
  1136. target, gtc->_index, 0, layer);
  1137. break;
  1138. case GL_TEXTURE_2D_ARRAY:
  1139. glgsg->_glFramebufferTextureLayer(GL_FRAMEBUFFER_EXT, attachpoint,
  1140. gtc->_index, 0, layer);
  1141. break;
  1142. #endif
  1143. default:
  1144. glgsg->_glFramebufferTexture2D(GL_FRAMEBUFFER_EXT, attachpoint,
  1145. target, gtc->_index, 0);
  1146. }
  1147. }
  1148. /**
  1149. * This function will be called within the draw thread after rendering is
  1150. * completed for a given frame. If we've just rendered into level zero of a
  1151. * mipmapped texture, then all subsequent mipmap levels will now be
  1152. * calculated.
  1153. */
  1154. void CLP(GraphicsBuffer)::
  1155. generate_mipmaps() {
  1156. if (gl_ignore_mipmaps && !gl_force_mipmaps) {
  1157. return;
  1158. }
  1159. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1160. // PStatGPUTimer timer(glgsg, _generate_mipmap_pcollector);
  1161. pvector<CLP(TextureContext)*>::iterator it;
  1162. for (it = _texture_contexts.begin(); it != _texture_contexts.end(); ++it) {
  1163. CLP(TextureContext) *gtc = *it;
  1164. if (gtc->_generate_mipmaps) {
  1165. glgsg->generate_mipmaps(gtc);
  1166. }
  1167. }
  1168. report_my_gl_errors();
  1169. }
  1170. /**
  1171. * This function will be called within the draw thread after rendering is
  1172. * completed for a given frame. It should do whatever finalization is
  1173. * required.
  1174. */
  1175. void CLP(GraphicsBuffer)::
  1176. end_frame(FrameMode mode, Thread *current_thread) {
  1177. end_frame_spam(mode);
  1178. nassertv(_gsg != nullptr);
  1179. // Resolve Multisample rendering if using it.
  1180. if (_requested_multisamples && _fbo_multisample) {
  1181. resolve_multisamples();
  1182. }
  1183. if (mode == FM_render) {
  1184. copy_to_textures();
  1185. }
  1186. // Unbind the FBO. TODO: calling bind_fbo is slow, so we should probably
  1187. // move this to begin_frame to prevent unnecessary calls.
  1188. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1189. glgsg->bind_fbo(0);
  1190. _bound_tex_page = -1;
  1191. if (mode == FM_render) {
  1192. generate_mipmaps();
  1193. }
  1194. if (_host != nullptr) {
  1195. _host->end_frame(FM_parasite, current_thread);
  1196. } else {
  1197. glgsg->end_frame(current_thread);
  1198. }
  1199. if (mode == FM_render) {
  1200. trigger_flip();
  1201. clear_cube_map_selection();
  1202. }
  1203. report_my_gl_errors();
  1204. glgsg->pop_group_marker();
  1205. }
  1206. /**
  1207. *
  1208. */
  1209. void CLP(GraphicsBuffer)::
  1210. set_size(int x, int y) {
  1211. if (_size.get_x() != x || _size.get_y() != y) {
  1212. _needs_rebuild = true;
  1213. }
  1214. set_size_and_recalc(x, y);
  1215. }
  1216. /**
  1217. * Called internally when the window is in render-to-a-texture mode and we are
  1218. * in the process of rendering the six faces of a cube map, or any other
  1219. * multi-page texture. This should do whatever needs to be done to switch the
  1220. * buffer to the indicated page.
  1221. */
  1222. void CLP(GraphicsBuffer)::
  1223. select_target_tex_page(int page) {
  1224. nassertv(page >= 0 && (size_t)page < _fbo.size());
  1225. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1226. bool switched_page = (_bound_tex_page != page);
  1227. if (switched_page) {
  1228. if (_bound_tex_page != -1) {
  1229. // Resolve the multisample rendering for the previous face.
  1230. if (_requested_multisamples && _fbo_multisample) {
  1231. resolve_multisamples();
  1232. }
  1233. }
  1234. if (_fbo_multisample != 0) {
  1235. // TODO: re-issue clears?
  1236. } else {
  1237. glgsg->bind_fbo(_fbo[page]);
  1238. }
  1239. _bound_tex_page = page;
  1240. }
  1241. report_my_gl_errors();
  1242. }
  1243. /**
  1244. * Opens the window right now. Called from the window thread. Returns true
  1245. * if the window is successfully opened, or false if there was a problem.
  1246. */
  1247. bool CLP(GraphicsBuffer)::
  1248. open_buffer() {
  1249. report_my_gl_errors();
  1250. // Double check that we have a valid gsg
  1251. nassertr(_gsg != nullptr, false);
  1252. if (!_gsg->is_valid()) {
  1253. return false;
  1254. }
  1255. // Count total color buffers.
  1256. int totalcolor =
  1257. (_fb_properties.get_stereo() ? 2 : 1) +
  1258. _fb_properties.get_aux_rgba() +
  1259. _fb_properties.get_aux_hrgba() +
  1260. _fb_properties.get_aux_float();
  1261. // Check for support of relevant extensions.
  1262. CLP(GraphicsStateGuardian) *glgsg;
  1263. DCAST_INTO_R(glgsg, _gsg, false);
  1264. if (!glgsg->_supports_framebuffer_object) {
  1265. return false;
  1266. }
  1267. if (_rb_context == nullptr) {
  1268. _rb_context = new BufferContext(&(glgsg->_renderbuffer_residency), nullptr);
  1269. }
  1270. /*
  1271. * Describe the framebuffer properties of the FBO. Unfortunately, we can't
  1272. * currently predict which formats the implementation will allow us to use at
  1273. * this point, so we'll just have to make some guesses and parrot the rest of
  1274. * the properties back to the user. When we actually attach the textures,
  1275. * we'll update the properties more appropriately.
  1276. */
  1277. // Rounding the depth bits is not spectacular, but at least we're telling
  1278. // the user *something* about what we're going to get.
  1279. // A lot of code seems to depend on being able to get a color buffer by just
  1280. // setting the rgb_color bit.
  1281. if (_fb_properties.get_color_bits() == 0 &&
  1282. _fb_properties.get_rgb_color()) {
  1283. _fb_properties.set_color_bits(1);
  1284. _fb_properties.set_red_bits(1);
  1285. _fb_properties.set_green_bits(1);
  1286. _fb_properties.set_blue_bits(1);
  1287. }
  1288. // Actually, let's always get a colour buffer for now until we figure out
  1289. // why Intel HD Graphics cards complain otherwise.
  1290. if (gl_force_fbo_color && _fb_properties.get_color_bits() == 0) {
  1291. _fb_properties.set_color_bits(1);
  1292. }
  1293. if (_fb_properties.get_depth_bits() > 24) {
  1294. _fb_properties.set_depth_bits(32);
  1295. } else if (_fb_properties.get_depth_bits() > 16) {
  1296. _fb_properties.set_depth_bits(24);
  1297. } else if (_fb_properties.get_depth_bits() > 0) {
  1298. _fb_properties.set_depth_bits(16);
  1299. } else {
  1300. _fb_properties.set_depth_bits(0);
  1301. }
  1302. // We're not going to get more than this, ever. At least not until OpenGL
  1303. // introduces 64-bit texture formats.
  1304. if (_fb_properties.get_color_bits() > 96) {
  1305. _fb_properties.set_color_bits(96);
  1306. }
  1307. if (_fb_properties.get_red_bits() > 32) {
  1308. _fb_properties.set_red_bits(32);
  1309. }
  1310. if (_fb_properties.get_green_bits() > 32) {
  1311. _fb_properties.set_green_bits(32);
  1312. }
  1313. if (_fb_properties.get_blue_bits() > 32) {
  1314. _fb_properties.set_blue_bits(32);
  1315. }
  1316. if (_fb_properties.get_alpha_bits() > 32) {
  1317. _fb_properties.set_alpha_bits(32);
  1318. }
  1319. if (_fb_properties.get_float_depth()) {
  1320. // GL_DEPTH_COMPONENT32F seems the only depth float format.
  1321. _fb_properties.set_depth_bits(32);
  1322. }
  1323. // We currently only support color formats this big as float.
  1324. if (_fb_properties.get_color_bits() > 16 * 3) {
  1325. _fb_properties.set_float_color(true);
  1326. }
  1327. if (_fb_properties.get_srgb_color()) {
  1328. // This is the only sRGB color format OpenGL supports.
  1329. _fb_properties.set_rgba_bits(8, 8, 8,
  1330. (_fb_properties.get_alpha_bits() > 0) ? 8 : 0);
  1331. _fb_properties.set_float_color(false);
  1332. }
  1333. if (!_gsg->get_supports_depth_stencil()) {
  1334. // At least we know we won't be getting stencil bits.
  1335. _fb_properties.set_stencil_bits(0);
  1336. } else if (_fb_properties.get_stencil_bits() > 0) {
  1337. // We don't currently support stencil-only targets.
  1338. _fb_properties.set_stencil_bits(8);
  1339. if (_fb_properties.get_depth_bits() < 24) {
  1340. _fb_properties.set_depth_bits(24);
  1341. }
  1342. }
  1343. // Accumulation buffers aren't supported for FBOs.
  1344. _fb_properties.set_accum_bits(0);
  1345. if (glgsg->get_supports_framebuffer_multisample() && glgsg->get_supports_framebuffer_blit()) {
  1346. _requested_multisamples = _fb_properties.get_multisamples();
  1347. } else {
  1348. _requested_multisamples = 0;
  1349. }
  1350. #ifndef OPENGLES
  1351. if (glgsg->get_supports_framebuffer_multisample_coverage_nv() && glgsg->get_supports_framebuffer_blit()) {
  1352. _requested_coverage_samples = _fb_properties.get_coverage_samples();
  1353. // Note: Only 4 and 8 actual samples are supported by the extension, with
  1354. // 8 or 16 coverage samples.
  1355. if ((_requested_coverage_samples <= 8) && (_requested_coverage_samples > 0)) {
  1356. _requested_multisamples = 4;
  1357. _requested_coverage_samples = 8;
  1358. } else if (_requested_coverage_samples > 8) {
  1359. if (_requested_multisamples < 8) {
  1360. _requested_multisamples = 4;
  1361. } else {
  1362. _requested_multisamples = 8;
  1363. }
  1364. _requested_coverage_samples = 16;
  1365. }
  1366. } else {
  1367. _requested_coverage_samples = 0;
  1368. }
  1369. #endif
  1370. if (_requested_multisamples > glgsg->_max_fb_samples) {
  1371. _requested_multisamples = glgsg->_max_fb_samples;
  1372. }
  1373. _fb_properties.set_multisamples(_requested_multisamples);
  1374. _fb_properties.set_coverage_samples(_requested_coverage_samples);
  1375. // Update aux settings to reflect the GL_MAX_DRAW_BUFFERS limit, if we
  1376. // exceed it, that is.
  1377. int availcolor = glgsg->_max_color_targets;
  1378. if (totalcolor > availcolor) {
  1379. int aux_rgba = _fb_properties.get_aux_rgba();
  1380. int aux_hrgba = _fb_properties.get_aux_hrgba();
  1381. int aux_float = _fb_properties.get_aux_float();
  1382. if (_fb_properties.get_color_bits() > 0 && availcolor > 0) {
  1383. --availcolor;
  1384. if (_fb_properties.is_stereo()) {
  1385. if (availcolor > 0) {
  1386. --availcolor;
  1387. } else {
  1388. _fb_properties.set_stereo(0);
  1389. }
  1390. }
  1391. }
  1392. aux_rgba = min(aux_rgba, availcolor);
  1393. availcolor -= aux_rgba;
  1394. aux_hrgba = min(aux_hrgba, availcolor);
  1395. availcolor -= aux_hrgba;
  1396. aux_float = min(aux_float, availcolor);
  1397. availcolor -= aux_float;
  1398. _fb_properties.set_aux_rgba(aux_rgba);
  1399. _fb_properties.set_aux_hrgba(aux_hrgba);
  1400. _fb_properties.set_aux_float(aux_float);
  1401. }
  1402. _fb_properties.set_back_buffers(0);
  1403. _fb_properties.set_indexed_color(0);
  1404. _fb_properties.set_rgb_color(1);
  1405. if (_host != nullptr) {
  1406. _fb_properties.set_force_hardware(_host->get_fb_properties().get_force_hardware());
  1407. _fb_properties.set_force_software(_host->get_fb_properties().get_force_software());
  1408. }
  1409. _is_valid = true;
  1410. _needs_rebuild = true;
  1411. report_my_gl_errors();
  1412. return true;
  1413. }
  1414. /**
  1415. * This is normally called only from within make_texture_buffer(). When
  1416. * called on a ParasiteBuffer, it returns the host of that buffer; but when
  1417. * called on some other buffer, it returns the buffer itself.
  1418. */
  1419. GraphicsOutput *CLP(GraphicsBuffer)::
  1420. get_host() {
  1421. return (_host != nullptr) ? _host : this;
  1422. }
  1423. /**
  1424. * Closes the buffer right now. Called from the window thread.
  1425. */
  1426. void CLP(GraphicsBuffer)::
  1427. close_buffer() {
  1428. _rb_data_size_bytes = 0;
  1429. if (_rb_context != nullptr) {
  1430. _rb_context->update_data_size_bytes(0);
  1431. delete _rb_context;
  1432. _rb_context = nullptr;
  1433. }
  1434. check_host_valid();
  1435. if (_gsg == 0) {
  1436. return;
  1437. }
  1438. // Get the glgsg.
  1439. CLP(GraphicsStateGuardian) *glgsg;
  1440. DCAST_INTO_V(glgsg, _gsg);
  1441. report_my_gl_errors();
  1442. // Delete the renderbuffers.
  1443. for (int i=0; i<RTP_COUNT; i++) {
  1444. if (_rb[i] != 0) {
  1445. glgsg->_glDeleteRenderbuffers(1, &(_rb[i]));
  1446. _rb[i] = 0;
  1447. }
  1448. }
  1449. // Delete the multisample renderbuffers.
  1450. for (int i=0; i<RTP_COUNT; i++) {
  1451. if (_rbm[i] != 0) {
  1452. glgsg->_glDeleteRenderbuffers(1, &(_rbm[i]));
  1453. _rb[i] = 0;
  1454. }
  1455. }
  1456. _rb_size_x = 0;
  1457. _rb_size_y = 0;
  1458. report_my_gl_errors();
  1459. // Delete the FBO itself.
  1460. if (!_fbo.empty()) {
  1461. glgsg->_glDeleteFramebuffers(_fbo.size(), _fbo.data());
  1462. _fbo.clear();
  1463. }
  1464. report_my_gl_errors();
  1465. // Release the Gsg
  1466. _gsg.clear();
  1467. _is_valid = false;
  1468. }
  1469. /**
  1470. * Will attempt to use the depth buffer of the input graphics_output. The
  1471. * buffer sizes must be exactly the same.
  1472. */
  1473. bool CLP(GraphicsBuffer)::
  1474. share_depth_buffer(GraphicsOutput *graphics_output) {
  1475. bool state;
  1476. CLP(GraphicsBuffer) *input_graphics_output;
  1477. state = false;
  1478. input_graphics_output = DCAST(CLP(GraphicsBuffer), graphics_output);
  1479. if (this != input_graphics_output && input_graphics_output) {
  1480. state = true;
  1481. this->unshare_depth_buffer();
  1482. // Make sure that the buffers are both FBOs.
  1483. if (!input_graphics_output->is_of_type(CLP(GraphicsBuffer)::get_class_type())) {
  1484. GLCAT.error() << "share_depth_buffer: non-matching type\n";
  1485. state = false;
  1486. }
  1487. // Check buffer size.
  1488. if (this->get_x_size() != input_graphics_output->get_x_size()) {
  1489. GLCAT.error() << "share_depth_buffer: non-matching width\n";
  1490. state = false;
  1491. }
  1492. if (this->get_y_size() != input_graphics_output->get_y_size()) {
  1493. GLCAT.error() << "share_depth_buffer: non-matching height\n";
  1494. state = false;
  1495. }
  1496. // Check multisample compatibility.
  1497. if (this->get_multisample_count() != input_graphics_output->get_multisample_count()) {
  1498. GLCAT.error() << "share_depth_buffer: non-matching multisamples\n";
  1499. state = false;
  1500. }
  1501. if (this->get_coverage_sample_count() != input_graphics_output->get_coverage_sample_count()) {
  1502. GLCAT.error() << "share_depth_buffer: non-matching coverage samples\n";
  1503. state = false;
  1504. }
  1505. if (state) {
  1506. // let the input GraphicsOutput know that there is an object sharing its
  1507. // depth buffer
  1508. input_graphics_output->register_shared_depth_buffer(this);
  1509. _shared_depth_buffer = input_graphics_output;
  1510. state = true;
  1511. }
  1512. _needs_rebuild = true;
  1513. }
  1514. report_my_gl_errors();
  1515. return state;
  1516. }
  1517. /**
  1518. * Discontinue sharing the depth buffer.
  1519. */
  1520. void CLP(GraphicsBuffer)::
  1521. unshare_depth_buffer() {
  1522. if (_shared_depth_buffer) {
  1523. // let the GraphicsOutput know that this object is no longer sharing its
  1524. // depth buffer
  1525. _shared_depth_buffer->unregister_shared_depth_buffer(this);
  1526. _shared_depth_buffer = 0;
  1527. _needs_rebuild = true;
  1528. }
  1529. }
  1530. /**
  1531. * Returns true if this particular GraphicsOutput can render directly into a
  1532. * texture, or false if it must always copy-to-texture at the end of each
  1533. * frame to achieve this effect.
  1534. */
  1535. bool CLP(GraphicsBuffer)::
  1536. get_supports_render_texture() const {
  1537. // FBO-based buffers, by their nature, can always bind-to-texture.
  1538. return true;
  1539. }
  1540. /**
  1541. * Register/save who is sharing the depth buffer.
  1542. */
  1543. void CLP(GraphicsBuffer)::
  1544. register_shared_depth_buffer(GraphicsOutput *graphics_output) {
  1545. CLP(GraphicsBuffer) *input_graphics_output;
  1546. input_graphics_output = DCAST (CLP(GraphicsBuffer), graphics_output);
  1547. if (input_graphics_output) {
  1548. // add to list
  1549. _shared_depth_buffer_list.push_back(input_graphics_output);
  1550. }
  1551. }
  1552. /**
  1553. * Unregister who is sharing the depth buffer.
  1554. */
  1555. void CLP(GraphicsBuffer)::
  1556. unregister_shared_depth_buffer(GraphicsOutput *graphics_output) {
  1557. CLP(GraphicsBuffer) *input_graphics_output;
  1558. input_graphics_output = DCAST (CLP(GraphicsBuffer), graphics_output);
  1559. if (input_graphics_output) {
  1560. // remove from list
  1561. _shared_depth_buffer_list.remove(input_graphics_output);
  1562. }
  1563. }
  1564. /**
  1565. * Unregister who is sharing the depth buffer.
  1566. */
  1567. void CLP(GraphicsBuffer)::
  1568. report_my_errors(int line, const char *file) {
  1569. if (_gsg == 0) {
  1570. GLenum error_code = glGetError();
  1571. if (error_code != GL_NO_ERROR) {
  1572. GLCAT.error() << file << ", line " << line << ": GL error " << (int)error_code << "\n";
  1573. }
  1574. } else {
  1575. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1576. glgsg->report_my_errors(line, file);
  1577. }
  1578. }
  1579. /**
  1580. * If the host window has been closed, then this buffer is dead too.
  1581. */
  1582. void CLP(GraphicsBuffer)::
  1583. check_host_valid() {
  1584. if (_host != nullptr && !_host->is_valid()) {
  1585. _rb_data_size_bytes = 0;
  1586. if (_rb_context != nullptr) {
  1587. // We must delete this object first, because when the GSG destructs, so
  1588. // will the tracker that this context is attached to.
  1589. _rb_context->update_data_size_bytes(0);
  1590. delete _rb_context;
  1591. _rb_context = nullptr;
  1592. }
  1593. _is_valid = false;
  1594. _gsg.clear();
  1595. _host.clear();
  1596. }
  1597. }
  1598. /**
  1599. * After the frame has been rendered into the multisample buffer, filters it
  1600. * down into the final render buffer.
  1601. */
  1602. void CLP(GraphicsBuffer)::
  1603. resolve_multisamples() {
  1604. nassertv(_fbo.size() > 0);
  1605. CLP(GraphicsStateGuardian) *glgsg = (CLP(GraphicsStateGuardian) *)_gsg.p();
  1606. PStatGPUTimer timer(glgsg, _resolve_multisample_pcollector);
  1607. #ifndef OPENGLES_1
  1608. if (gl_enable_memory_barriers) {
  1609. // Issue memory barriers as necessary to make sure that the texture memory
  1610. // is synchronized before we blit to it.
  1611. pvector<CLP(TextureContext)*>::iterator it;
  1612. for (it = _texture_contexts.begin(); it != _texture_contexts.end(); ++it) {
  1613. CLP(TextureContext) *gtc = *it;
  1614. if (gtc != nullptr && gtc->needs_barrier(GL_FRAMEBUFFER_BARRIER_BIT)) {
  1615. glgsg->issue_memory_barrier(GL_FRAMEBUFFER_BARRIER_BIT);
  1616. // If we've done it for one, we've done it for all.
  1617. break;
  1618. }
  1619. }
  1620. }
  1621. #endif
  1622. glgsg->report_my_gl_errors();
  1623. GLuint fbo = _fbo[0];
  1624. if (_bound_tex_page != -1) {
  1625. fbo = _fbo[_bound_tex_page];
  1626. }
  1627. glgsg->_glBindFramebuffer(GL_DRAW_FRAMEBUFFER_EXT, fbo);
  1628. glgsg->_glBindFramebuffer(GL_READ_FRAMEBUFFER_EXT, _fbo_multisample);
  1629. glgsg->_current_fbo = fbo;
  1630. // If the depth buffer is shared, resolve it only on the last to render FBO.
  1631. bool do_depth_blit = false;
  1632. if (_rbm[RTP_depth_stencil] != 0 || _rbm[RTP_depth] != 0) {
  1633. if (_shared_depth_buffer) {
  1634. CLP(GraphicsBuffer) *graphics_buffer = nullptr;
  1635. //CLP(GraphicsBuffer) *highest_sort_graphics_buffer = NULL;
  1636. std::list <CLP(GraphicsBuffer) *>::iterator graphics_buffer_iterator;
  1637. int max_sort_order = 0;
  1638. for (graphics_buffer_iterator = _shared_depth_buffer_list.begin();
  1639. graphics_buffer_iterator != _shared_depth_buffer_list.end();
  1640. graphics_buffer_iterator++) {
  1641. graphics_buffer = (*graphics_buffer_iterator);
  1642. if (graphics_buffer) {
  1643. // this call removes the entry from the list
  1644. if (graphics_buffer->get_sort() >= max_sort_order) {
  1645. max_sort_order = graphics_buffer->get_sort();
  1646. //highest_sort_graphics_buffer = graphics_buffer;
  1647. }
  1648. }
  1649. }
  1650. if (max_sort_order == this->get_sort()) {
  1651. do_depth_blit = true;
  1652. }
  1653. } else {
  1654. do_depth_blit = true;
  1655. }
  1656. }
  1657. if (do_depth_blit) {
  1658. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1659. GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT,
  1660. GL_NEAREST);
  1661. } else {
  1662. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1663. GL_COLOR_BUFFER_BIT,
  1664. GL_NEAREST);
  1665. }
  1666. // Now handle the other color buffers.
  1667. #ifndef OPENGLES
  1668. int next = GL_COLOR_ATTACHMENT1_EXT;
  1669. if (_fb_properties.is_stereo()) {
  1670. glReadBuffer(next);
  1671. glDrawBuffer(next);
  1672. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1673. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1674. next += 1;
  1675. }
  1676. for (int i = 0; i < _fb_properties.get_aux_rgba(); ++i) {
  1677. glReadBuffer(next);
  1678. glDrawBuffer(next);
  1679. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1680. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1681. next += 1;
  1682. }
  1683. for (int i = 0; i < _fb_properties.get_aux_hrgba(); ++i) {
  1684. glReadBuffer(next);
  1685. glDrawBuffer(next);
  1686. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1687. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1688. next += 1;
  1689. }
  1690. for (int i = 0; i < _fb_properties.get_aux_float(); ++i) {
  1691. glReadBuffer(next);
  1692. glDrawBuffer(next);
  1693. glgsg->_glBlitFramebuffer(0, 0, _rb_size_x, _rb_size_y, 0, 0, _rb_size_x, _rb_size_y,
  1694. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  1695. next += 1;
  1696. }
  1697. #endif
  1698. report_my_gl_errors();
  1699. #ifndef OPENGLES
  1700. if (_have_any_color) {
  1701. glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
  1702. glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  1703. } else {
  1704. glDrawBuffer(GL_NONE);
  1705. glReadBuffer(GL_NONE);
  1706. }
  1707. #endif
  1708. report_my_gl_errors();
  1709. }