graphicsdevice.monkey2 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. Namespace mojo.graphics
  2. #rem monkeydoc Blend modes.
  3. Blend modes are used with the [[Canvas.BlendMode]] property.
  4. | BlendMode | Description
  5. |:----------|:-----------
  6. | Opaque | Blending disabled.
  7. | Alpha | Alpha blending.
  8. | Multiply | Multiply blending.
  9. | Additive | Additive blending.
  10. #end
  11. Enum BlendMode
  12. None=0
  13. Opaque
  14. Alpha
  15. Additive
  16. Multiply
  17. End
  18. #rem monkeydoc @hidden Color mask values.
  19. Color masks are used with the [[Canvas.ColorMask]] property.
  20. | ColorMask | Descripten
  21. |:----------|:----------
  22. | Red | Red color mask.
  23. | Green | Green color mask.
  24. | Blue | Blue color mask.
  25. | Alpha | Alpha color mask.
  26. #end
  27. Enum ColorMask
  28. None=0
  29. Red=1
  30. Green=2
  31. Blue=4
  32. Alpha=8
  33. All=15
  34. End
  35. #rem monkeydoc @hidden
  36. #end
  37. Class GraphicsDevice
  38. Method New()
  39. Init()
  40. End
  41. Method New( width:Int,height:Int )
  42. Init()
  43. _deviceSize=New Vec2i( width,height )
  44. _rtargetSize=_deviceSize
  45. End
  46. Method Resize( size:Vec2i )
  47. _deviceSize=size
  48. If Not _rtarget _rtargetSize=size
  49. End
  50. Property RenderTargetSize:Vec2i()
  51. Return _rtargetSize
  52. End
  53. '***** PUBLIC *****
  54. Property RenderTarget:Texture()
  55. Return _rtarget
  56. Setter( renderTarget:Texture )
  57. FlushTarget()
  58. _rtarget=renderTarget
  59. _rtargetSize=_rtarget ? _rtarget.Rect.Size Else _deviceSize
  60. _dirty|=Dirty.RenderTarget|Dirty.Viewport|Dirty.Scissor
  61. End
  62. Property Viewport:Recti()
  63. Return _viewport
  64. Setter( viewport:Recti )
  65. FlushTarget()
  66. _viewport=viewport
  67. _dirty|=Dirty.Viewport|Dirty.Scissor
  68. End
  69. Property Scissor:Recti()
  70. Return _scissor
  71. Setter( scissor:Recti )
  72. FlushTarget()
  73. _scissor=scissor
  74. _dirty|=Dirty.Scissor
  75. End
  76. Property ColorMask:ColorMask()
  77. Return _colorMask
  78. Setter( colorMask:ColorMask )
  79. _colorMask=colorMask
  80. _dirty|=Dirty.ColorMask
  81. End
  82. Property BlendMode:BlendMode()
  83. Return _blendMode
  84. Setter( blendMode:BlendMode )
  85. _blendMode=blendMode
  86. _dirty2|=Dirty.BlendMode
  87. End
  88. Property TextureFilter:TextureFilter()
  89. return _textureFilter
  90. Setter( filter:TextureFilter )
  91. _textureFilter=filter
  92. _dirty2|=Dirty.TextureFilter
  93. End
  94. Property VertexBuffer:VertexBuffer()
  95. Return _vertexBuffer
  96. Setter( vbuffer:VertexBuffer )
  97. _vertexBuffer=vbuffer
  98. _dirty2|=Dirty.VertexBuffer
  99. End
  100. Property IndexBuffer:IndexBuffer()
  101. Return _indexBuffer
  102. Setter( ibuffer:IndexBuffer )
  103. _indexBuffer=ibuffer
  104. _dirty2|=Dirty.IndexBuffer
  105. End
  106. Property RenderPass:Int()
  107. Return _rpass
  108. Setter( rpass:Int )
  109. _rpass=rpass
  110. _dirty2|=Dirty.Shader
  111. End
  112. Property Shader:Shader()
  113. Return _shader
  114. Setter( shader:Shader )
  115. _shader=shader
  116. _dirty2|=Dirty.Shader
  117. End
  118. Method SetUniformBlock( id:Int,ublock:UniformBlock )
  119. _ublocks[id]=ublock
  120. End
  121. Method GetUniformBlock:UniformBlock( id:Int )
  122. Return _ublocks[id]
  123. End
  124. Method CopyPixmap:Pixmap( rect:Recti )
  125. Validate()
  126. Local pixmap:=New Pixmap( rect.Width,rect.Height,PixelFormat.RGBA32 )
  127. glReadPixels( rect.X,rect.Y,rect.Width,rect.Height,GL_RGBA,GL_UNSIGNED_BYTE,pixmap.Data )
  128. If Not _rtarget pixmap.FlipY()
  129. Return pixmap
  130. End
  131. Method Clear( color:Color )
  132. Validate()
  133. glClearColor( color.r,color.g,color.b,color.a )
  134. If Not _scissorTest glEnable( GL_SCISSOR_TEST )
  135. glClear( GL_COLOR_BUFFER_BIT )
  136. If Not _scissorTest glDisable( GL_SCISSOR_TEST )
  137. _modified=true
  138. End
  139. Method Render( order:Int,count:Int,offset:Int=0 )
  140. Validate2()
  141. Local n:=order*count
  142. Select order
  143. Case 1 glDrawArrays( GL_POINTS,offset,n )
  144. Case 2 glDrawArrays( GL_LINES,offset,n )
  145. Case 3 glDrawArrays( GL_TRIANGLES,offset,n )
  146. Default
  147. For Local i:=0 Until count
  148. glDrawArrays( GL_TRIANGLE_FAN,offset+i*order,order )
  149. Next
  150. End
  151. _modified=true
  152. End
  153. Method RenderIndexed( order:Int,count:Int,offset:Int=0 )
  154. Validate2()
  155. Local n:=order*count
  156. Local p:=Cast<UShort Ptr>( offset*2 )
  157. Select order
  158. Case 1 glDrawElements( GL_POINTS,n,GL_UNSIGNED_SHORT,p )
  159. Case 2 glDrawElements( GL_LINES,n,GL_UNSIGNED_SHORT,p )
  160. Case 3 glDrawElements( GL_TRIANGLES,n,GL_UNSIGNED_SHORT,p )
  161. Default
  162. For Local i:=0 Until count
  163. glDrawElements( GL_TRIANGLE_FAN,order,GL_UNSIGNED_SHORT,p+i*order )
  164. Next
  165. End
  166. _modified=true
  167. End
  168. Private
  169. Enum Dirty
  170. '
  171. RenderTarget= $0001
  172. Viewport= $0002
  173. Scissor= $0004
  174. ColorMask= $0008
  175. '
  176. BlendMode= $0010
  177. VertexBuffer= $0020
  178. IndexBuffer= $0040
  179. Shader= $0080
  180. TextureFilter= $0100
  181. All= $01ff
  182. '
  183. End
  184. Field _dirty:Dirty
  185. Field _dirty2:Dirty
  186. Field _modified:Bool
  187. Field _rtarget:Texture
  188. Field _rtargetSize:Vec2i
  189. Field _deviceSize:Vec2i
  190. Field _viewport:Recti
  191. Field _scissor:Recti
  192. Field _scissorTest:Bool
  193. Field _colorMask:ColorMask
  194. Field _blendMode:BlendMode
  195. Field _textureFilter:TextureFilter
  196. Field _vertexBuffer:VertexBuffer
  197. Field _indexBuffer:IndexBuffer
  198. Field _ublocks:=New UniformBlock[4]
  199. Field _shader:Shader
  200. Field _rpass:Int
  201. Global _seq:Int
  202. Global _current:GraphicsDevice
  203. Global _defaultFbo:GLint
  204. Method Init()
  205. _colorMask=ColorMask.All
  206. End
  207. Function InitGLState()
  208. glDisable( GL_CULL_FACE )
  209. glDisable( GL_DEPTH_TEST )
  210. glGetIntegerv( GL_FRAMEBUFFER_BINDING,Varptr _defaultFbo )
  211. End
  212. Method FlushTarget()
  213. If Not _modified Return
  214. _modified=False
  215. If _rtarget
  216. Validate()
  217. _rtarget.Modified( _viewport & _scissor )
  218. Endif
  219. End
  220. Method Validate()
  221. If _seq<>glGraphicsSeq
  222. _seq=glGraphicsSeq
  223. _current=Null
  224. InitGLState()
  225. Endif
  226. If _current=Self
  227. If Not _dirty Return
  228. Else
  229. If _current _current.FlushTarget()
  230. _current=Self
  231. _dirty=Dirty.All
  232. Endif
  233. If _dirty & Dirty.RenderTarget
  234. If _rtarget
  235. glBindFramebuffer( GL_FRAMEBUFFER,_rtarget.GLFramebuffer )
  236. Else
  237. glBindFramebuffer( GL_FRAMEBUFFER,_defaultFbo )
  238. Endif
  239. Endif
  240. If _dirty & Dirty.Viewport
  241. If _rtarget
  242. glViewport( _viewport.X,_viewport.Y,_viewport.Width,_viewport.Height )
  243. Else
  244. glViewport( _viewport.X,_rtargetSize.y-_viewport.Bottom,_viewport.Width,_viewport.Height )
  245. Endif
  246. Endif
  247. If _dirty & Dirty.Scissor
  248. Local scissor:=_scissor & _viewport
  249. _scissorTest=scissor<>_viewport
  250. If _scissorTest glEnable( GL_SCISSOR_TEST ) Else glDisable( GL_SCISSOR_TEST )
  251. If _rtarget
  252. glScissor( scissor.X,scissor.Y,scissor.Width,scissor.Height )
  253. Else
  254. glScissor( scissor.X,_rtargetSize.y-scissor.Bottom,scissor.Width,scissor.Height )
  255. Endif
  256. Endif
  257. If _dirty & Dirty.ColorMask
  258. Local r:=Bool( _colorMask & ColorMask.Red )
  259. Local g:=Bool( _colorMask & ColorMask.Green )
  260. Local b:=Bool( _colorMask & ColorMask.Blue )
  261. Local a:=Bool( _colorMask & ColorMask.Alpha )
  262. glColorMask( r,g,b,a )
  263. Endif
  264. _dirty=Null
  265. End
  266. Method Validate2()
  267. Validate()
  268. If _dirty2 & Dirty.BlendMode
  269. Select _blendMode
  270. Case BlendMode.Opaque
  271. glDisable( GL_BLEND )
  272. Case BlendMode.Alpha
  273. glEnable( GL_BLEND )
  274. glBlendFunc( GL_ONE,GL_ONE_MINUS_SRC_ALPHA )
  275. Case BlendMode.Additive
  276. glEnable( GL_BLEND )
  277. glBlendFunc( GL_ONE,GL_ONE )
  278. Case BlendMode.Multiply
  279. glEnable( GL_BLEND )
  280. glBlendFunc( GL_DST_COLOR,GL_ONE_MINUS_SRC_ALPHA )
  281. Default
  282. glDisable( GL_BLEND )
  283. End
  284. Endif
  285. If _dirty2 & Dirty.VertexBuffer
  286. _vertexBuffer.Bind()
  287. Endif
  288. If _dirty2 & Dirty.IndexBuffer
  289. If _indexBuffer _indexBuffer.Bind()
  290. Endif
  291. If _dirty2 & Dirty.Shader
  292. _shader.Bind( _rpass )
  293. Endif
  294. _vertexBuffer.Validate()
  295. If _indexBuffer _indexBuffer.Validate()
  296. _shader.ValidateUniforms( _rpass,_ublocks,_textureFilter )
  297. _dirty2=Null
  298. End
  299. End