Texture 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_TEXTURE
  14. #define OSG_TEXTURE 1
  15. #include <osg/GL>
  16. #include <osg/Image>
  17. #include <osg/StateAttribute>
  18. #include <osg/GraphicsContext>
  19. #include <osg/ref_ptr>
  20. #include <osg/Vec4>
  21. #include <osg/Vec4d>
  22. #include <osg/Vec4i>
  23. #include <osg/buffered_value>
  24. #include <osg/GLExtensions>
  25. #include <list>
  26. #include <map>
  27. // If not defined by gl.h use the definition found in:
  28. // http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_filter_anisotropic.txt
  29. #ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
  30. #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
  31. #endif
  32. // If not defined, use the definition found in:
  33. // http://www.opengl.org/registry/specs/ARB/texture_swizzle.txt
  34. #ifndef GL_TEXTURE_SWIZZLE_RGBA
  35. #define GL_TEXTURE_SWIZZLE_RGBA 0x8E46
  36. #endif
  37. #ifndef GL_ARB_texture_compression
  38. #define GL_COMPRESSED_ALPHA_ARB 0x84E9
  39. #define GL_COMPRESSED_LUMINANCE_ARB 0x84EA
  40. #define GL_COMPRESSED_LUMINANCE_ALPHA_ARB 0x84EB
  41. #define GL_COMPRESSED_INTENSITY_ARB 0x84EC
  42. #define GL_COMPRESSED_RGB_ARB 0x84ED
  43. #define GL_COMPRESSED_RGBA_ARB 0x84EE
  44. #define GL_TEXTURE_COMPRESSION_HINT_ARB 0x84EF
  45. #define GL_TEXTURE_COMPRESSED_ARB 0x86A1
  46. #define GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB 0x86A2
  47. #define GL_COMPRESSED_TEXTURE_FORMATS_ARB 0x86A3
  48. #endif
  49. #ifndef GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB
  50. #define GL_TEXTURE_COMPRESSED_IMAGE_SIZE_ARB 0x86A0
  51. #endif
  52. #ifndef GL_EXT_texture_compression_s3tc
  53. #define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
  54. #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  55. #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  56. #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  57. #endif
  58. #ifndef GL_EXT_texture_compression_rgtc
  59. #define GL_COMPRESSED_RED_RGTC1_EXT 0x8DBB
  60. #define GL_COMPRESSED_SIGNED_RED_RGTC1_EXT 0x8DBC
  61. #define GL_COMPRESSED_RED_GREEN_RGTC2_EXT 0x8DBD
  62. #define GL_COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT 0x8DBE
  63. #endif
  64. #ifndef GL_IMG_texture_compression_pvrtc
  65. #define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  66. #define GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  67. #define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  68. #define GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  69. #endif
  70. #ifndef GL_OES_compressed_ETC1_RGB8_texture
  71. #define GL_ETC1_RGB8_OES 0x8D64
  72. #endif
  73. #ifndef GL_ARB_INTERNAL_TEXTURE_FORMAT
  74. #define GL_RGBA32F_ARB 0x8814
  75. #define GL_RGB32F_ARB 0x8815
  76. #define GL_ALPHA32F_ARB 0x8816
  77. #define GL_INTENSITY32F_ARB 0x8817
  78. #define GL_LUMINANCE32F_ARB 0x8818
  79. #define GL_LUMINANCE_ALPHA32F_ARB 0x8819
  80. #define GL_RGBA16F_ARB 0x881A
  81. #define GL_RGB16F_ARB 0x881B
  82. #define GL_ALPHA16F_ARB 0x881C
  83. #define GL_INTENSITY16F_ARB 0x881D
  84. #define GL_LUMINANCE16F_ARB 0x881E
  85. #define GL_LUMINANCE_ALPHA16F_ARB 0x881F
  86. #endif
  87. #ifndef GL_HALF_FLOAT
  88. #define GL_HALF_FLOAT 0x140B
  89. #endif
  90. #ifndef GL_NV_texture_shader
  91. #define GL_HILO_NV 0x86F4
  92. #define GL_DSDT_NV 0x86F5
  93. #define GL_DSDT_MAG_NV 0x86F6
  94. #define GL_DSDT_MAG_VIB_NV 0x86F7
  95. #define GL_HILO16_NV 0x86F8
  96. #define GL_SIGNED_HILO_NV 0x86F9
  97. #define GL_SIGNED_HILO16_NV 0x86FA
  98. #define GL_SIGNED_RGBA_NV 0x86FB
  99. #define GL_SIGNED_RGBA8_NV 0x86FC
  100. #define GL_SIGNED_RGB_NV 0x86FE
  101. #define GL_SIGNED_RGB8_NV 0x86FF
  102. #define GL_SIGNED_LUMINANCE_NV 0x8701
  103. #define GL_SIGNED_LUMINANCE8_NV 0x8702
  104. #define GL_SIGNED_LUMINANCE_ALPHA_NV 0x8703
  105. #define GL_SIGNED_LUMINANCE8_ALPHA8_NV 0x8704
  106. #define GL_SIGNED_ALPHA_NV 0x8705
  107. #define GL_SIGNED_ALPHA8_NV 0x8706
  108. #define GL_SIGNED_INTENSITY_NV 0x8707
  109. #define GL_SIGNED_INTENSITY8_NV 0x8708
  110. #define GL_DSDT8_NV 0x8709
  111. #define GL_DSDT8_MAG8_NV 0x870A
  112. #define GL_DSDT8_MAG8_INTENSITY8_NV 0x870B
  113. #define GL_SIGNED_RGB_UNSIGNED_ALPHA_NV 0x870C
  114. #define GL_SIGNED_RGB8_UNSIGNED_ALPHA8_NV 0x870D
  115. #endif
  116. #ifndef GL_NV_float_buffer
  117. #define GL_FLOAT_R_NV 0x8880
  118. #define GL_FLOAT_RG_NV 0x8881
  119. #define GL_FLOAT_RGB_NV 0x8882
  120. #define GL_FLOAT_RGBA_NV 0x8883
  121. #define GL_FLOAT_R16_NV 0x8884
  122. #define GL_FLOAT_R32_NV 0x8885
  123. #define GL_FLOAT_RG16_NV 0x8886
  124. #define GL_FLOAT_RG32_NV 0x8887
  125. #define GL_FLOAT_RGB16_NV 0x8888
  126. #define GL_FLOAT_RGB32_NV 0x8889
  127. #define GL_FLOAT_RGBA16_NV 0x888A
  128. #define GL_FLOAT_RGBA32_NV 0x888B
  129. #endif
  130. #ifndef GL_ATI_texture_float
  131. #define GL_RGBA_FLOAT32_ATI 0x8814
  132. #define GL_RGB_FLOAT32_ATI 0x8815
  133. #define GL_ALPHA_FLOAT32_ATI 0x8816
  134. #define GL_INTENSITY_FLOAT32_ATI 0x8817
  135. #define GL_LUMINANCE_FLOAT32_ATI 0x8818
  136. #define GL_LUMINANCE_ALPHA_FLOAT32_ATI 0x8819
  137. #define GL_RGBA_FLOAT16_ATI 0x881A
  138. #define GL_RGB_FLOAT16_ATI 0x881B
  139. #define GL_ALPHA_FLOAT16_ATI 0x881C
  140. #define GL_INTENSITY_FLOAT16_ATI 0x881D
  141. #define GL_LUMINANCE_FLOAT16_ATI 0x881E
  142. #define GL_LUMINANCE_ALPHA_FLOAT16_ATI 0x881F
  143. #endif
  144. #ifndef GL_MIRRORED_REPEAT_IBM
  145. #define GL_MIRRORED_REPEAT_IBM 0x8370
  146. #endif
  147. #ifndef GL_CLAMP_TO_EDGE
  148. #define GL_CLAMP_TO_EDGE 0x812F
  149. #endif
  150. #ifndef GL_CLAMP
  151. #define GL_CLAMP 0x2900
  152. #endif
  153. #ifndef GL_CLAMP_TO_BORDER_ARB
  154. #define GL_CLAMP_TO_BORDER_ARB 0x812D
  155. #endif
  156. #ifndef GL_INTENSITY
  157. // OpenGL ES1 and ES2 doesn't provide GL_INTENSITY
  158. #define GL_INTENSITY 0x8049
  159. #endif
  160. #ifndef GL_GENERATE_MIPMAP_SGIS
  161. #define GL_GENERATE_MIPMAP_SGIS 0x8191
  162. #define GL_GENERATE_MIPMAP_HINT_SGIS 0x8192
  163. #endif
  164. #ifndef GL_TEXTURE_3D
  165. #define GL_TEXTURE_3D 0x806F
  166. #endif
  167. #ifndef GL_TEXTURE_CUBE_MAP
  168. #define GL_TEXTURE_CUBE_MAP 0x8513
  169. #define GL_TEXTURE_BINDING_CUBE_MAP 0x8514
  170. #define GL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
  171. #define GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
  172. #define GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
  173. #define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
  174. #define GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
  175. #define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
  176. #define GL_PROXY_TEXTURE_CUBE_MAP 0x851B
  177. #define GL_MAX_CUBE_MAP_TEXTURE_SIZE 0x851C
  178. #endif
  179. #ifndef GL_TEXTURE_BINDING_3D
  180. #define GL_TEXTURE_BINDING_3D 0x806A
  181. #endif
  182. #ifndef GL_DEPTH_TEXTURE_MODE_ARB
  183. #define GL_DEPTH_TEXTURE_MODE_ARB 0x884B
  184. #endif
  185. #ifndef GL_TEXTURE_COMPARE_MODE_ARB
  186. #define GL_TEXTURE_COMPARE_MODE_ARB 0x884C
  187. #endif
  188. #ifndef GL_TEXTURE_COMPARE_FUNC_ARB
  189. #define GL_TEXTURE_COMPARE_FUNC_ARB 0x884D
  190. #endif
  191. #ifndef GL_COMPARE_R_TO_TEXTURE_ARB
  192. #define GL_COMPARE_R_TO_TEXTURE_ARB 0x884E
  193. #endif
  194. #ifndef TEXTURE_COMPARE_FAIL_VALUE_ARB
  195. #define TEXTURE_COMPARE_FAIL_VALUE_ARB 0x80BF
  196. #endif
  197. #if !defined( GL_MAX_TEXTURE_UNITS )
  198. #define GL_MAX_TEXTURE_UNITS 0x84E2
  199. #endif
  200. #ifndef GL_TEXTURE_DEPTH
  201. #define GL_TEXTURE_DEPTH 0x8071
  202. #endif
  203. #ifndef GL_TEXTURE_2D_MULTISAMPLE
  204. #define GL_TEXTURE_2D_MULTISAMPLE 0x9100
  205. #endif
  206. // Integer texture extension as in http://www.opengl.org/registry/specs/EXT/texture_integer.txt
  207. #ifndef GL_EXT_texture_integer
  208. #define GL_RGBA32UI_EXT 0x8D70
  209. #define GL_RGB32UI_EXT 0x8D71
  210. #define GL_ALPHA32UI_EXT 0x8D72
  211. #define GL_INTENSITY32UI_EXT 0x8D73
  212. #define GL_LUMINANCE32UI_EXT 0x8D74
  213. #define GL_LUMINANCE_ALPHA32UI_EXT 0x8D75
  214. #define GL_RGBA16UI_EXT 0x8D76
  215. #define GL_RGB16UI_EXT 0x8D77
  216. #define GL_ALPHA16UI_EXT 0x8D78
  217. #define GL_INTENSITY16UI_EXT 0x8D79
  218. #define GL_LUMINANCE16UI_EXT 0x8D7A
  219. #define GL_LUMINANCE_ALPHA16UI_EXT 0x8D7B
  220. #define GL_RGBA8UI_EXT 0x8D7C
  221. #define GL_RGB8UI_EXT 0x8D7D
  222. #define GL_ALPHA8UI_EXT 0x8D7E
  223. #define GL_INTENSITY8UI_EXT 0x8D7F
  224. #define GL_LUMINANCE8UI_EXT 0x8D80
  225. #define GL_LUMINANCE_ALPHA8UI_EXT 0x8D81
  226. #define GL_RGBA32I_EXT 0x8D82
  227. #define GL_RGB32I_EXT 0x8D83
  228. #define GL_ALPHA32I_EXT 0x8D84
  229. #define GL_INTENSITY32I_EXT 0x8D85
  230. #define GL_LUMINANCE32I_EXT 0x8D86
  231. #define GL_LUMINANCE_ALPHA32I_EXT 0x8D87
  232. #define GL_RGBA16I_EXT 0x8D88
  233. #define GL_RGB16I_EXT 0x8D89
  234. #define GL_ALPHA16I_EXT 0x8D8A
  235. #define GL_INTENSITY16I_EXT 0x8D8B
  236. #define GL_LUMINANCE16I_EXT 0x8D8C
  237. #define GL_LUMINANCE_ALPHA16I_EXT 0x8D8D
  238. #define GL_RGBA8I_EXT 0x8D8E
  239. #define GL_RGB8I_EXT 0x8D8F
  240. #define GL_ALPHA8I_EXT 0x8D90
  241. #define GL_INTENSITY8I_EXT 0x8D91
  242. #define GL_LUMINANCE8I_EXT 0x8D92
  243. #define GL_LUMINANCE_ALPHA8I_EXT 0x8D93
  244. #define GL_RED_INTEGER_EXT 0x8D94
  245. #define GL_GREEN_INTEGER_EXT 0x8D95
  246. #define GL_BLUE_INTEGER_EXT 0x8D96
  247. #define GL_ALPHA_INTEGER_EXT 0x8D97
  248. #define GL_RGB_INTEGER_EXT 0x8D98
  249. #define GL_RGBA_INTEGER_EXT 0x8D99
  250. #define GL_BGR_INTEGER_EXT 0x8D9A
  251. #define GL_BGRA_INTEGER_EXT 0x8D9B
  252. #define GL_LUMINANCE_INTEGER_EXT 0x8D9C
  253. #define GL_LUMINANCE_ALPHA_INTEGER_EXT 0x8D9D
  254. #define GL_RGBA_INTEGER_MODE_EXT 0x8D9E
  255. #endif
  256. #ifndef GL_VERSION_1_1
  257. #define GL_R3_G3_B2 0x2A10
  258. #define GL_RGB4 0x804F
  259. #define GL_RGB5 0x8050
  260. #define GL_RGB8 0x8051
  261. #define GL_RGB10 0x8052
  262. #define GL_RGB12 0x8053
  263. #define GL_RGB16 0x8054
  264. #define GL_RGBA2 0x8055
  265. #define GL_RGBA4 0x8056
  266. #define GL_RGB5_A1 0x8057
  267. #define GL_RGBA8 0x8058
  268. #define GL_RGB10_A2 0x8059
  269. #define GL_RGBA12 0x805A
  270. #define GL_RGBA16 0x805B
  271. #define GL_BGR_EXT 0x80E0
  272. #define GL_BGRA_EXT 0x80E1
  273. #endif
  274. #ifndef GL_ARB_texture_rg
  275. #define GL_RG 0x8227
  276. #define GL_RG_INTEGER 0x8228
  277. #define GL_R8 0x8229
  278. #define GL_R16 0x822A
  279. #define GL_RG8 0x822B
  280. #define GL_RG16 0x822C
  281. #define GL_R16F 0x822D
  282. #define GL_R32F 0x822E
  283. #define GL_RG16F 0x822F
  284. #define GL_RG32F 0x8230
  285. #define GL_R8I 0x8231
  286. #define GL_R8UI 0x8232
  287. #define GL_R16I 0x8233
  288. #define GL_R16UI 0x8234
  289. #define GL_R32I 0x8235
  290. #define GL_R32UI 0x8236
  291. #define GL_RG8I 0x8237
  292. #define GL_RG8UI 0x8238
  293. #define GL_RG16I 0x8239
  294. #define GL_RG16UI 0x823A
  295. #define GL_RG32I 0x823B
  296. #define GL_RG32UI 0x823C
  297. #endif
  298. #ifndef GL_ARB_shader_image_load_store
  299. #define GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT 0x00000001
  300. #define GL_ELEMENT_ARRAY_BARRIER_BIT 0x00000002
  301. #define GL_UNIFORM_BARRIER_BIT 0x00000004
  302. #define GL_TEXTURE_FETCH_BARRIER_BIT 0x00000008
  303. #define GL_SHADER_IMAGE_ACCESS_BARRIER_BIT 0x00000020
  304. #define GL_COMMAND_BARRIER_BIT 0x00000040
  305. #define GL_PIXEL_BUFFER_BARRIER_BIT 0x00000080
  306. #define GL_TEXTURE_UPDATE_BARRIER_BIT 0x00000100
  307. #define GL_BUFFER_UPDATE_BARRIER_BIT 0x00000200
  308. #define GL_FRAMEBUFFER_BARRIER_BIT 0x00000400
  309. #define GL_TRANSFORM_FEEDBACK_BARRIER_BIT 0x00000800
  310. #define GL_ATOMIC_COUNTER_BARRIER_BIT 0x00001000
  311. #define GL_ALL_BARRIER_BITS 0xFFFFFFFF
  312. #define GL_MAX_IMAGE_UNITS 0x8F38
  313. #define GL_MAX_COMBINED_IMAGE_UNITS_AND_FRAGMENT_OUTPUTS 0x8F39
  314. #define GL_IMAGE_BINDING_NAME 0x8F3A
  315. #define GL_IMAGE_BINDING_LEVEL 0x8F3B
  316. #define GL_IMAGE_BINDING_LAYERED 0x8F3C
  317. #define GL_IMAGE_BINDING_LAYER 0x8F3D
  318. #define GL_IMAGE_BINDING_ACCESS 0x8F3E
  319. #define GL_IMAGE_1D 0x904C
  320. #define GL_IMAGE_2D 0x904D
  321. #define GL_IMAGE_3D 0x904E
  322. #define GL_IMAGE_2D_RECT 0x904F
  323. #define GL_IMAGE_CUBE 0x9050
  324. #define GL_IMAGE_BUFFER 0x9051
  325. #define GL_IMAGE_1D_ARRAY 0x9052
  326. #define GL_IMAGE_2D_ARRAY 0x9053
  327. #define GL_IMAGE_CUBE_MAP_ARRAY 0x9054
  328. #define GL_IMAGE_2D_MULTISAMPLE 0x9055
  329. #define GL_IMAGE_2D_MULTISAMPLE_ARRAY 0x9056
  330. #define GL_INT_IMAGE_1D 0x9057
  331. #define GL_INT_IMAGE_2D 0x9058
  332. #define GL_INT_IMAGE_3D 0x9059
  333. #define GL_INT_IMAGE_2D_RECT 0x905A
  334. #define GL_INT_IMAGE_CUBE 0x905B
  335. #define GL_INT_IMAGE_BUFFER 0x905C
  336. #define GL_INT_IMAGE_1D_ARRAY 0x905D
  337. #define GL_INT_IMAGE_2D_ARRAY 0x905E
  338. #define GL_INT_IMAGE_CUBE_MAP_ARRAY 0x905F
  339. #define GL_INT_IMAGE_2D_MULTISAMPLE 0x9060
  340. #define GL_INT_IMAGE_2D_MULTISAMPLE_ARRAY 0x9061
  341. #define GL_UNSIGNED_INT_IMAGE_1D 0x9062
  342. #define GL_UNSIGNED_INT_IMAGE_2D 0x9063
  343. #define GL_UNSIGNED_INT_IMAGE_3D 0x9064
  344. #define GL_UNSIGNED_INT_IMAGE_2D_RECT 0x9065
  345. #define GL_UNSIGNED_INT_IMAGE_CUBE 0x9066
  346. #define GL_UNSIGNED_INT_IMAGE_BUFFER 0x9067
  347. #define GL_UNSIGNED_INT_IMAGE_1D_ARRAY 0x9068
  348. #define GL_UNSIGNED_INT_IMAGE_2D_ARRAY 0x9069
  349. #define GL_UNSIGNED_INT_IMAGE_CUBE_MAP_ARRAY 0x906A
  350. #define GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE 0x906B
  351. #define GL_UNSIGNED_INT_IMAGE_2D_MULTISAMPLE_ARRAY 0x906C
  352. #define GL_MAX_IMAGE_SAMPLES 0x906D
  353. #define GL_IMAGE_BINDING_FORMAT 0x906E
  354. #define GL_IMAGE_FORMAT_COMPATIBILITY_TYPE 0x90C7
  355. #define GL_IMAGE_FORMAT_COMPATIBILITY_BY_SIZE 0x90C8
  356. #define GL_IMAGE_FORMAT_COMPATIBILITY_BY_CLASS 0x90C9
  357. #define GL_MAX_VERTEX_IMAGE_UNIFORMS 0x90CA
  358. #define GL_MAX_TESS_CONTROL_IMAGE_UNIFORMS 0x90CB
  359. #define GL_MAX_TESS_EVALUATION_IMAGE_UNIFORMS 0x90CC
  360. #define GL_MAX_GEOMETRY_IMAGE_UNIFORMS 0x90CD
  361. #define GL_MAX_FRAGMENT_IMAGE_UNIFORMS 0x90CE
  362. #define GL_MAX_COMBINED_IMAGE_UNIFORMS 0x90CF
  363. #endif
  364. //#define OSG_COLLECT_TEXTURE_APPLIED_STATS 1
  365. namespace osg {
  366. // forward declare
  367. class TextureObjectSet;
  368. class TextureObjectManager;
  369. /** Texture pure virtual base class that encapsulates OpenGL texture
  370. * functionality common to the various types of OSG textures.
  371. */
  372. class OSG_EXPORT Texture : public osg::StateAttribute
  373. {
  374. public :
  375. Texture();
  376. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  377. Texture(const Texture& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  378. virtual osg::Object* cloneType() const = 0;
  379. virtual osg::Object* clone(const CopyOp& copyop) const = 0;
  380. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Texture *>(obj)!=NULL; }
  381. virtual const char* libraryName() const { return "osg"; }
  382. virtual const char* className() const { return "Texture"; }
  383. /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
  384. virtual Texture* asTexture() { return this; }
  385. /** Fast alternative to dynamic_cast<> for determining if state attribute is a Texture.*/
  386. virtual const Texture* asTexture() const { return this; }
  387. virtual Type getType() const { return TEXTURE; }
  388. virtual bool isTextureAttribute() const { return true; }
  389. virtual GLenum getTextureTarget() const = 0;
  390. #ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
  391. virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
  392. {
  393. usage.usesTextureMode(getTextureTarget());
  394. return true;
  395. }
  396. #endif
  397. virtual int getTextureWidth() const { return 0; }
  398. virtual int getTextureHeight() const { return 0; }
  399. virtual int getTextureDepth() const { return 0; }
  400. enum WrapParameter {
  401. WRAP_S,
  402. WRAP_T,
  403. WRAP_R
  404. };
  405. enum WrapMode {
  406. CLAMP = GL_CLAMP,
  407. CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
  408. CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER_ARB,
  409. REPEAT = GL_REPEAT,
  410. MIRROR = GL_MIRRORED_REPEAT_IBM
  411. };
  412. /** Sets the texture wrap mode. */
  413. void setWrap(WrapParameter which, WrapMode wrap);
  414. /** Gets the texture wrap mode. */
  415. WrapMode getWrap(WrapParameter which) const;
  416. /** Sets the border color. Only used when wrap mode is CLAMP_TO_BORDER.
  417. * The border color will be casted to the appropriate type to match the
  418. * internal pixel format of the texture. */
  419. void setBorderColor(const Vec4d& color) { _borderColor = color; dirtyTextureParameters(); }
  420. /** Gets the border color. */
  421. const Vec4d& getBorderColor() const { return _borderColor; }
  422. /** Sets the border width. */
  423. void setBorderWidth(GLint width) { _borderWidth = width; dirtyTextureParameters(); }
  424. GLint getBorderWidth() const { return _borderWidth; }
  425. enum FilterParameter {
  426. MIN_FILTER,
  427. MAG_FILTER
  428. };
  429. enum FilterMode {
  430. LINEAR = GL_LINEAR,
  431. LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR,
  432. LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
  433. NEAREST = GL_NEAREST,
  434. NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
  435. NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST
  436. };
  437. /** Sets the texture filter mode. */
  438. void setFilter(FilterParameter which, FilterMode filter);
  439. /** Gets the texture filter mode. */
  440. FilterMode getFilter(FilterParameter which) const;
  441. /** Sets the maximum anisotropy value, default value is 1.0 for no
  442. * anisotropic filtering. If hardware does not support anisotropic
  443. * filtering, use normal filtering (equivalent to a max anisotropy
  444. * value of 1.0. Valid range is 1.0f upwards. The maximum value
  445. * depends on the graphics system. */
  446. void setMaxAnisotropy(float anis);
  447. /** Gets the maximum anisotropy value. */
  448. inline float getMaxAnisotropy() const { return _maxAnisotropy; }
  449. /** Sets the minimum level of detail value. */
  450. void setMinLOD(float minlod);
  451. /** Gets the minimum level of detail value. */
  452. inline float getMinLOD() const { return _minlod; }
  453. /** Sets the maximum level of detail value. */
  454. void setMaxLOD(float maxlod);
  455. /** Gets the maximum level of detail value. */
  456. inline float getMaxLOD() const { return _maxlod; }
  457. /** Gets the level of detail bias value. */
  458. void setLODBias(float lodbias);
  459. /** Sets the level of detail bias value. */
  460. inline float getLODBias() const { return _lodbias; }
  461. /** Configure the source of texture swizzling for all channels */
  462. inline void setSwizzle(const Vec4i& swizzle) { _swizzle = swizzle; dirtyTextureParameters(); };
  463. /** Gets the source of texture swizzling for all channels */
  464. inline const Vec4i& getSwizzle() const { return _swizzle; }
  465. /** Sets the hardware mipmap generation hint. If enabled, it will
  466. * only be used if supported in the graphics system. */
  467. inline void setUseHardwareMipMapGeneration(bool useHardwareMipMapGeneration) { _useHardwareMipMapGeneration = useHardwareMipMapGeneration; }
  468. /** Gets the hardware mipmap generation hint. */
  469. inline bool getUseHardwareMipMapGeneration() const { return _useHardwareMipMapGeneration; }
  470. /** Sets whether or not the apply() function will unreference the image
  471. * data. If enabled, and the image data is only referenced by this
  472. * Texture, apply() will delete the image data. */
  473. inline void setUnRefImageDataAfterApply(bool flag) { _unrefImageDataAfterApply = flag; }
  474. /** Gets whether or not apply() unreferences image data. */
  475. inline bool getUnRefImageDataAfterApply() const { return _unrefImageDataAfterApply; }
  476. /** Sets whether to use client storage for the texture, if supported
  477. * by the graphics system. Note: If enabled, and the graphics system
  478. * supports it, the osg::Image(s) associated with this texture cannot
  479. * be deleted, so the UnRefImageDataAfterApply flag would be ignored. */
  480. inline void setClientStorageHint(bool flag) { _clientStorageHint = flag; }
  481. /** Gets whether to use client storage for the texture. */
  482. inline bool getClientStorageHint() const { return _clientStorageHint; }
  483. /** Sets whether to force the texture to resize images that have dimensions
  484. * that are not a power of two. If enabled, NPOT images will be resized,
  485. * whether or not NPOT textures are supported by the hardware. If disabled,
  486. * NPOT images will not be resized if supported by hardware. */
  487. inline void setResizeNonPowerOfTwoHint(bool flag) { _resizeNonPowerOfTwoHint = flag; }
  488. /** Gets whether texture will force non power to two images to be resized. */
  489. inline bool getResizeNonPowerOfTwoHint() const { return _resizeNonPowerOfTwoHint; }
  490. enum InternalFormatMode {
  491. USE_IMAGE_DATA_FORMAT,
  492. USE_USER_DEFINED_FORMAT,
  493. USE_ARB_COMPRESSION,
  494. USE_S3TC_DXT1_COMPRESSION,
  495. USE_S3TC_DXT3_COMPRESSION,
  496. USE_S3TC_DXT5_COMPRESSION,
  497. USE_PVRTC_2BPP_COMPRESSION,
  498. USE_PVRTC_4BPP_COMPRESSION,
  499. USE_ETC_COMPRESSION,
  500. USE_ETC2_COMPRESSION,
  501. USE_RGTC1_COMPRESSION,
  502. USE_RGTC2_COMPRESSION,
  503. USE_S3TC_DXT1c_COMPRESSION,
  504. USE_S3TC_DXT1a_COMPRESSION
  505. };
  506. /** Sets the internal texture format mode. Note: If the texture format is
  507. * USE_IMAGE_DATA_FORMAT, USE_ARB_COMPRESSION, or USE_S3TC_COMPRESSION,
  508. * the internal format mode is set automatically and will overwrite the
  509. * previous _internalFormat. */
  510. inline void setInternalFormatMode(InternalFormatMode mode) { _internalFormatMode = mode; }
  511. /** Gets the internal texture format mode. */
  512. inline InternalFormatMode getInternalFormatMode() const { return _internalFormatMode; }
  513. /** Sets the internal texture format. Implicitly sets the
  514. * internalFormatMode to USE_USER_DEFINED_FORMAT.
  515. * The corresponding internal format type will be computed. */
  516. inline void setInternalFormat(GLint internalFormat)
  517. {
  518. _internalFormatMode = USE_USER_DEFINED_FORMAT;
  519. _internalFormat = internalFormat;
  520. computeInternalFormatType();
  521. }
  522. /** Gets the internal texture format. */
  523. inline GLint getInternalFormat() const { if (_internalFormat==0) computeInternalFormat(); return _internalFormat; }
  524. /** Return true if the internal format is one of the compressed formats.*/
  525. bool isCompressedInternalFormat() const;
  526. /** Sets the external source image format, used as a fallback when no osg::Image is attached to provide the source image format. */
  527. inline void setSourceFormat(GLenum sourceFormat) { _sourceFormat = sourceFormat; }
  528. /** Gets the external source image format. */
  529. inline GLenum getSourceFormat() const { return _sourceFormat; }
  530. /** Sets the external source data type, used as a fallback when no osg::Image is attached to provide the source image format.*/
  531. inline void setSourceType(GLenum sourceType) { _sourceType = sourceType; }
  532. /** Gets the external source data type.*/
  533. inline GLenum getSourceType() const { return _sourceType; }
  534. /** Texture type determined by the internal texture format */
  535. enum InternalFormatType{
  536. //! default OpenGL format (clamped values to [0,1) or [0,255])
  537. NORMALIZED = 0x0,
  538. //! float values, Shader Model 3.0 (see ARB_texture_float)
  539. FLOAT = 0x1,
  540. //! Signed integer values (see EXT_texture_integer)
  541. SIGNED_INTEGER = 0x2,
  542. //! Unsigned integer value (see EXT_texture_integer)
  543. UNSIGNED_INTEGER = 0x4
  544. };
  545. /** Get the internal texture format type. */
  546. inline InternalFormatType getInternalFormatType() const { return _internalFormatType; }
  547. /* select the size internal format to use based on Image when available or Texture format settings.*/
  548. GLenum selectSizedInternalFormat(const osg::Image* image=0) const;
  549. class TextureObject;
  550. /** return true if the texture image data has been modified and the associated GL texture object needs to be updated.*/
  551. virtual bool isDirty(unsigned int /*contextID*/) const { return false; }
  552. /** Returns a pointer to the TextureObject for the current context. */
  553. inline TextureObject* getTextureObject(unsigned int contextID) const
  554. {
  555. return _textureObjectBuffer[contextID].get();
  556. }
  557. inline void setTextureObject(unsigned int contextID, TextureObject* to)
  558. {
  559. _textureObjectBuffer[contextID] = to;
  560. }
  561. /** Forces a recompile on next apply() of associated OpenGL texture
  562. * objects. */
  563. void dirtyTextureObject();
  564. /** Returns true if the texture objects for all the required graphics
  565. * contexts are loaded. */
  566. bool areAllTextureObjectsLoaded() const;
  567. /** Gets the dirty flag for the current contextID. */
  568. inline unsigned int& getTextureParameterDirty(unsigned int contextID) const
  569. {
  570. return _texParametersDirtyList[contextID];
  571. }
  572. /** Force a reset on next apply() of associated OpenGL texture
  573. * parameters. */
  574. void dirtyTextureParameters();
  575. /** Force a manual allocation of the mipmap levels on the next apply() call.
  576. * User is responsible for filling the mipmap levels with valid data.
  577. * The OpenGL's glGenerateMipmapEXT function is used to generate the mipmap levels.
  578. * If glGenerateMipmapEXT is not supported or texture's internal format is not supported
  579. * by the glGenerateMipmapEXT, then empty mipmap levels will
  580. * be allocated manually. The mipmap levels are also allocated if a non-mipmapped
  581. * min filter is used. */
  582. void allocateMipmapLevels();
  583. /** Sets GL_TEXTURE_COMPARE_MODE_ARB to GL_COMPARE_R_TO_TEXTURE_ARB
  584. * See http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow.txt. */
  585. void setShadowComparison(bool flag) { _use_shadow_comparison = flag; }
  586. bool getShadowComparison() const { return _use_shadow_comparison; }
  587. enum ShadowCompareFunc {
  588. NEVER = GL_NEVER,
  589. LESS = GL_LESS,
  590. EQUAL = GL_EQUAL,
  591. LEQUAL = GL_LEQUAL,
  592. GREATER = GL_GREATER,
  593. NOTEQUAL = GL_NOTEQUAL,
  594. GEQUAL = GL_GEQUAL,
  595. ALWAYS = GL_ALWAYS
  596. };
  597. /** Sets shadow texture comparison function. */
  598. void setShadowCompareFunc(ShadowCompareFunc func) { _shadow_compare_func = func; }
  599. ShadowCompareFunc getShadowCompareFunc() const { return _shadow_compare_func; }
  600. enum ShadowTextureMode {
  601. LUMINANCE = GL_LUMINANCE,
  602. INTENSITY = GL_INTENSITY,
  603. ALPHA = GL_ALPHA,
  604. NONE = GL_NONE
  605. };
  606. /** Sets shadow texture mode after comparison. */
  607. void setShadowTextureMode(ShadowTextureMode mode) { _shadow_texture_mode = mode; }
  608. ShadowTextureMode getShadowTextureMode() const { return _shadow_texture_mode; }
  609. /** Sets the TEXTURE_COMPARE_FAIL_VALUE_ARB texture parameter. See
  610. * http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow_ambient.txt. */
  611. void setShadowAmbient(float shadow_ambient) { _shadow_ambient = shadow_ambient; }
  612. float getShadowAmbient() const { return _shadow_ambient; }
  613. /** Sets the texture image for the specified face. */
  614. virtual void setImage(unsigned int face, Image* image) = 0;
  615. template<class T> void setImage(unsigned int face, const ref_ptr<T>& image) { setImage(face, image.get()); }
  616. /** Gets the texture image for the specified face. */
  617. virtual Image* getImage(unsigned int face) = 0;
  618. /** Gets the const texture image for specified face. */
  619. virtual const Image* getImage(unsigned int face) const = 0;
  620. /** Gets the number of images that can be assigned to this Texture. */
  621. virtual unsigned int getNumImages() const = 0;
  622. /** Set the PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
  623. void setReadPBuffer(GraphicsContext* context) { _readPBuffer = context; }
  624. template<class T> void setReadPBuffer(const ref_ptr<T>& context) { setReadPBuffer(context.get()); }
  625. /** Get the PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
  626. GraphicsContext* getReadPBuffer() { return _readPBuffer.get(); }
  627. /** Get the const PBuffer graphics context to read from when using PBuffers for RenderToTexture.*/
  628. const GraphicsContext* getReadPBuffer() const { return _readPBuffer.get(); }
  629. /** Texture is a pure virtual base class, apply must be overridden. */
  630. virtual void apply(State& state) const = 0;
  631. /** Calls apply(state) to compile the texture. */
  632. virtual void compileGLObjects(State& state) const;
  633. /** Resize any per context GLObject buffers to specified size. */
  634. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  635. /** If State is non-zero, this function releases OpenGL objects for
  636. * the specified graphics context. Otherwise, releases OpenGL objects
  637. * for all graphics contexts. */
  638. virtual void releaseGLObjects(State* state=0) const;
  639. /** Determine whether the given internalFormat is a compressed
  640. * image format. */
  641. static bool isCompressedInternalFormat(GLint internalFormat);
  642. /** Determine the size of a compressed image, given the internalFormat,
  643. * the width, the height, and the depth of the image. The block size
  644. * and the size are output parameters. */
  645. static void getCompressedSize(GLenum internalFormat, GLint width, GLint height, GLint depth, GLint& blockSize, GLint& size);
  646. /** Helper method. Creates the texture, but doesn't set or use a
  647. * texture binding. Note: Don't call this method directly unless
  648. * you're implementing a subload callback. */
  649. void applyTexImage2D_load(State& state, GLenum target, const Image* image, GLsizei width, GLsizei height,GLsizei numMipmapLevels) const;
  650. /** Helper method. Subloads images into the texture, but doesn't set
  651. * or use a texture binding. Note: Don't call this method directly
  652. * unless you're implementing a subload callback. */
  653. void applyTexImage2D_subload(State& state, GLenum target, const Image* image, GLsizei width, GLsizei height, GLint inInternalFormat, GLsizei numMipmapLevels) const;
  654. /** Returned by mipmapBeforeTexImage() to indicate what
  655. * mipmapAfterTexImage() should do */
  656. enum GenerateMipmapMode
  657. {
  658. GENERATE_MIPMAP_NONE,
  659. GENERATE_MIPMAP,
  660. GENERATE_MIPMAP_TEX_PARAMETER
  661. };
  662. protected :
  663. virtual ~Texture();
  664. virtual void computeInternalFormat() const = 0;
  665. /** Computes the internal format from Image parameters. */
  666. void computeInternalFormatWithImage(const osg::Image& image) const;
  667. /** Computes the texture dimension for the given Image. */
  668. void computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& width, GLsizei& height,GLsizei& numMipmapLevels) const;
  669. /** Computes the internal format type. */
  670. void computeInternalFormatType() const;
  671. /** Helper method. Sets texture parameters. */
  672. void applyTexParameters(GLenum target, State& state) const;
  673. /** Returns true if _useHardwareMipMapGeneration is true and either
  674. * glGenerateMipmapEXT() or GL_GENERATE_MIPMAP_SGIS are supported. */
  675. bool isHardwareMipmapGenerationEnabled(const State& state) const;
  676. /** Returns true if the associated Image should be released and it's safe to do so. */
  677. bool isSafeToUnrefImageData(const State& state) const {
  678. return (_unrefImageDataAfterApply && state.getMaxTexturePoolSize()==0 && areAllTextureObjectsLoaded());
  679. }
  680. /** Helper methods to be called before and after calling
  681. * gl[Compressed][Copy]Tex[Sub]Image2D to handle generating mipmaps. */
  682. GenerateMipmapMode mipmapBeforeTexImage(const State& state, bool hardwareMipmapOn) const;
  683. void mipmapAfterTexImage(State& state, GenerateMipmapMode beforeResult) const;
  684. /** Helper method to generate mipmap levels by calling of glGenerateMipmapEXT.
  685. * If it is not supported, then call the virtual allocateMipmap() method */
  686. void generateMipmap(State& state) const;
  687. /** Allocate mipmap levels of the texture by subsequent calling of glTexImage* function. */
  688. virtual void allocateMipmap(State& state) const = 0;
  689. /** Returns -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  690. int compareTexture(const Texture& rhs) const;
  691. /** Returns -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  692. int compareTextureObjects(const Texture& rhs) const;
  693. typedef buffered_value<unsigned int> TexParameterDirtyList;
  694. mutable TexParameterDirtyList _texParametersDirtyList;
  695. mutable TexParameterDirtyList _texMipmapGenerationDirtyList;
  696. WrapMode _wrap_s;
  697. WrapMode _wrap_t;
  698. WrapMode _wrap_r;
  699. FilterMode _min_filter;
  700. FilterMode _mag_filter;
  701. float _maxAnisotropy;
  702. float _minlod;
  703. float _maxlod;
  704. float _lodbias;
  705. Vec4i _swizzle;
  706. bool _useHardwareMipMapGeneration;
  707. bool _unrefImageDataAfterApply;
  708. bool _clientStorageHint;
  709. bool _resizeNonPowerOfTwoHint;
  710. Vec4d _borderColor;
  711. GLint _borderWidth;
  712. InternalFormatMode _internalFormatMode;
  713. mutable InternalFormatType _internalFormatType;
  714. mutable GLint _internalFormat;
  715. mutable GLenum _sourceFormat;
  716. mutable GLenum _sourceType;
  717. bool _use_shadow_comparison;
  718. ShadowCompareFunc _shadow_compare_func;
  719. ShadowTextureMode _shadow_texture_mode;
  720. float _shadow_ambient;
  721. public:
  722. struct OSG_EXPORT TextureProfile
  723. {
  724. inline TextureProfile(GLenum target):
  725. _target(target),
  726. _numMipmapLevels(0),
  727. _internalFormat(0),
  728. _width(0),
  729. _height(0),
  730. _depth(0),
  731. _border(0),
  732. _size(0) {}
  733. inline TextureProfile(GLenum target,
  734. GLint numMipmapLevels,
  735. GLenum internalFormat,
  736. GLsizei width,
  737. GLsizei height,
  738. GLsizei depth,
  739. GLint border):
  740. _target(target),
  741. _numMipmapLevels(numMipmapLevels),
  742. _internalFormat(internalFormat),
  743. _width(width),
  744. _height(height),
  745. _depth(depth),
  746. _border(border),
  747. _size(0) { computeSize(); }
  748. #define LESSTHAN(A,B) if (A<B) return true; if (B<A) return false;
  749. #define FINALLESSTHAN(A,B) return (A<B);
  750. bool operator < (const TextureProfile& rhs) const
  751. {
  752. LESSTHAN(_size,rhs._size);
  753. LESSTHAN(_target,rhs._target);
  754. LESSTHAN(_numMipmapLevels,rhs._numMipmapLevels);
  755. LESSTHAN(_internalFormat,rhs._internalFormat);
  756. LESSTHAN(_width,rhs._width);
  757. LESSTHAN(_height,rhs._height);
  758. LESSTHAN(_depth,rhs._depth);
  759. FINALLESSTHAN(_border, rhs._border);
  760. }
  761. bool operator == (const TextureProfile& rhs) const
  762. {
  763. return _target == rhs._target &&
  764. _numMipmapLevels == rhs._numMipmapLevels &&
  765. _internalFormat == rhs._internalFormat &&
  766. _width == rhs._width &&
  767. _height == rhs._height &&
  768. _depth == rhs._depth &&
  769. _border == rhs._border;
  770. }
  771. inline void set(GLint numMipmapLevels,
  772. GLenum internalFormat,
  773. GLsizei width,
  774. GLsizei height,
  775. GLsizei depth,
  776. GLint border)
  777. {
  778. _numMipmapLevels = numMipmapLevels;
  779. _internalFormat = internalFormat;
  780. _width = width;
  781. _height = height;
  782. _depth = depth;
  783. _border = border;
  784. computeSize();
  785. }
  786. inline bool match(GLenum target,
  787. GLint numMipmapLevels,
  788. GLenum internalFormat,
  789. GLsizei width,
  790. GLsizei height,
  791. GLsizei depth,
  792. GLint border)
  793. {
  794. return (_target == target) &&
  795. (_numMipmapLevels == numMipmapLevels) &&
  796. (_internalFormat == internalFormat) &&
  797. (_width == width) &&
  798. (_height == height) &&
  799. (_depth == depth) &&
  800. (_border == border);
  801. }
  802. void computeSize();
  803. GLenum _target;
  804. GLint _numMipmapLevels;
  805. GLenum _internalFormat;
  806. GLsizei _width;
  807. GLsizei _height;
  808. GLsizei _depth;
  809. GLint _border;
  810. unsigned int _size;
  811. };
  812. class OSG_EXPORT TextureObject : public GraphicsObject
  813. {
  814. public:
  815. inline TextureObject(Texture* texture, GLuint id, GLenum target):
  816. _id(id),
  817. _profile(target),
  818. _set(0),
  819. _previous(0),
  820. _next(0),
  821. _texture(texture),
  822. _allocated(false),
  823. _frameLastUsed(0),
  824. _timeStamp(0) {}
  825. inline TextureObject(Texture* texture, GLuint id, const TextureProfile& profile):
  826. _id(id),
  827. _profile(profile),
  828. _set(0),
  829. _previous(0),
  830. _next(0),
  831. _texture(texture),
  832. _allocated(false),
  833. _frameLastUsed(0),
  834. _timeStamp(0) {}
  835. inline TextureObject(Texture* texture,
  836. GLuint id,
  837. GLenum target,
  838. GLint numMipmapLevels,
  839. GLenum internalFormat,
  840. GLsizei width,
  841. GLsizei height,
  842. GLsizei depth,
  843. GLint border):
  844. _id(id),
  845. _profile(target,numMipmapLevels,internalFormat,width,height,depth,border),
  846. _set(0),
  847. _previous(0),
  848. _next(0),
  849. _texture(texture),
  850. _allocated(false),
  851. _frameLastUsed(0),
  852. _timeStamp(0) {}
  853. inline bool match(GLenum target,
  854. GLint numMipmapLevels,
  855. GLenum internalFormat,
  856. GLsizei width,
  857. GLsizei height,
  858. GLsizei depth,
  859. GLint border)
  860. {
  861. return isReusable() &&
  862. _profile.match(target,numMipmapLevels,internalFormat,width,height,depth,border);
  863. }
  864. void bind();
  865. inline GLenum id() const { return _id; }
  866. inline GLenum target() const { return _profile._target; }
  867. inline unsigned int size() const { return _profile._size; }
  868. inline void setTexture(Texture* texture) { _texture = texture; }
  869. inline Texture* getTexture() const { return _texture; }
  870. inline void setTimeStamp(double timestamp) { _timeStamp = timestamp; }
  871. inline double getTimeStamp() const { return _timeStamp; }
  872. inline void setAllocated(bool allocated=true) { _allocated = allocated; }
  873. void setAllocated(GLint numMipmapLevels,
  874. GLenum internalFormat,
  875. GLsizei width,
  876. GLsizei height,
  877. GLsizei depth,
  878. GLint border);
  879. inline bool isAllocated() const { return _allocated; }
  880. inline bool isReusable() const { return _allocated && _profile._width!=0; }
  881. /** release TextureObject to the orphan list to be reused or deleted.*/
  882. void release();
  883. GLuint _id;
  884. TextureProfile _profile;
  885. TextureObjectSet* _set;
  886. TextureObject* _previous;
  887. TextureObject* _next;
  888. Texture* _texture;
  889. bool _allocated;
  890. unsigned int _frameLastUsed;
  891. double _timeStamp;
  892. protected:
  893. virtual ~TextureObject();
  894. };
  895. typedef std::list< ref_ptr<TextureObject> > TextureObjectList;
  896. static osg::ref_ptr<TextureObject> generateTextureObject(const Texture* texture, unsigned int contextID,GLenum target);
  897. static osg::ref_ptr<TextureObject> generateTextureObject(const Texture* texture,
  898. unsigned int contextID,
  899. GLenum target,
  900. GLint numMipmapLevels,
  901. GLenum internalFormat,
  902. GLsizei width,
  903. GLsizei height,
  904. GLsizei depth,
  905. GLint border);
  906. TextureObject* generateAndAssignTextureObject(unsigned int contextID, GLenum target) const;
  907. TextureObject* generateAndAssignTextureObject(unsigned int contextID,
  908. GLenum target,
  909. GLint numMipmapLevels,
  910. GLenum internalFormat,
  911. GLsizei width,
  912. GLsizei height,
  913. GLsizei depth,
  914. GLint border) const;
  915. protected:
  916. typedef buffered_object< ref_ptr<TextureObject> > TextureObjectBuffer;
  917. mutable TextureObjectBuffer _textureObjectBuffer;
  918. mutable ref_ptr<GraphicsContext> _readPBuffer;
  919. };
  920. class OSG_EXPORT TextureObjectSet : public Referenced
  921. {
  922. public:
  923. TextureObjectSet(TextureObjectManager* parent, const Texture::TextureProfile& profile);
  924. const Texture::TextureProfile& getProfile() const { return _profile; }
  925. void handlePendingOrphandedTextureObjects();
  926. void deleteAllTextureObjects();
  927. void discardAllTextureObjects();
  928. void flushAllDeletedTextureObjects();
  929. void discardAllDeletedTextureObjects();
  930. void flushDeletedTextureObjects(double currentTime, double& availableTime);
  931. osg::ref_ptr<Texture::TextureObject> takeFromOrphans(Texture* texture);
  932. osg::ref_ptr<Texture::TextureObject> takeOrGenerate(Texture* texture);
  933. void moveToBack(Texture::TextureObject* to);
  934. void addToBack(Texture::TextureObject* to);
  935. void orphan(Texture::TextureObject* to);
  936. void remove(Texture::TextureObject* to);
  937. void moveToSet(Texture::TextureObject* to, TextureObjectSet* set);
  938. unsigned int size() const { return _profile._size * _numOfTextureObjects; }
  939. bool makeSpace(unsigned int& size);
  940. bool checkConsistency() const;
  941. TextureObjectManager* getParent() { return _parent; }
  942. unsigned int computeNumTextureObjectsInList() const;
  943. unsigned int getNumOfTextureObjects() const { return _numOfTextureObjects; }
  944. unsigned int getNumOrphans() const { return static_cast<unsigned int>(_orphanedTextureObjects.size()); }
  945. unsigned int getNumPendingOrphans() const { return static_cast<unsigned int>(_pendingOrphanedTextureObjects.size()); }
  946. protected:
  947. virtual ~TextureObjectSet();
  948. OpenThreads::Mutex _mutex;
  949. TextureObjectManager* _parent;
  950. unsigned int _contextID;
  951. Texture::TextureProfile _profile;
  952. unsigned int _numOfTextureObjects;
  953. Texture::TextureObjectList _orphanedTextureObjects;
  954. Texture::TextureObjectList _pendingOrphanedTextureObjects;
  955. Texture::TextureObject* _head;
  956. Texture::TextureObject* _tail;
  957. };
  958. class OSG_EXPORT TextureObjectManager : public GraphicsObjectManager
  959. {
  960. public:
  961. TextureObjectManager(unsigned int contextID);
  962. void setNumberActiveTextureObjects(unsigned int size) { _numActiveTextureObjects = size; }
  963. unsigned int& getNumberActiveTextureObjects() { return _numActiveTextureObjects; }
  964. unsigned int getNumberActiveTextureObjects() const { return _numActiveTextureObjects; }
  965. void setNumberOrphanedTextureObjects(unsigned int size) { _numOrphanedTextureObjects = size; }
  966. unsigned int& getNumberOrphanedTextureObjects() { return _numOrphanedTextureObjects; }
  967. unsigned int getNumberOrphanedTextureObjects() const { return _numOrphanedTextureObjects; }
  968. void setCurrTexturePoolSize(unsigned int size) { _currTexturePoolSize = size; }
  969. unsigned int& getCurrTexturePoolSize() { return _currTexturePoolSize; }
  970. unsigned int getCurrTexturePoolSize() const { return _currTexturePoolSize; }
  971. void setMaxTexturePoolSize(unsigned int size);
  972. unsigned int getMaxTexturePoolSize() const { return _maxTexturePoolSize; }
  973. bool hasSpace(unsigned int size) const { return (_currTexturePoolSize+size)<=_maxTexturePoolSize; }
  974. bool makeSpace(unsigned int size);
  975. osg::ref_ptr<Texture::TextureObject> generateTextureObject(const Texture* texture, GLenum target);
  976. osg::ref_ptr<Texture::TextureObject> generateTextureObject(const Texture* texture,
  977. GLenum target,
  978. GLint numMipmapLevels,
  979. GLenum internalFormat,
  980. GLsizei width,
  981. GLsizei height,
  982. GLsizei depth,
  983. GLint border);
  984. void handlePendingOrphandedTextureObjects();
  985. void deleteAllGLObjects();
  986. void discardAllGLObjects();
  987. void flushAllDeletedGLObjects();
  988. void discardAllDeletedGLObjects();
  989. void flushDeletedGLObjects(double currentTime, double& availableTime);
  990. TextureObjectSet* getTextureObjectSet(const Texture::TextureProfile& profile);
  991. void newFrame(osg::FrameStamp* fs);
  992. void resetStats();
  993. void reportStats(std::ostream& out);
  994. void recomputeStats(std::ostream& out) const;
  995. bool checkConsistency() const;
  996. unsigned int& getFrameNumber() { return _frameNumber; }
  997. unsigned int& getNumberFrames() { return _numFrames; }
  998. unsigned int& getNumberDeleted() { return _numDeleted; }
  999. double& getDeleteTime() { return _deleteTime; }
  1000. unsigned int& getNumberGenerated() { return _numGenerated; }
  1001. double& getGenerateTime() { return _generateTime; }
  1002. protected:
  1003. ~TextureObjectManager();
  1004. typedef std::map< Texture::TextureProfile, osg::ref_ptr<TextureObjectSet> > TextureSetMap;
  1005. unsigned int _numActiveTextureObjects;
  1006. unsigned int _numOrphanedTextureObjects;
  1007. unsigned int _currTexturePoolSize;
  1008. unsigned int _maxTexturePoolSize;
  1009. TextureSetMap _textureSetMap;
  1010. unsigned int _frameNumber;
  1011. unsigned int _numFrames;
  1012. unsigned int _numDeleted;
  1013. double _deleteTime;
  1014. unsigned int _numGenerated;
  1015. double _generateTime;
  1016. };
  1017. }
  1018. #endif