gl_fbo.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Emacs style mode select -*- C++ -*-
  2. *-----------------------------------------------------------------------------
  3. *
  4. *
  5. * PrBoom: a Doom port merged with LxDoom and LSDLDoom
  6. * based on BOOM, a modified and improved DOOM engine
  7. * Copyright (C) 1999 by
  8. * id Software, Chi Hoang, Lee Killough, Jim Flynn, Rand Phares, Ty Halderman
  9. * Copyright (C) 1999-2000 by
  10. * Jess Haas, Nicolas Kalkhof, Colin Phipps, Florian Schulze
  11. * Copyright 2005, 2006 by
  12. * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License
  16. * as published by the Free Software Foundation; either version 2
  17. * of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. * 02111-1307, USA.
  28. *
  29. * DESCRIPTION:
  30. * Thanks Roman "Vortex" Marchenko
  31. *---------------------------------------------------------------------
  32. */
  33. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #include "gl_opengl.h"
  37. #include <SDL.h>
  38. #include "gl_intern.h"
  39. #include "i_main.h"
  40. #include "lprintf.h"
  41. dboolean gl_use_FBO = false;
  42. #ifdef USE_FBO_TECHNIQUE
  43. GLuint glSceneImageFBOTexID = 0;
  44. GLuint glDepthBufferFBOTexID = 0;
  45. GLuint glSceneImageTextureFBOTexID = 0;
  46. int SceneInTexture = false;
  47. static dboolean gld_CreateScreenSizeFBO(void);
  48. #endif
  49. //e6y: motion bloor
  50. int gl_motionblur;
  51. int gl_use_motionblur = false;
  52. motion_blur_params_t motion_blur;
  53. #ifdef USE_FBO_TECHNIQUE
  54. void gld_InitMotionBlur(void);
  55. void gld_InitFBO(void)
  56. {
  57. gld_FreeScreenSizeFBO();
  58. gl_use_motionblur = gl_ext_framebuffer_object && gl_motionblur && gl_ext_blend_color;
  59. gl_use_FBO = (gl_ext_framebuffer_object) && (gl_version >= OPENGL_VERSION_1_3) &&
  60. (gl_use_motionblur || !gl_boom_colormaps || gl_has_hires);
  61. if (gl_use_FBO)
  62. {
  63. if (gld_CreateScreenSizeFBO())
  64. {
  65. // motion blur setup
  66. gld_InitMotionBlur();
  67. }
  68. else
  69. {
  70. gld_FreeScreenSizeFBO();
  71. gl_use_FBO = false;
  72. gl_ext_framebuffer_object = false;
  73. }
  74. }
  75. }
  76. static dboolean gld_CreateScreenSizeFBO(void)
  77. {
  78. int status = 0;
  79. GLenum internalFormat;
  80. dboolean attach_stencil = gl_ext_packed_depth_stencil;// && (gl_has_hires || gl_use_motionblur);
  81. if (!gl_ext_framebuffer_object)
  82. return false;
  83. GLEXT_glGenFramebuffersEXT(1, &glSceneImageFBOTexID);
  84. GLEXT_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, glSceneImageFBOTexID);
  85. GLEXT_glGenRenderbuffersEXT(1, &glDepthBufferFBOTexID);
  86. GLEXT_glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, glDepthBufferFBOTexID);
  87. internalFormat = (attach_stencil ? GL_DEPTH_STENCIL_EXT : GL_DEPTH_COMPONENT);
  88. GLEXT_glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, internalFormat, SCREENWIDTH, SCREENHEIGHT);
  89. // attach a renderbuffer to depth attachment point
  90. GLEXT_glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, glDepthBufferFBOTexID);
  91. if (attach_stencil)
  92. {
  93. // attach a renderbuffer to stencil attachment point
  94. GLEXT_glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, glDepthBufferFBOTexID);
  95. }
  96. glGenTextures(1, &glSceneImageTextureFBOTexID);
  97. glBindTexture(GL_TEXTURE_2D, glSceneImageTextureFBOTexID);
  98. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, SCREENWIDTH, SCREENHEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
  99. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  100. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  101. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  102. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  103. // e6y
  104. // Some ATI’s drivers have a bug whereby adding the depth renderbuffer
  105. // and then a texture causes the application to crash.
  106. // This should be kept in mind when doing any FBO related work and
  107. // tested for as it is possible it could be fixed in a future driver revision
  108. // thus rendering the problem non-existent.
  109. PRBOOM_TRY(EXEPTION_glFramebufferTexture2DEXT)
  110. {
  111. GLEXT_glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, glSceneImageTextureFBOTexID, 0);
  112. status = GLEXT_glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  113. }
  114. PRBOOM_EXCEPT(EXEPTION_glFramebufferTexture2DEXT)
  115. if (status == GL_FRAMEBUFFER_COMPLETE_EXT)
  116. {
  117. GLEXT_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  118. }
  119. else
  120. {
  121. lprintf(LO_ERROR, "gld_CreateScreenSizeFBO: Cannot create framebuffer object (error code: %d)\n", status);
  122. }
  123. return (status == GL_FRAMEBUFFER_COMPLETE_EXT);
  124. }
  125. void gld_FreeScreenSizeFBO(void)
  126. {
  127. if (!gl_ext_framebuffer_object)
  128. return;
  129. GLEXT_glDeleteFramebuffersEXT(1, &glSceneImageFBOTexID);
  130. glSceneImageFBOTexID = 0;
  131. GLEXT_glDeleteRenderbuffersEXT(1, &glDepthBufferFBOTexID);
  132. glDepthBufferFBOTexID = 0;
  133. glDeleteTextures(1, &glSceneImageTextureFBOTexID);
  134. glSceneImageTextureFBOTexID = 0;
  135. }
  136. void gld_InitMotionBlur(void)
  137. {
  138. if (gl_use_motionblur)
  139. {
  140. float f;
  141. sscanf(motion_blur.str_min_speed, "%f", &f);
  142. motion_blur.minspeed_pow2 = f * f;
  143. sscanf(motion_blur.str_min_angle, "%f", &f);
  144. motion_blur.minangle = (int)(f * 65536.0f / 360.0f);
  145. sscanf(motion_blur.str_att_a, "%f", &motion_blur.att_a);
  146. sscanf(motion_blur.str_att_b, "%f", &motion_blur.att_b);
  147. sscanf(motion_blur.str_att_c, "%f", &motion_blur.att_c);
  148. }
  149. }
  150. #endif