indexbuffer.monkey2 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. Namespace mojo.graphics
  2. Enum IndexFormat
  3. UINT8=1
  4. UINT16=2
  5. UINT32=4
  6. End
  7. Class IndexBuffer Extends Resource
  8. Method New( format:IndexFormat,length:Int=0 )
  9. _format=format
  10. _length=length
  11. _pitch=Int( _format )
  12. _managed=New UByte[_length*_pitch]
  13. _dirtyMin=_length
  14. _dirtyMax=0
  15. End
  16. Method New( indices:UInt[] )
  17. Self.New( IndexFormat.UINT32,indices.Length )
  18. SetIndices( indices.Data,0,indices.Length )
  19. End
  20. Property Format:IndexFormat()
  21. Return _format
  22. End
  23. Property Length:Int()
  24. Return _length
  25. End
  26. Property Pitch:Int()
  27. Return _pitch
  28. End
  29. #rem monkeydoc Resizes the index buffer.
  30. #end
  31. Method Resize( length:Int )
  32. If length=_length Return
  33. Local managed:=New UByte[length*_pitch]
  34. Local n:=Min( length,_length )
  35. If n libc.memcpy( managed.Data,_managed.Data,n*_pitch )
  36. _managed=managed
  37. _length=length
  38. If _glSeq=glGraphicsSeq glDeleteBuffers( 1,Varptr _glBuffer )
  39. _glSeq=0
  40. End
  41. #rem monkeydoc Sets a range of indices.
  42. #end
  43. Method SetIndices( indices:Void Ptr,first:Int,count:Int )
  44. DebugAssert( Not _locked,"IndexBuffer is locked" )
  45. DebugAssert( first>=0 And count>=0 And first<=_length And first+count<=_length,"Invalid index range" )
  46. libc.memcpy( _managed.Data+first*_pitch,indices,count*_pitch )
  47. Invalidate( first,count )
  48. End
  49. #rem monkeydoc Locks indices.
  50. Make sure to invalidate any indices you modify by using [[Invalidate]].
  51. #end
  52. Method Lock:UByte ptr()
  53. DebugAssert( Not _locked,"IndexBuffer is already locked" )
  54. _locked=_managed.Data
  55. Return _locked
  56. End
  57. #rem onkeydoc Invalidates indices.
  58. You should use this method to invalidate any indices you have modified by writing to a locked index buffer.
  59. #End
  60. Method Invalidate( first:Int,count:Int )
  61. ' DebugAssert( _locked,"Index buffer is not locked" )
  62. DebugAssert( first>=0 And count>=0 And first<=_length And first+count<=_length,"Invalid index range" )
  63. _dirtyMin=Min( _dirtyMin,first )
  64. _dirtyMax=Max( _dirtyMax,first+count )
  65. End
  66. Method Invalidate()
  67. Invalidate( 0,_length )
  68. End
  69. #rem monkeydoc Unlocks indices.
  70. #end
  71. Method Unlock:Void()
  72. DebugAssert( _locked,"Index buffer is not locked" )
  73. _locked=Null
  74. End
  75. Protected
  76. Method OnDiscard() Override
  77. If _glSeq=glGraphicsSeq glDeleteBuffers( 1,Varptr _glBuffer )
  78. _glSeq=-1
  79. End
  80. Method OnFinalize() Override
  81. If _glSeq=glGraphicsSeq glDeleteBuffers( 1,Varptr _glBuffer )
  82. End
  83. Internal
  84. Method Bind()
  85. DebugAssert( Not _locked,"IndexBuffer.Bind() failed, IndexBuffer is locked" )
  86. If _glSeq<>glGraphicsSeq
  87. glGenBuffers( 1,Varptr _glBuffer )
  88. glBindBuffer( GL_ELEMENT_ARRAY_BUFFER,_glBuffer )
  89. glBufferData( GL_ELEMENT_ARRAY_BUFFER,_length*_pitch,_managed.Data,GL_DYNAMIC_DRAW )
  90. _dirtyMin=_length
  91. _dirtyMax=0
  92. _glSeq=glGraphicsSeq
  93. Else
  94. glBindBuffer( GL_ELEMENT_ARRAY_BUFFER,_glBuffer )
  95. Endif
  96. End
  97. Method Validate()
  98. If _dirtyMax>_dirtyMin
  99. glBufferSubData( GL_ELEMENT_ARRAY_BUFFER,_dirtyMin*_pitch,(_dirtyMax-_dirtyMin)*_pitch,_managed.Data )
  100. _dirtyMin=_length
  101. _dirtyMax=0
  102. Endif
  103. End
  104. Private
  105. Field _format:IndexFormat
  106. Field _length:Int
  107. Field _pitch:Int
  108. Field _managed:UByte[]
  109. Field _dirtyMin:Int
  110. Field _dirtyMax:Int
  111. Field _locked:UByte Ptr
  112. Field _glSeq:Int
  113. Field _glBuffer:GLuint
  114. End