rendertarget.monkey2 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. Namespace mojo.graphics
  2. Class RenderTarget Extends Resource
  3. Method New( colorTextures:Texture[],depthTexture:Texture )
  4. _colorTextures=colorTextures.Slice( 0 )
  5. _depthTexture=depthTexture
  6. _size=_colorTextures ? _colorTextures[0].Size Else _depthTexture.Size
  7. _glDrawBufs=New GLenum[_colorTextures.Length]
  8. For Local i:=0 Until _colorTextures.Length
  9. _glDrawBufs[i]=_colorTextures[i] ? GL_COLOR_ATTACHMENT0+i Else GL_NONE
  10. Next
  11. End
  12. Property NumColorTextures:Int()
  13. Return _colorTextures.Length
  14. End
  15. Property ColorTextures:Texture[]()
  16. Return _colorTextures.Slice( 0 )
  17. End
  18. Property DepthTexture:Texture()
  19. Return _depthTexture
  20. End
  21. Property Size:Vec2i()
  22. Return _size
  23. End
  24. Method GetColorTexture:Texture( index:Int )
  25. Return index>=0 And index<_colorTextures.Length ? _colorTextures[index] Else Null
  26. End
  27. Protected
  28. #rem monkeydoc Discards the rendertarget
  29. #end
  30. Method OnDiscard() Override
  31. If _glSeq=glGraphicsSeq glDeleteFramebuffers( 1,Varptr _glFramebuffer )
  32. _colorTextures=Null
  33. _depthTexture=Null
  34. _glSeq=-1
  35. End
  36. #rem monkeydoc @hidden
  37. #end
  38. Method OnFinalize() Override
  39. If _glSeq=glGraphicsSeq glDeleteFramebuffers( 1,Varptr _glFramebuffer )
  40. End
  41. Internal
  42. Method Bind()
  43. glBindFramebuffer( GL_FRAMEBUFFER,ValidateGLFramebuffer() )
  44. If BBGL_draw_buffers
  45. glDrawBuffers( _glDrawBufs.Length,_glDrawBufs.Data )
  46. Endif
  47. If Not BBGL_ES
  48. glReadBuffer( _glDrawBufs ? _glDrawBufs[0] Else GL_NONE )
  49. Endif
  50. CheckStatus()
  51. End
  52. Private
  53. Field _colorTextures:Texture[]
  54. Field _depthTexture:Texture
  55. Field _size:Vec2i
  56. Field _glDrawBufs:GLenum[]
  57. Field _glFramebuffer:GLuint
  58. Field _glSeq:Int
  59. Field _checked:Bool
  60. Method ValidateGLFramebuffer:GLuint()
  61. If _glSeq=glGraphicsSeq Return _glFramebuffer
  62. glCheck()
  63. glGenFramebuffers( 1,Varptr _glFramebuffer )
  64. glPushFramebuffer( GL_FRAMEBUFFER,_glFramebuffer )
  65. For Local i:=0 Until _colorTextures.Length
  66. Local texture:=_colorTextures[i]
  67. If texture glFramebufferTexture2D( GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0+i,texture.GLTarget,texture.ValidateGLTexture(),0 )
  68. Next
  69. If _depthTexture
  70. glFramebufferTexture2D( GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,_depthTexture.GLTarget,_depthTexture.ValidateGLTexture(),0 )
  71. Endif
  72. _glSeq=glGraphicsSeq
  73. glPopFramebuffer()
  74. glCheck()
  75. Return _glFramebuffer
  76. End
  77. Method CheckStatus()
  78. If _checked Return
  79. _checked=true
  80. Local status:=glCheckFramebufferStatus( GL_FRAMEBUFFER )
  81. If status=GL_FRAMEBUFFER_COMPLETE Return
  82. Local err:=""
  83. Select status
  84. Case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT
  85. err="GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"
  86. Case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT
  87. err="GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"
  88. ' Case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS
  89. ' err="GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS"
  90. Case GL_FRAMEBUFFER_UNSUPPORTED
  91. err="GL_FRAMEBUFFER_UNSUPPORTED"
  92. Default
  93. err="GL_FRAMEBUFFER_?????"
  94. End
  95. Print "ColorTexture0 format="+(_colorTextures ? Cast<Int>( _colorTextures[0].Format ) Else 0)
  96. Print "DepthTexture format="+(_depthTexture ? Cast<Int>( _depthTexture.Format ) Else 0)
  97. RuntimeError( "Framebuffer incomplete: status="+err )
  98. End
  99. End