Guest User

Untitled

a guest
Apr 25th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. bool FBO::createFBO(const FBO::Config &c, FBO::Data &data) const {
  2. FBO::Config config(c);
  3.  
  4. bool multisample = config._depthSamples > 0;
  5. bool csaa = config._coverageSamples > config._depthSamples;
  6. bool ret = true;
  7. int query;
  8.  
  9. glGenFramebuffersEXT(1, &data.fb);
  10. ret &= checkFrameBufferStatus(true);
  11. glGenTextures(1, &data.tex);
  12.  
  13. // init texture
  14. glBindTexture(GL_TEXTURE_2D, data.tex);
  15. glTexImage2D(GL_TEXTURE_2D, 0,
  16. config._colorFormat, _texWidth, _texHeight, 0,
  17. GL_RGB, GL_FLOAT, NULL);
  18.  
  19. // glGenerateMipmapEXT( GL_TEXTURE_2D);
  20.  
  21. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  22. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  23. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  24. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  25.  
  26.  
  27. //
  28. // Handle multisample FBO's first
  29. //
  30. if (multisample) {
  31. glGenRenderbuffersEXT( 1, &data.depthRB);
  32. glGenRenderbuffersEXT( 1, &data.colorRB);
  33. glGenFramebuffersEXT( 1, &data.resolveFB);
  34. data.depthTex = 0; //no resolve of depth buffer for now
  35.  
  36. //multisample, so we need to resolve from the FBO, bind the texture to the resolve FBO
  37. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, data.resolveFB);
  38.  
  39. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, data.tex, 0);
  40.  
  41. ret &= checkFrameBufferStatus(true);
  42.  
  43. //now handle the rendering FBO
  44. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, data.fb);
  45.  
  46. // initialize color renderbuffer
  47. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, data.colorRB);
  48.  
  49. if (csaa) {
  50. glRenderbufferStorageMultisampleCoverageNV( GL_RENDERBUFFER_EXT, config._coverageSamples, config._depthSamples, config._colorFormat,
  51. _texWidth, _texHeight);
  52.  
  53. glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_COVERAGE_SAMPLES_NV, &query);
  54.  
  55. if ( query < config._coverageSamples) {
  56. ret = false;
  57. }
  58. else if ( query > config._coverageSamples) {
  59. // report back the actual number
  60. config._coverageSamples = query;
  61. }
  62.  
  63. glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_COLOR_SAMPLES_NV, &query);
  64.  
  65. if ( query < config._depthSamples) {
  66. ret = false;
  67. }
  68. else if ( query > config._depthSamples) {
  69. // report back the actual number
  70. config._depthSamples = query;
  71. }
  72. }
  73. else {
  74.  
  75. // create a regular MSAA color buffer
  76. glRenderbufferStorageMultisampleEXT( GL_RENDERBUFFER_EXT, config._depthSamples, config._colorFormat, _texWidth, _texHeight);
  77.  
  78. // check the number of samples
  79. glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &query);
  80.  
  81. if ( query < config._depthSamples) {
  82. ret = false;
  83. }
  84. else if ( query > config._depthSamples) {
  85. config._depthSamples = query;
  86. }
  87.  
  88. }
  89.  
  90. // attach the multisampled color buffer
  91. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, data.colorRB);
  92.  
  93. ret &= checkFrameBufferStatus(true);
  94.  
  95. // bind the multisampled depth buffer
  96. glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, data.depthRB);
  97.  
  98. // create the multisampled depth buffer (with or without coverage sampling)
  99. if (csaa) {
  100.  
  101. // create a coverage sampled MSAA depth buffer
  102. glRenderbufferStorageMultisampleCoverageNV( GL_RENDERBUFFER_EXT, config._coverageSamples, config._depthSamples, config._depthFormat,
  103. _texWidth, _texHeight);
  104.  
  105. // check the number of coverage samples
  106. glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_COVERAGE_SAMPLES_NV, &query);
  107.  
  108. if ( query < config._coverageSamples) {
  109. ret = false;
  110. }
  111. else if ( query > config._coverageSamples) {
  112. // set the coverage samples value to return the actual value
  113. config._coverageSamples = query;
  114. }
  115.  
  116. // cehck the number of stored color samples (same as depth samples)
  117. glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_COLOR_SAMPLES_NV, &query);
  118.  
  119. if ( query < config._depthSamples) {
  120. ret = false;
  121. }
  122. else if ( query > config._depthSamples) {
  123. // set the depth samples value to return the actual value
  124. config._depthSamples = query;
  125. }
  126. }
  127. else {
  128.  
  129. // create a regular (not coverage sampled) MSAA depth buffer
  130. glRenderbufferStorageMultisampleEXT( GL_RENDERBUFFER_EXT, config._depthSamples, config._depthFormat, _texWidth, _texHeight);
  131.  
  132. // check the number of depth samples
  133. glGetRenderbufferParameterivEXT( GL_RENDERBUFFER_EXT, GL_RENDERBUFFER_SAMPLES_EXT, &query);
  134.  
  135. if ( query < config._depthSamples) {
  136. ret = false;
  137. }
  138. else if ( query < config._depthSamples) {
  139. config._depthSamples = query;
  140. }
  141. }
  142.  
  143. // attach the depth buffer
  144. glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, data.depthRB);
  145.  
  146. ret &= checkFrameBufferStatus(true);
  147.  
  148. } // if (multisample)
  149. else {
  150. // glGenTextures( 1, &data.depthTex);
  151. // data.depthRB = 0;
  152. data.colorRB = 0;
  153. data.resolveFB = 0;
  154.  
  155. //non-multisample, so bind things directly to the FBO
  156. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, data.fb);
  157.  
  158. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, data.tex, 0);
  159.  
  160. /* glBindTexture( GL_TEXTURE_2D, data.depthTex);
  161. glTexImage2D( GL_TEXTURE_2D, 0, config._depthFormat, _texWidth, _texHeight, 0,
  162. GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  163.  
  164. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  165. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  166. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  167. glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  168. glTexParameterf( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
  169.  
  170. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, data.depthTex, 0);
  171. */
  172. ret &= checkFrameBufferStatus(true);
  173. }
  174.  
  175. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, data.fb);
  176. /*glGetIntegerv( GL_RED_BITS, &query);
  177. if ( query != config.redbits) {
  178. ret = false;
  179. }*/
  180.  
  181. /*glGetIntegerv( GL_DEPTH_BITS, &query);
  182. if ( query != config.depthBits) {
  183. ret = false;
  184. }*/
  185.  
  186. if (multisample) {
  187. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, data.resolveFB);
  188. /* glGetIntegerv( GL_RED_BITS, &query);
  189. if ( query != config.redbits) {
  190. ret = false;
  191. }*/
  192.  
  193. }
  194.  
  195. glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
  196.  
  197. return ret;
  198. }
Add Comment
Please, Sign In to add comment