uniformblock.monkey2 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. Namespace mojo.graphics
  2. #rem monkeydoc The UniformBlock class.
  3. #end
  4. Class UniformBlock Extends Resource
  5. Method New( blockid:Int,linearColors:Bool=False )
  6. _blockid=blockid
  7. _linearColors=linearColors
  8. End
  9. Method New( uniforms:UniformBlock )
  10. _blockid=uniforms._blockid
  11. _linearColors=uniforms._linearColors
  12. _defaultTexture=uniforms._defaultTexture
  13. _ntextures=uniforms._ntextures
  14. For Local i:=0 Until _uniforms.Length
  15. _uniforms[i]=uniforms._uniforms[i]
  16. Next
  17. End
  18. Property LinearColors:Bool()
  19. Return _linearColors
  20. Setter( linearColors:Bool )
  21. _linearColors=linearColors
  22. End
  23. '***** Int *****
  24. '
  25. Method SetInt( uniform:String,value:Int )
  26. SetData( uniform,value,Type.Integer )
  27. End
  28. Method GetInt:Int( uniform:String )
  29. Return GetData<Int>( uniform,Type.Integer )
  30. End
  31. Method GetInt:Int( id:Int )
  32. Return GetDataPtr<Int>( id,Type.Integer )[0]
  33. End
  34. '***** Float *****
  35. '
  36. Method SetFloat( uniform:String,value:Float )
  37. SetData( uniform,value,Type.Scalar )
  38. End
  39. Method GetFloat:Float( uniform:String )
  40. Return GetData<Float>( uniform,Type.Scalar )
  41. End
  42. Method GetFloat:Float( id:Int )
  43. Return GetFloatPtr( id,Type.Scalar )[0]
  44. End
  45. '***** Vec2f *****
  46. '
  47. Method SetVec2f( uniform:String,value:Vec2f )
  48. SetData( uniform,value,Type.Vec2f )
  49. End
  50. method GetVec2f:Vec2f( uniform:String )
  51. Return GetData<Vec2f>( uniform,Type.Vec2f )
  52. End
  53. Method GetVec2fv:Float Ptr( id:Int )
  54. Return GetFloatPtr( id,Type.Vec2f )
  55. End
  56. '***** Vec3f *****
  57. '
  58. Method SetVec3f( uniform:String,value:Vec3f )
  59. SetData( uniform,value,Type.Vec3f )
  60. End
  61. Method GetVec3f:Vec3f( uniform:String )
  62. Return GetData<Vec3f>( uniform,Type.Vec3f )
  63. End
  64. Method GetVec3fv:Float Ptr( id:Int )
  65. Return GetFloatPtr( id,Type.Vec3f )
  66. End
  67. '***** Vec4f *****
  68. '
  69. Method SetVec4f( uniform:String,value:Vec4f )
  70. SetData( uniform,value,Type.Vec4f )
  71. End
  72. Method GetVec4f:Vec4f( uniform:String )
  73. Return GetData<Vec4f>( uniform,Type.Vec4f )
  74. End
  75. Method GetVec4fv:Float Ptr( id:Int )
  76. Return GetFloatPtr( id,Type.Vec4f )
  77. End
  78. '***** Color (really just Vec4f) *****
  79. '
  80. Method SetColor( uniform:String,value:Color )
  81. If _linearColors
  82. value.r=Pow(value.r,2.2 )
  83. value.g=Pow(value.g,2.2 )
  84. value.b=Pow(value.b,2.2 )
  85. Endif
  86. SetData( uniform,value,Type.Vec4f )
  87. End
  88. Method GetColor:Color( uniform:String )
  89. Local value:=GetData<Color>( uniform,Type.Vec4f )
  90. If _linearColors
  91. value.r=Pow(value.r,1.0/2.2 )
  92. value.g=Pow(value.g,1.0/2.2 )
  93. value.b=Pow(value.b,1.0/2.2 )
  94. Endif
  95. Return value
  96. End
  97. '***** Mat3f *****
  98. '
  99. Method SetMat3f( uniform:String,value:Mat3f )
  100. SetData( uniform,value,Type.Mat3f )
  101. End
  102. Method GetMat3f:Mat3f( uniform:String )
  103. Return GetData<Mat3f>( uniform,Type.Mat3f )
  104. End
  105. Method GetMat3fv:Float Ptr( id:Int )
  106. Return GetFloatPtr( id,Type.Mat3f )
  107. End
  108. '***** AffineMat3f *****
  109. '
  110. Method SetAffineMat3f( uniform:String,value:AffineMat3f )
  111. Local m:=New Mat3f( value.i.x,value.i.y,0, value.j.x,value.j.y,0, value.t.x,value.t.y,1 )
  112. SetData( uniform,m,Type.Mat3f )
  113. End
  114. Method GetAffineMat3f:AffineMat3f( uniform:String )
  115. Local m:=GetData<Mat3f>( uniform,Type.Mat3f )
  116. Return New AffineMat3f( m.i.x,m.i.y,m.j.x,m.j.y,m.k.x,m.k.y )
  117. End
  118. '***** Mat4f *****
  119. '
  120. Method SetMat4f( uniform:String,value:Mat4f )
  121. SetData( uniform,value,Type.Mat4f )
  122. End
  123. Method SetMat4f( uniform:String,value:AffineMat4f )
  124. SetData( uniform,New Mat4f( value ),Type.Mat4f )
  125. End
  126. Method GetMat4f:Mat4f( uniform:String )
  127. Return GetData<Mat4f>( uniform,Type.Mat4f )
  128. End
  129. Method GetMat4fv:Float Ptr( id:Int )
  130. If _uniforms[id].type=Type.Mat4fArray Return Varptr _uniforms[id].arrayData[0].i.x
  131. Return GetFloatPtr( id,Type.Mat4f )
  132. End
  133. '***** Mat4f array *****
  134. '
  135. Method SetMat4fArray( name:String,value:Mat4f[] )
  136. Local id:=GetUniformId( name )
  137. _uniforms[id].arrayData=value
  138. _uniforms[id].type=Type.Mat4fArray
  139. _seq=_gseq
  140. _gseq+=1
  141. End
  142. Method GetMat4fArray:Mat4f[]( name:String )
  143. Local id:=GetUniformId( name,Type.Mat4fArray )
  144. Return _uniforms[id].arrayData
  145. End
  146. Method GetMat4fArray:Mat4f[]( id:Int )
  147. #If __DEBUG__
  148. Local type:=Type.Mat4fArray
  149. Assert( id,"Uniform '"+GetUniformName( id )+"' not found" )
  150. Assert( _uniforms[id].type,"Uniform '"+GetUniformName( id )+"' not found in UniformBlock" )
  151. Assert( _uniforms[id].type=type,"Uniform '"+GetUniformName( id )+"' has incorrect type '"+_typenames[_uniforms[id].type]="', expecting type '"+_typenames[type]+"'" )
  152. #endif
  153. Return _uniforms[id].arrayData
  154. End
  155. '***** Texture *****
  156. '
  157. Property NumTextures:Int()
  158. Return _ntextures
  159. End
  160. Property DefaultTexture:Texture()
  161. Return _defaultTexture
  162. Setter( texture:Texture )
  163. _defaultTexture=texture
  164. End
  165. Method SetTexture( name:String,value:Texture )
  166. Local id:=GetUniformId( name )
  167. If (value<>Null)<>(_uniforms[id].texture<>Null)
  168. If value _ntextures+=1 Else _ntextures-=1
  169. End
  170. _uniforms[id].texture=value
  171. _uniforms[id].type=Type.Texture
  172. _seq=_gseq
  173. _gseq+=1
  174. End
  175. Method GetTexture:Texture( uniform:String )
  176. Local id:=GetUniformId( uniform,Type.Texture )
  177. Return _uniforms[id].texture
  178. End
  179. Method GetTexture:Texture( id:Int )
  180. #If __DEBUG__
  181. Local type:=Type.Texture
  182. Assert( id,"Uniform '"+GetUniformName( id )+"' not found" )
  183. Assert( _uniforms[id].type,"Uniform '"+GetUniformName( id )+"' not found in UniformBlock" )
  184. Assert( _uniforms[id].type=type,"Uniform '"+GetUniformName( id )+"' has incorrect type '"+_typenames[_uniforms[id].type]="', expecting type '"+_typenames[type]+"'" )
  185. #endif
  186. Return _uniforms[id].texture ?Else _defaultTexture
  187. End
  188. #rem monkeydoc @hidden
  189. #end
  190. Property Seq:Int()
  191. Return _seq
  192. End
  193. Protected
  194. #rem monkeydoc @hidden
  195. #end
  196. Method OnDiscard() Override
  197. _uniforms=Null
  198. End
  199. Internal
  200. Property BlockId:Int()
  201. Return _blockid
  202. End
  203. Function GetUniformId:Int( name:String,blockid:Int )
  204. Local ids:=_ids[blockid]
  205. If Not ids
  206. ids=New StringMap<Int>
  207. _ids[blockid]=ids
  208. _names[blockid]=New IntMap<String>
  209. Endif
  210. Local id:=ids[name]
  211. If Not id
  212. id=ids.Count()+1
  213. ids[name]=id
  214. _names[blockid][id]=name
  215. Endif
  216. Return id
  217. End
  218. Method GetUniformId:Int( name:String )
  219. Return GetUniformId( name,_blockid )
  220. End
  221. Method GetUniformId:Int( name:String,type:Type )
  222. Local id:=_ids[_blockid][name]
  223. #If __DEBUG__
  224. Assert( id,"Uniform '"+name+"' not found" )
  225. Assert( _uniforms[id].type,"Uniform '"+name+"' not found in UniformBlock" )
  226. Assert( _uniforms[id].type=type,"Uniform '"+name+"' has incorrect type '"+_typenames[_uniforms[id].type]="', expecting type '"+_typenames[type]+"'" )
  227. #endif
  228. Return id
  229. End
  230. Method GetUniformName:String( id:Int )
  231. Local names:=_names[_blockid]
  232. Local name:=names ? names[id] Else ""
  233. Return name ?Else "<unknown>"
  234. End
  235. Private
  236. Global _gseq:Int=1
  237. Global _ids:=New StringMap<Int>[8]
  238. Global _names:=New IntMap<String>[8]
  239. Global _typenames:=New String[]( "Void","Float","Vec2f","Vec3f","Vec4f","Mat3f","Mat4f","Mat4f[]","Int" )
  240. Enum Type
  241. None=0
  242. Scalar=1
  243. Vec2f=2
  244. Vec3f=3
  245. Vec4f=4
  246. Mat3f=5
  247. Mat4f=6
  248. Texture=7
  249. Mat4fArray=8
  250. Integer=9
  251. End
  252. Struct Uniform
  253. Field type:Type
  254. Field texture:Texture
  255. Field arrayData:Mat4f[]
  256. Field floatData:Mat4f
  257. End
  258. Field _blockid:Int
  259. Field _linearColors:bool
  260. Field _defaultTexture:Texture
  261. Field _ntextures:Int
  262. Field _uniforms:=New Uniform[64]
  263. Field _seq:Int
  264. Method SetData<T>( name:String,data:T,type:Type )
  265. Local id:=GetUniformId( name )
  266. Cast<T Ptr>( Varptr _uniforms[id].floatData )[0]=data
  267. _uniforms[id].type=type
  268. _seq=_gseq
  269. _gseq+=1
  270. End
  271. Method GetData<T>:T( name:String,type:Type )
  272. Local id:=GetUniformId( name,type )
  273. ' DebugAssert( _uniforms[id].type=type,"Invalid uniform type" )
  274. Return Cast<T Ptr>( Varptr _uniforms[id].floatData )[0]
  275. End
  276. Method GetDataPtr<T>:T Ptr( id:Int,type:Type )
  277. #If __DEBUG__
  278. Assert( id,"Uniform '"+GetUniformName( id )+"' not found" )
  279. Assert( _uniforms[id].type,"Uniform '"+GetUniformName( id )+"' not found in UniformBlock" )
  280. Assert( _uniforms[id].type=type,"Uniform '"+GetUniformName( id )+"' has incorrect type '"+_typenames[_uniforms[id].type]="', expecting type '"+_typenames[type]+"'" )
  281. #endif
  282. Return Cast<T Ptr>( Varptr _uniforms[id].floatData )
  283. End
  284. Method GetFloatPtr:Float Ptr( id:Int,type:Type )
  285. #If __DEBUG__
  286. Assert( id,"Uniform '"+GetUniformName( id )+"' not found" )
  287. Assert( _uniforms[id].type,"Uniform '"+GetUniformName( id )+"' not found in UniformBlock" )
  288. Assert( _uniforms[id].type=type,"Uniform '"+GetUniformName( id )+"' has incorrect type '"+_typenames[_uniforms[id].type]="', expecting type '"+_typenames[type]+"'" )
  289. #endif
  290. Return Cast<Float Ptr>( Varptr _uniforms[id].floatData )
  291. End
  292. End