glGraphicsBuffer_src.cxx 67 KB

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