tinyGraphicsStateGuardian.cxx 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306
  1. // Filename: tinyGraphicsStateGuardian.cxx
  2. // Created by: drose (24Apr08)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) Carnegie Mellon University. All rights reserved.
  8. //
  9. // All use of this software is subject to the terms of the revised BSD
  10. // license. You should have received a copy of this license along
  11. // with this source code in a file named "LICENSE."
  12. //
  13. ////////////////////////////////////////////////////////////////////
  14. #include "tinyGraphicsStateGuardian.h"
  15. #include "tinyGeomMunger.h"
  16. #include "tinyTextureContext.h"
  17. #include "config_tinydisplay.h"
  18. #include "pStatTimer.h"
  19. #include "geomVertexReader.h"
  20. #include "ambientLight.h"
  21. #include "pointLight.h"
  22. #include "directionalLight.h"
  23. #include "spotlight.h"
  24. #include "bitMask.h"
  25. #include "zgl.h"
  26. #include "zmath.h"
  27. #include "ztriangle_table.h"
  28. #include "store_pixel_table.h"
  29. TypeHandle TinyGraphicsStateGuardian::_type_handle;
  30. PStatCollector TinyGraphicsStateGuardian::_vertices_immediate_pcollector("Vertices:Immediate mode");
  31. PStatCollector TinyGraphicsStateGuardian::_draw_transform_pcollector("Draw:Transform");
  32. ////////////////////////////////////////////////////////////////////
  33. // Function: TinyGraphicsStateGuardian::Constructor
  34. // Access: Public
  35. // Description:
  36. ////////////////////////////////////////////////////////////////////
  37. TinyGraphicsStateGuardian::
  38. TinyGraphicsStateGuardian(GraphicsPipe *pipe,
  39. TinyGraphicsStateGuardian *share_with) :
  40. GraphicsStateGuardian(CS_yup_right, pipe),
  41. _textures_lru("textures_lru", td_texture_ram)
  42. {
  43. _current_frame_buffer = NULL;
  44. _aux_frame_buffer = NULL;
  45. _c = NULL;
  46. _vertices = NULL;
  47. _vertices_size = 0;
  48. }
  49. ////////////////////////////////////////////////////////////////////
  50. // Function: TinyGraphicsStateGuardian::Destructor
  51. // Access: Public
  52. // Description:
  53. ////////////////////////////////////////////////////////////////////
  54. TinyGraphicsStateGuardian::
  55. ~TinyGraphicsStateGuardian() {
  56. }
  57. ////////////////////////////////////////////////////////////////////
  58. // Function: TinyGraphicsStateGuardian::reset
  59. // Access: Public, Virtual
  60. // Description: Resets all internal state as if the gsg were newly
  61. // created.
  62. ////////////////////////////////////////////////////////////////////
  63. void TinyGraphicsStateGuardian::
  64. reset() {
  65. free_pointers();
  66. GraphicsStateGuardian::reset();
  67. if (_c != (GLContext *)NULL) {
  68. glClose(_c);
  69. _c = NULL;
  70. }
  71. _c = (GLContext *)gl_zalloc(sizeof(GLContext));
  72. glInit(_c, _current_frame_buffer);
  73. _c->draw_triangle_front = gl_draw_triangle_fill;
  74. _c->draw_triangle_back = gl_draw_triangle_fill;
  75. _supported_geom_rendering =
  76. Geom::GR_point |
  77. Geom::GR_indexed_other |
  78. Geom::GR_flat_last_vertex;
  79. _max_texture_dimension = (1 << ZB_POINT_ST_FRAC_BITS);
  80. _max_lights = MAX_LIGHTS;
  81. _color_scale_via_lighting = false;
  82. _alpha_scale_via_texture = false;
  83. _runtime_color_scale = true;
  84. // Now that the GSG has been initialized, make it available for
  85. // optimizations.
  86. add_gsg(this);
  87. }
  88. ////////////////////////////////////////////////////////////////////
  89. // Function: TinyGraphicsStateGuardian::free_pointers
  90. // Access: Protected, Virtual
  91. // Description: Frees some memory that was explicitly allocated
  92. // within the glgsg.
  93. ////////////////////////////////////////////////////////////////////
  94. void TinyGraphicsStateGuardian::
  95. free_pointers() {
  96. if (_aux_frame_buffer != (ZBuffer *)NULL) {
  97. ZB_close(_aux_frame_buffer);
  98. _aux_frame_buffer = NULL;
  99. }
  100. if (_vertices != (GLVertex *)NULL) {
  101. PANDA_FREE_ARRAY(_vertices);
  102. _vertices = NULL;
  103. }
  104. _vertices_size = 0;
  105. }
  106. ////////////////////////////////////////////////////////////////////
  107. // Function: TinyGraphicsStateGuardian::close_gsg
  108. // Access: Protected, Virtual
  109. // Description: This is called by the associated GraphicsWindow when
  110. // close_window() is called. It should null out the
  111. // _win pointer and possibly free any open resources
  112. // associated with the GSG.
  113. ////////////////////////////////////////////////////////////////////
  114. void TinyGraphicsStateGuardian::
  115. close_gsg() {
  116. GraphicsStateGuardian::close_gsg();
  117. if (_c != (GLContext *)NULL) {
  118. glClose(_c);
  119. _c = NULL;
  120. }
  121. }
  122. ////////////////////////////////////////////////////////////////////
  123. // Function: TinyGraphicsStateGuardian::depth_offset_decals
  124. // Access: Public, Virtual
  125. // Description: Returns true if this GSG can implement decals using a
  126. // DepthOffsetAttrib, or false if that is unreliable
  127. // and the three-step rendering process should be used
  128. // instead.
  129. ////////////////////////////////////////////////////////////////////
  130. bool TinyGraphicsStateGuardian::
  131. depth_offset_decals() {
  132. return false;
  133. }
  134. ////////////////////////////////////////////////////////////////////
  135. // Function: TinyGraphicsStateGuardian::make_geom_munger
  136. // Access: Public, Virtual
  137. // Description: Creates a new GeomMunger object to munge vertices
  138. // appropriate to this GSG for the indicated state.
  139. ////////////////////////////////////////////////////////////////////
  140. PT(GeomMunger) TinyGraphicsStateGuardian::
  141. make_geom_munger(const RenderState *state, Thread *current_thread) {
  142. PT(TinyGeomMunger) munger = new TinyGeomMunger(this, state);
  143. return GeomMunger::register_munger(munger, current_thread);
  144. }
  145. ////////////////////////////////////////////////////////////////////
  146. // Function: TinyGraphicsStateGuardian::clear
  147. // Access: Public
  148. // Description: Clears the framebuffer within the current
  149. // DisplayRegion, according to the flags indicated by
  150. // the given DrawableRegion object.
  151. //
  152. // This does not set the DisplayRegion first. You
  153. // should call prepare_display_region() to specify the
  154. // region you wish the clear operation to apply to.
  155. ////////////////////////////////////////////////////////////////////
  156. void TinyGraphicsStateGuardian::
  157. clear(DrawableRegion *clearable) {
  158. PStatTimer timer(_clear_pcollector);
  159. if ((!clearable->get_clear_color_active())&&
  160. (!clearable->get_clear_depth_active())&&
  161. (!clearable->get_clear_stencil_active())) {
  162. return;
  163. }
  164. set_state_and_transform(RenderState::make_empty(), _internal_transform);
  165. bool clear_color = false;
  166. int r, g, b, a;
  167. if (clearable->get_clear_color_active()) {
  168. Colorf v = clearable->get_clear_color();
  169. r = (int)(v[0] * 0xffff);
  170. g = (int)(v[1] * 0xffff);
  171. b = (int)(v[2] * 0xffff);
  172. a = (int)(v[3] * 0xffff);
  173. clear_color = true;
  174. }
  175. bool clear_z = false;
  176. int z;
  177. if (clearable->get_clear_depth_active()) {
  178. // We ignore the specified depth clear value, since we don't
  179. // support alternate depth compare functions anyway.
  180. z = 0;
  181. clear_z = true;
  182. }
  183. ZB_clear_viewport(_c->zb, clear_z, z,
  184. clear_color, r, g, b, a,
  185. _c->viewport.xmin, _c->viewport.ymin,
  186. _c->viewport.xsize, _c->viewport.ysize);
  187. }
  188. ////////////////////////////////////////////////////////////////////
  189. // Function: TinyGraphicsStateGuardian::prepare_display_region
  190. // Access: Public, Virtual
  191. // Description: Prepare a display region for rendering (set up
  192. // scissor region and viewport)
  193. ////////////////////////////////////////////////////////////////////
  194. void TinyGraphicsStateGuardian::
  195. prepare_display_region(DisplayRegionPipelineReader *dr,
  196. Lens::StereoChannel stereo_channel) {
  197. nassertv(dr != (DisplayRegionPipelineReader *)NULL);
  198. GraphicsStateGuardian::prepare_display_region(dr, stereo_channel);
  199. int xmin, ymin, xsize, ysize;
  200. dr->get_region_pixels_i(xmin, ymin, xsize, ysize);
  201. float pixel_factor = _current_display_region->get_pixel_factor();
  202. if (pixel_factor != 1.0) {
  203. // Render into an aux buffer, and zoom it up into the main
  204. // frame buffer later.
  205. xmin = 0;
  206. ymin = 0;
  207. xsize = int(xsize * pixel_factor);
  208. ysize = int(ysize * pixel_factor);
  209. if (_aux_frame_buffer == (ZBuffer *)NULL) {
  210. // We add 3 to xsize, since ZB_open may resize the frame buffer
  211. // down by up to 3 pixels to make it fit within the
  212. // word-alignment rule.
  213. _aux_frame_buffer = ZB_open(xsize + 3, ysize, ZB_MODE_RGBA, 0, 0, 0, 0);
  214. } else if (_aux_frame_buffer->xsize < xsize || _aux_frame_buffer->ysize < ysize) {
  215. ZB_resize(_aux_frame_buffer, NULL,
  216. max(_aux_frame_buffer->xsize, xsize) + 3,
  217. max(_aux_frame_buffer->ysize, ysize));
  218. }
  219. _c->zb = _aux_frame_buffer;
  220. } else {
  221. // Render directly into the main frame buffer.
  222. _c->zb = _current_frame_buffer;
  223. }
  224. _c->viewport.xmin = xmin;
  225. _c->viewport.ymin = ymin;
  226. _c->viewport.xsize = xsize;
  227. _c->viewport.ysize = ysize;
  228. gl_eval_viewport(_c);
  229. GLViewport *v = &_c->viewport;
  230. }
  231. ////////////////////////////////////////////////////////////////////
  232. // Function: TinyGraphicsStateGuardian::calc_projection_mat
  233. // Access: Public, Virtual
  234. // Description: Given a lens, calculates the appropriate projection
  235. // matrix for use with this gsg. Note that the
  236. // projection matrix depends a lot upon the coordinate
  237. // system of the rendering API.
  238. //
  239. // The return value is a TransformState if the lens is
  240. // acceptable, NULL if it is not.
  241. ////////////////////////////////////////////////////////////////////
  242. CPT(TransformState) TinyGraphicsStateGuardian::
  243. calc_projection_mat(const Lens *lens) {
  244. if (lens == (Lens *)NULL) {
  245. return NULL;
  246. }
  247. if (!lens->is_linear()) {
  248. return NULL;
  249. }
  250. // The projection matrix must always be right-handed Y-up, even if
  251. // our coordinate system of choice is otherwise, because certain GL
  252. // calls (specifically glTexGen(GL_SPHERE_MAP)) assume this kind of
  253. // a coordinate system. Sigh. In order to implement a Z-up (or
  254. // other arbitrary) coordinate system, we'll use a Y-up projection
  255. // matrix, and store the conversion to our coordinate system of
  256. // choice in the modelview matrix.
  257. LMatrix4f result =
  258. LMatrix4f::convert_mat(CS_yup_right, _current_lens->get_coordinate_system()) *
  259. lens->get_projection_mat(_current_stereo_channel);
  260. if (_scene_setup->get_inverted()) {
  261. // If the scene is supposed to be inverted, then invert the
  262. // projection matrix.
  263. result *= LMatrix4f::scale_mat(1.0f, -1.0f, 1.0f);
  264. }
  265. return TransformState::make_mat(result);
  266. }
  267. ////////////////////////////////////////////////////////////////////
  268. // Function: TinyGraphicsStateGuardian::prepare_lens
  269. // Access: Public, Virtual
  270. // Description: Makes the current lens (whichever lens was most
  271. // recently specified with set_scene()) active, so
  272. // that it will transform future rendered geometry.
  273. // Normally this is only called from the draw process,
  274. // and usually it is called by set_scene().
  275. //
  276. // The return value is true if the lens is acceptable,
  277. // false if it is not.
  278. ////////////////////////////////////////////////////////////////////
  279. bool TinyGraphicsStateGuardian::
  280. prepare_lens() {
  281. _transform_stale = true;
  282. return true;
  283. }
  284. ////////////////////////////////////////////////////////////////////
  285. // Function: GraphicsStateGuardian::begin_frame
  286. // Access: Public, Virtual
  287. // Description: Called before each frame is rendered, to allow the
  288. // GSG a chance to do any internal cleanup before
  289. // beginning the frame.
  290. //
  291. // The return value is true if successful (in which case
  292. // the frame will be drawn and end_frame() will be
  293. // called later), or false if unsuccessful (in which
  294. // case nothing will be drawn and end_frame() will not
  295. // be called).
  296. ////////////////////////////////////////////////////////////////////
  297. bool TinyGraphicsStateGuardian::
  298. begin_frame(Thread *current_thread) {
  299. if (!GraphicsStateGuardian::begin_frame(current_thread)) {
  300. return false;
  301. }
  302. _c->zb = _current_frame_buffer;
  303. #ifdef DO_PSTATS
  304. _vertices_immediate_pcollector.clear_level();
  305. #endif
  306. return true;
  307. }
  308. ////////////////////////////////////////////////////////////////////
  309. // Function: GraphicsStateGuardian::begin_scene
  310. // Access: Public, Virtual
  311. // Description: Called between begin_frame() and end_frame() to mark
  312. // the beginning of drawing commands for a "scene"
  313. // (usually a particular DisplayRegion) within a frame.
  314. // All 3-D drawing commands, except the clear operation,
  315. // must be enclosed within begin_scene() .. end_scene().
  316. //
  317. // The return value is true if successful (in which case
  318. // the scene will be drawn and end_scene() will be
  319. // called later), or false if unsuccessful (in which
  320. // case nothing will be drawn and end_scene() will not
  321. // be called).
  322. ////////////////////////////////////////////////////////////////////
  323. bool TinyGraphicsStateGuardian::
  324. begin_scene() {
  325. return GraphicsStateGuardian::begin_scene();
  326. }
  327. ////////////////////////////////////////////////////////////////////
  328. // Function: TinyGraphicsStateGuardian::end_scene
  329. // Access: Protected, Virtual
  330. // Description: Called between begin_frame() and end_frame() to mark
  331. // the end of drawing commands for a "scene" (usually a
  332. // particular DisplayRegion) within a frame. All 3-D
  333. // drawing commands, except the clear operation, must be
  334. // enclosed within begin_scene() .. end_scene().
  335. ////////////////////////////////////////////////////////////////////
  336. void TinyGraphicsStateGuardian::
  337. end_scene() {
  338. if (_c->zb == _aux_frame_buffer) {
  339. // Copy the aux frame buffer into the main scene now, zooming it
  340. // up to the appropriate size.
  341. int xmin, ymin, xsize, ysize;
  342. _current_display_region->get_region_pixels_i(xmin, ymin, xsize, ysize);
  343. float pixel_factor = _current_display_region->get_pixel_factor();
  344. int fb_xsize = int(xsize * pixel_factor);
  345. int fb_ysize = int(ysize * pixel_factor);
  346. int tyinc = _current_frame_buffer->linesize / PSZB;
  347. int fyinc = _aux_frame_buffer->linesize / PSZB;
  348. int fyt = 0;
  349. for (int ty = 0; ty < ysize; ++ty) {
  350. int fy = fyt / ysize;
  351. fyt += fb_ysize;
  352. PIXEL *tp = _current_frame_buffer->pbuf + xmin + (ymin + ty) * tyinc;
  353. PIXEL *fp = _aux_frame_buffer->pbuf + fy * fyinc;
  354. ZPOINT *tz = _current_frame_buffer->zbuf + xmin + (ymin + ty) * _current_frame_buffer->xsize;
  355. ZPOINT *fz = _aux_frame_buffer->zbuf + fy * _aux_frame_buffer->xsize;
  356. int fxt = 0;
  357. for (int tx = 0; tx < xsize; ++tx) {
  358. int fx = fxt / xsize;
  359. fxt += fb_xsize;
  360. tp[tx] = fp[fx];
  361. tz[tx] = fz[fx];
  362. }
  363. }
  364. _c->zb = _current_frame_buffer;
  365. }
  366. GraphicsStateGuardian::end_scene();
  367. }
  368. ////////////////////////////////////////////////////////////////////
  369. // Function: TinyGraphicsStateGuardian::end_frame
  370. // Access: Public, Virtual
  371. // Description: Called after each frame is rendered, to allow the
  372. // GSG a chance to do any internal cleanup after
  373. // rendering the frame, and before the window flips.
  374. ////////////////////////////////////////////////////////////////////
  375. void TinyGraphicsStateGuardian::
  376. end_frame(Thread *current_thread) {
  377. GraphicsStateGuardian::end_frame(current_thread);
  378. // Flush any PCollectors specific to this kind of GSG.
  379. _vertices_immediate_pcollector.flush_level();
  380. // Evict any textures that exceed our texture memory.
  381. _textures_lru.begin_epoch();
  382. }
  383. ////////////////////////////////////////////////////////////////////
  384. // Function: TinyGraphicsStateGuardian::begin_draw_primitives
  385. // Access: Public, Virtual
  386. // Description: Called before a sequence of draw_primitive()
  387. // functions are called, this should prepare the vertex
  388. // data for rendering. It returns true if the vertices
  389. // are ok, false to abort this group of primitives.
  390. ////////////////////////////////////////////////////////////////////
  391. bool TinyGraphicsStateGuardian::
  392. begin_draw_primitives(const GeomPipelineReader *geom_reader,
  393. const GeomMunger *munger,
  394. const GeomVertexDataPipelineReader *data_reader,
  395. bool force) {
  396. #ifndef NDEBUG
  397. if (tinydisplay_cat.is_spam()) {
  398. tinydisplay_cat.spam() << "begin_draw_primitives: " << *(data_reader->get_object()) << "\n";
  399. }
  400. #endif // NDEBUG
  401. if (!GraphicsStateGuardian::begin_draw_primitives(geom_reader, munger, data_reader, force)) {
  402. return false;
  403. }
  404. nassertr(_data_reader != (GeomVertexDataPipelineReader *)NULL, false);
  405. PStatTimer timer(_draw_transform_pcollector);
  406. // Set up the proper transform.
  407. if (_data_reader->is_vertex_transformed()) {
  408. // If the vertex data claims to be already transformed into clip
  409. // coordinates, wipe out the current projection and modelview
  410. // matrix (so we don't attempt to transform it again).
  411. const TransformState *ident = TransformState::make_identity();
  412. load_matrix(&_c->matrix_model_view, ident);
  413. load_matrix(&_c->matrix_projection, ident);
  414. load_matrix(&_c->matrix_model_view_inv, ident);
  415. load_matrix(&_c->matrix_model_projection, ident);
  416. _c->matrix_model_projection_no_w_transform = 1;
  417. _transform_stale = true;
  418. } else if (_transform_stale) {
  419. // Load the actual transform.
  420. if (_c->lighting_enabled) {
  421. // With the lighting equation, we need to keep the modelview and
  422. // projection matrices separate.
  423. load_matrix(&_c->matrix_model_view, _internal_transform);
  424. load_matrix(&_c->matrix_projection, _projection_mat);
  425. /* precompute inverse modelview */
  426. M4 tmp;
  427. gl_M4_Inv(&tmp, &_c->matrix_model_view);
  428. gl_M4_Transpose(&_c->matrix_model_view_inv, &tmp);
  429. }
  430. // Compose the modelview and projection matrices.
  431. load_matrix(&_c->matrix_model_projection,
  432. _projection_mat->compose(_internal_transform));
  433. /* test to accelerate computation */
  434. _c->matrix_model_projection_no_w_transform = 0;
  435. float *m = &_c->matrix_model_projection.m[0][0];
  436. if (m[12] == 0.0 && m[13] == 0.0 && m[14] == 0.0) {
  437. _c->matrix_model_projection_no_w_transform = 1;
  438. }
  439. _transform_stale = false;
  440. }
  441. // Figure out the subset of vertices we will be using in this
  442. // operation.
  443. int num_vertices = data_reader->get_num_rows();
  444. _min_vertex = num_vertices;
  445. _max_vertex = 0;
  446. int num_prims = geom_reader->get_num_primitives();
  447. int i;
  448. for (i = 0; i < num_prims; ++i) {
  449. CPT(GeomPrimitive) prim = geom_reader->get_primitive(i);
  450. int nv = prim->get_min_vertex();
  451. _min_vertex = min(_min_vertex, nv);
  452. int xv = prim->get_max_vertex();
  453. _max_vertex = max(_max_vertex, xv);
  454. }
  455. if (_min_vertex > _max_vertex) {
  456. return false;
  457. }
  458. // Now copy all of those vertices into our working table,
  459. // transforming into screen space them as we go.
  460. int num_used_vertices = _max_vertex - _min_vertex + 1;
  461. if (_vertices_size < num_used_vertices) {
  462. if (_vertices_size == 0) {
  463. _vertices_size = 1;
  464. }
  465. while (_vertices_size < num_used_vertices) {
  466. _vertices_size *= 2;
  467. }
  468. if (_vertices != (GLVertex *)NULL) {
  469. PANDA_FREE_ARRAY(_vertices);
  470. }
  471. _vertices = (GLVertex *)PANDA_MALLOC_ARRAY(_vertices_size * sizeof(GLVertex));
  472. }
  473. GeomVertexReader rtexcoord, rcolor, rnormal;
  474. // We only support single-texturing, so only bother with the first
  475. // texture stage.
  476. bool needs_texcoord = false;
  477. bool needs_texmat = false;
  478. LMatrix4f texmat;
  479. const InternalName *texcoord_name = InternalName::get_texcoord();
  480. int max_stage_index = _effective_texture->get_num_on_ff_stages();
  481. if (max_stage_index > 0) {
  482. TextureStage *stage = _effective_texture->get_on_ff_stage(0);
  483. rtexcoord = GeomVertexReader(data_reader, stage->get_texcoord_name(),
  484. force);
  485. rtexcoord.set_row(_min_vertex);
  486. needs_texcoord = rtexcoord.has_column();
  487. if (needs_texcoord && _target._tex_matrix->has_stage(stage)) {
  488. needs_texmat = true;
  489. texmat = _target._tex_matrix->get_mat(stage);
  490. }
  491. }
  492. bool needs_color = false;
  493. if (_vertex_colors_enabled) {
  494. rcolor = GeomVertexReader(data_reader, InternalName::get_color(), force);
  495. rcolor.set_row(_min_vertex);
  496. needs_color = rcolor.has_column();
  497. }
  498. if (!needs_color) {
  499. const Colorf &d = _scene_graph_color;
  500. const Colorf &s = _current_color_scale;
  501. _c->current_color.X = d[0] * s[0];
  502. _c->current_color.Y = d[1] * s[1];
  503. _c->current_color.Z = d[2] * s[2];
  504. _c->current_color.W = d[3] * s[3];
  505. }
  506. bool needs_normal = false;
  507. if (_c->lighting_enabled) {
  508. rnormal = GeomVertexReader(data_reader, InternalName::get_normal(), force);
  509. rnormal.set_row(_min_vertex);
  510. needs_normal = rnormal.has_column();
  511. }
  512. GeomVertexReader rvertex(data_reader, InternalName::get_vertex(), force);
  513. rvertex.set_row(_min_vertex);
  514. if (!rvertex.has_column()) {
  515. // Whoops, guess the vertex data isn't resident.
  516. return false;
  517. }
  518. if (!needs_color && _color_material_flags) {
  519. if (_color_material_flags & CMF_ambient) {
  520. _c->materials[0].ambient = _c->current_color;
  521. _c->materials[1].ambient = _c->current_color;
  522. }
  523. if (_color_material_flags & CMF_diffuse) {
  524. _c->materials[0].diffuse = _c->current_color;
  525. _c->materials[1].diffuse = _c->current_color;
  526. }
  527. }
  528. if (_texturing_state != 0 && _texture_replace) {
  529. // We don't need the vertex color or lighting calculation after
  530. // all, since the current texture will just hide all of that.
  531. needs_color = false;
  532. needs_normal = false;
  533. }
  534. bool lighting_enabled = (needs_normal && _c->lighting_enabled);
  535. for (i = 0; i < num_used_vertices; ++i) {
  536. GLVertex *v = &_vertices[i];
  537. const LVecBase4f &d = rvertex.get_data4f();
  538. v->coord.X = d[0];
  539. v->coord.Y = d[1];
  540. v->coord.Z = d[2];
  541. v->coord.W = d[3];
  542. if (needs_texmat) {
  543. // Transform texcoords as a four-component vector for most generality.
  544. LVecBase4f d = rtexcoord.get_data4f() * texmat;
  545. v->tex_coord.X = d[0];
  546. v->tex_coord.Y = d[1];
  547. } else if (needs_texcoord) {
  548. // No need to transform, so just extract as two-component.
  549. const LVecBase2f &d = rtexcoord.get_data2f();
  550. v->tex_coord.X = d[0];
  551. v->tex_coord.Y = d[1];
  552. }
  553. if (needs_color) {
  554. const Colorf &d = rcolor.get_data4f();
  555. const Colorf &s = _current_color_scale;
  556. _c->current_color.X = d[0] * s[0];
  557. _c->current_color.Y = d[1] * s[1];
  558. _c->current_color.Z = d[2] * s[2];
  559. _c->current_color.W = d[3] * s[3];
  560. if (_color_material_flags) {
  561. if (_color_material_flags & CMF_ambient) {
  562. _c->materials[0].ambient = _c->current_color;
  563. _c->materials[1].ambient = _c->current_color;
  564. }
  565. if (_color_material_flags & CMF_diffuse) {
  566. _c->materials[0].diffuse = _c->current_color;
  567. _c->materials[1].diffuse = _c->current_color;
  568. }
  569. }
  570. }
  571. v->color = _c->current_color;
  572. if (lighting_enabled) {
  573. const LVecBase3f &d = rnormal.get_data3f();
  574. _c->current_normal.X = d[0];
  575. _c->current_normal.Y = d[1];
  576. _c->current_normal.Z = d[2];
  577. _c->current_normal.W = 0.0f;
  578. gl_vertex_transform(_c, v);
  579. gl_shade_vertex(_c, v);
  580. } else {
  581. gl_vertex_transform(_c, v);
  582. }
  583. if (v->clip_code == 0) {
  584. gl_transform_to_viewport(_c, v);
  585. }
  586. v->edge_flag = 1;
  587. }
  588. // Set up the appropriate function callback for filling triangles,
  589. // according to the current state.
  590. int depth_write_state = 0; // zon
  591. if (_target._depth_write->get_mode() != DepthWriteAttrib::M_on) {
  592. depth_write_state = 1; // zoff
  593. }
  594. int color_write_state = 0; // cstore
  595. switch (_target._transparency->get_mode()) {
  596. case TransparencyAttrib::M_alpha:
  597. case TransparencyAttrib::M_dual:
  598. color_write_state = 1; // cblend
  599. break;
  600. default:
  601. break;
  602. }
  603. if (_target._color_blend->get_mode() == ColorBlendAttrib::M_add) {
  604. // If we have a color blend set that we can support, it overrides
  605. // the transparency set.
  606. int op_a = get_color_blend_op(_target._color_blend->get_operand_a());
  607. int op_b = get_color_blend_op(_target._color_blend->get_operand_b());
  608. _c->zb->store_pix_func = store_pixel_funcs[op_a][op_b];
  609. Colorf c = _target._color_blend->get_color();
  610. _c->zb->blend_r = (int)(c[0] * ZB_POINT_RED_MAX);
  611. _c->zb->blend_g = (int)(c[1] * ZB_POINT_GREEN_MAX);
  612. _c->zb->blend_b = (int)(c[2] * ZB_POINT_BLUE_MAX);
  613. _c->zb->blend_a = (int)(c[3] * ZB_POINT_ALPHA_MAX);
  614. color_write_state = 2; // cgeneral
  615. }
  616. unsigned int color_channels =
  617. _target._color_write->get_channels() & _color_write_mask;
  618. if (color_channels == ColorWriteAttrib::C_off) {
  619. color_write_state = 3; // coff
  620. }
  621. int alpha_test_state = 0; // anone
  622. switch (_target._alpha_test->get_mode()) {
  623. case AlphaTestAttrib::M_none:
  624. case AlphaTestAttrib::M_never:
  625. case AlphaTestAttrib::M_always:
  626. case AlphaTestAttrib::M_equal:
  627. case AlphaTestAttrib::M_not_equal:
  628. alpha_test_state = 0; // anone
  629. break;
  630. case AlphaTestAttrib::M_less:
  631. case AlphaTestAttrib::M_less_equal:
  632. alpha_test_state = 1; // aless
  633. _c->zb->reference_alpha = (int)_target._alpha_test->get_reference_alpha() * ZB_POINT_ALPHA_MAX;
  634. break;
  635. case AlphaTestAttrib::M_greater:
  636. case AlphaTestAttrib::M_greater_equal:
  637. alpha_test_state = 2; // amore
  638. _c->zb->reference_alpha = (int)_target._alpha_test->get_reference_alpha() * ZB_POINT_ALPHA_MAX;
  639. break;
  640. }
  641. int depth_test_state = 1; // zless
  642. _c->depth_test = 1; // set this for ZB_line
  643. if (_target._depth_test->get_mode() == DepthTestAttrib::M_none) {
  644. depth_test_state = 0; // zless
  645. _c->depth_test = 0;
  646. }
  647. ShadeModelAttrib::Mode shade_model = _target._shade_model->get_mode();
  648. if (!needs_normal && !needs_color) {
  649. // With no per-vertex lighting, and no per-vertex colors, we might
  650. // as well use the flat shading model.
  651. shade_model = ShadeModelAttrib::M_flat;
  652. }
  653. int shade_model_state = 2; // smooth
  654. _c->smooth_shade_model = true;
  655. if (shade_model == ShadeModelAttrib::M_flat) {
  656. _c->smooth_shade_model = false;
  657. shade_model_state = 1; // flat
  658. if (_c->current_color.X == 1.0f &&
  659. _c->current_color.Y == 1.0f &&
  660. _c->current_color.Z == 1.0f &&
  661. _c->current_color.W == 1.0f) {
  662. shade_model_state = 0; // white
  663. }
  664. }
  665. int texturing_state = _texturing_state;
  666. int texfilter_state = 0; // tnearest
  667. if (texturing_state > 0) {
  668. texfilter_state = _texfilter_state;
  669. if (_c->matrix_model_projection_no_w_transform) {
  670. // Don't bother with the perspective-correct algorithm if we're
  671. // under an orthonormal lens, e.g. render2d.
  672. texturing_state = 1; // textured (not perspective correct)
  673. }
  674. if (_texture_replace) {
  675. // If we're completely replacing the underlying color, then it
  676. // doesn't matter what the color is.
  677. shade_model_state = 0;
  678. }
  679. }
  680. _c->zb_fill_tri = fill_tri_funcs[depth_write_state][color_write_state][alpha_test_state][depth_test_state][texfilter_state][shade_model_state][texturing_state];
  681. return true;
  682. }
  683. ////////////////////////////////////////////////////////////////////
  684. // Function: TinyGraphicsStateGuardian::draw_triangles
  685. // Access: Public, Virtual
  686. // Description: Draws a series of disconnected triangles.
  687. ////////////////////////////////////////////////////////////////////
  688. bool TinyGraphicsStateGuardian::
  689. draw_triangles(const GeomPrimitivePipelineReader *reader, bool force) {
  690. PStatTimer timer(_draw_primitive_pcollector, reader->get_current_thread());
  691. #ifndef NDEBUG
  692. if (tinydisplay_cat.is_spam()) {
  693. tinydisplay_cat.spam() << "draw_triangles: " << *(reader->get_object()) << "\n";
  694. }
  695. #endif // NDEBUG
  696. int num_vertices = reader->get_num_vertices();
  697. _vertices_immediate_pcollector.add_level(num_vertices);
  698. if (reader->is_indexed()) {
  699. switch (reader->get_index_type()) {
  700. case Geom::NT_uint8:
  701. {
  702. PN_uint8 *index = (PN_uint8 *)reader->get_read_pointer(true);
  703. for (int i = 0; i < num_vertices; i += 3) {
  704. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  705. GLVertex *v1 = &_vertices[index[i + 1] - _min_vertex];
  706. GLVertex *v2 = &_vertices[index[i + 2] - _min_vertex];
  707. gl_draw_triangle(_c, v0, v1, v2);
  708. }
  709. }
  710. break;
  711. case Geom::NT_uint16:
  712. {
  713. PN_uint16 *index = (PN_uint16 *)reader->get_read_pointer(true);
  714. for (int i = 0; i < num_vertices; i += 3) {
  715. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  716. GLVertex *v1 = &_vertices[index[i + 1] - _min_vertex];
  717. GLVertex *v2 = &_vertices[index[i + 2] - _min_vertex];
  718. gl_draw_triangle(_c, v0, v1, v2);
  719. }
  720. }
  721. break;
  722. case Geom::NT_uint32:
  723. {
  724. PN_uint32 *index = (PN_uint32 *)reader->get_read_pointer(true);
  725. for (int i = 0; i < num_vertices; i += 3) {
  726. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  727. GLVertex *v1 = &_vertices[index[i + 1] - _min_vertex];
  728. GLVertex *v2 = &_vertices[index[i + 2] - _min_vertex];
  729. gl_draw_triangle(_c, v0, v1, v2);
  730. }
  731. }
  732. break;
  733. default:
  734. break;
  735. }
  736. } else {
  737. int delta = reader->get_first_vertex() - _min_vertex;
  738. for (int vi = 0; vi < num_vertices; vi += 3) {
  739. GLVertex *v0 = &_vertices[vi + delta];
  740. GLVertex *v1 = &_vertices[vi + delta + 1];
  741. GLVertex *v2 = &_vertices[vi + delta + 2];
  742. gl_draw_triangle(_c, v0, v1, v2);
  743. }
  744. }
  745. return true;
  746. }
  747. ////////////////////////////////////////////////////////////////////
  748. // Function: TinyGraphicsStateGuardian::draw_lines
  749. // Access: Public, Virtual
  750. // Description: Draws a series of disconnected line segments.
  751. ////////////////////////////////////////////////////////////////////
  752. bool TinyGraphicsStateGuardian::
  753. draw_lines(const GeomPrimitivePipelineReader *reader, bool force) {
  754. PStatTimer timer(_draw_primitive_pcollector, reader->get_current_thread());
  755. #ifndef NDEBUG
  756. if (tinydisplay_cat.is_spam()) {
  757. tinydisplay_cat.spam() << "draw_lines: " << *(reader->get_object()) << "\n";
  758. }
  759. #endif // NDEBUG
  760. int num_vertices = reader->get_num_vertices();
  761. _vertices_immediate_pcollector.add_level(num_vertices);
  762. if (reader->is_indexed()) {
  763. switch (reader->get_index_type()) {
  764. case Geom::NT_uint8:
  765. {
  766. PN_uint8 *index = (PN_uint8 *)reader->get_read_pointer(true);
  767. for (int i = 0; i < num_vertices; i += 2) {
  768. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  769. GLVertex *v1 = &_vertices[index[i + 1] - _min_vertex];
  770. gl_draw_line(_c, v0, v1);
  771. }
  772. }
  773. break;
  774. case Geom::NT_uint16:
  775. {
  776. PN_uint16 *index = (PN_uint16 *)reader->get_read_pointer(true);
  777. for (int i = 0; i < num_vertices; i += 2) {
  778. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  779. GLVertex *v1 = &_vertices[index[i + 1] - _min_vertex];
  780. gl_draw_line(_c, v0, v1);
  781. }
  782. }
  783. break;
  784. case Geom::NT_uint32:
  785. {
  786. PN_uint32 *index = (PN_uint32 *)reader->get_read_pointer(true);
  787. for (int i = 0; i < num_vertices; i += 2) {
  788. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  789. GLVertex *v1 = &_vertices[index[i + 1] - _min_vertex];
  790. gl_draw_line(_c, v0, v1);
  791. }
  792. }
  793. break;
  794. default:
  795. break;
  796. }
  797. } else {
  798. int delta = reader->get_first_vertex() - _min_vertex;
  799. for (int vi = 0; vi < num_vertices; vi += 2) {
  800. GLVertex *v0 = &_vertices[vi + delta];
  801. GLVertex *v1 = &_vertices[vi + delta + 1];
  802. gl_draw_line(_c, v0, v1);
  803. }
  804. }
  805. return true;
  806. }
  807. ////////////////////////////////////////////////////////////////////
  808. // Function: TinyGraphicsStateGuardian::draw_points
  809. // Access: Public, Virtual
  810. // Description: Draws a series of disconnected points.
  811. ////////////////////////////////////////////////////////////////////
  812. bool TinyGraphicsStateGuardian::
  813. draw_points(const GeomPrimitivePipelineReader *reader, bool force) {
  814. PStatTimer timer(_draw_primitive_pcollector, reader->get_current_thread());
  815. #ifndef NDEBUG
  816. if (tinydisplay_cat.is_spam()) {
  817. tinydisplay_cat.spam() << "draw_points: " << *(reader->get_object()) << "\n";
  818. }
  819. #endif // NDEBUG
  820. int num_vertices = reader->get_num_vertices();
  821. _vertices_immediate_pcollector.add_level(num_vertices);
  822. if (reader->is_indexed()) {
  823. switch (reader->get_index_type()) {
  824. case Geom::NT_uint8:
  825. {
  826. PN_uint8 *index = (PN_uint8 *)reader->get_read_pointer(true);
  827. for (int i = 0; i < num_vertices; ++i) {
  828. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  829. gl_draw_point(_c, v0);
  830. }
  831. }
  832. break;
  833. case Geom::NT_uint16:
  834. {
  835. PN_uint16 *index = (PN_uint16 *)reader->get_read_pointer(true);
  836. for (int i = 0; i < num_vertices; ++i) {
  837. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  838. gl_draw_point(_c, v0);
  839. }
  840. }
  841. break;
  842. case Geom::NT_uint32:
  843. {
  844. PN_uint32 *index = (PN_uint32 *)reader->get_read_pointer(true);
  845. for (int i = 0; i < num_vertices; ++i) {
  846. GLVertex *v0 = &_vertices[index[i] - _min_vertex];
  847. gl_draw_point(_c, v0);
  848. }
  849. }
  850. break;
  851. default:
  852. break;
  853. }
  854. } else {
  855. int delta = reader->get_first_vertex() - _min_vertex;
  856. for (int vi = 0; vi < num_vertices; ++vi) {
  857. GLVertex *v0 = &_vertices[vi + delta];
  858. gl_draw_point(_c, v0);
  859. }
  860. }
  861. return true;
  862. }
  863. ////////////////////////////////////////////////////////////////////
  864. // Function: TinyGraphicsStateGuardian::end_draw_primitives()
  865. // Access: Public, Virtual
  866. // Description: Called after a sequence of draw_primitive()
  867. // functions are called, this should do whatever cleanup
  868. // is appropriate.
  869. ////////////////////////////////////////////////////////////////////
  870. void TinyGraphicsStateGuardian::
  871. end_draw_primitives() {
  872. GraphicsStateGuardian::end_draw_primitives();
  873. }
  874. ////////////////////////////////////////////////////////////////////
  875. // Function: TinyGraphicsStateGuardian::framebuffer_copy_to_texture
  876. // Access: Public, Virtual
  877. // Description: Copy the pixels within the indicated display
  878. // region from the framebuffer into texture memory.
  879. //
  880. // If z > -1, it is the cube map index into which to
  881. // copy.
  882. ////////////////////////////////////////////////////////////////////
  883. void TinyGraphicsStateGuardian::
  884. framebuffer_copy_to_texture(Texture *tex, int z, const DisplayRegion *dr,
  885. const RenderBuffer &rb) {
  886. nassertv(tex != NULL && dr != NULL);
  887. int xo, yo, w, h;
  888. dr->get_region_pixels_i(xo, yo, w, h);
  889. tex->setup_2d_texture(w, h, Texture::T_unsigned_byte, Texture::F_rgba);
  890. TextureContext *tc = tex->prepare_now(get_prepared_objects(), this);
  891. nassertv(tc != (TextureContext *)NULL);
  892. TinyTextureContext *gtc = DCAST(TinyTextureContext, tc);
  893. GLTexture *gltex = gtc->_gltex;
  894. if (!setup_gltex(gltex, tex->get_x_size(), tex->get_y_size(), 1)) {
  895. return;
  896. }
  897. PIXEL *ip = gltex->levels[0].pixmap + gltex->xsize * gltex->ysize;
  898. PIXEL *fo = _c->zb->pbuf + xo + yo * _c->zb->linesize / PSZB;
  899. for (int y = 0; y < gltex->ysize; ++y) {
  900. ip -= gltex->xsize;
  901. memcpy(ip, fo, gltex->xsize * PSZB);
  902. fo += _c->zb->linesize / PSZB;
  903. }
  904. gtc->update_data_size_bytes(gltex->xsize * gltex->ysize * 4);
  905. gtc->mark_loaded();
  906. gtc->enqueue_lru(&_textures_lru);
  907. }
  908. ////////////////////////////////////////////////////////////////////
  909. // Function: TinyGraphicsStateGuardian::framebuffer_copy_to_ram
  910. // Access: Public, Virtual
  911. // Description: Copy the pixels within the indicated display region
  912. // from the framebuffer into system memory, not texture
  913. // memory. Returns true on success, false on failure.
  914. //
  915. // This completely redefines the ram image of the
  916. // indicated texture.
  917. ////////////////////////////////////////////////////////////////////
  918. bool TinyGraphicsStateGuardian::
  919. framebuffer_copy_to_ram(Texture *tex, int z, const DisplayRegion *dr,
  920. const RenderBuffer &rb) {
  921. nassertr(tex != NULL && dr != NULL, false);
  922. int xo, yo, w, h;
  923. dr->get_region_pixels_i(xo, yo, w, h);
  924. Texture::TextureType texture_type;
  925. int z_size;
  926. if (z >= 0) {
  927. texture_type = Texture::TT_cube_map;
  928. z_size = 6;
  929. } else {
  930. texture_type = Texture::TT_2d_texture;
  931. z_size = 1;
  932. }
  933. Texture::ComponentType component_type = Texture::T_unsigned_byte;
  934. Texture::Format format = Texture::F_rgba;
  935. if (tex->get_x_size() != w || tex->get_y_size() != h ||
  936. tex->get_z_size() != z_size ||
  937. tex->get_component_type() != component_type ||
  938. tex->get_format() != format ||
  939. tex->get_texture_type() != texture_type) {
  940. // Re-setup the texture; its properties have changed.
  941. tex->setup_texture(texture_type, w, h, z_size,
  942. component_type, format);
  943. }
  944. unsigned char *image_ptr = tex->modify_ram_image();
  945. size_t image_size = tex->get_ram_image_size();
  946. if (z >= 0) {
  947. nassertr(z < tex->get_z_size(), false);
  948. image_size = tex->get_expected_ram_page_size();
  949. image_ptr += z * image_size;
  950. }
  951. PIXEL *ip = (PIXEL *)(image_ptr + image_size);
  952. PIXEL *fo = _c->zb->pbuf + xo + yo * _c->zb->linesize / PSZB;
  953. for (int y = 0; y < h; ++y) {
  954. ip -= w;
  955. memcpy(ip, fo, w * PSZB);
  956. fo += _c->zb->linesize / PSZB;
  957. }
  958. return true;
  959. }
  960. ////////////////////////////////////////////////////////////////////
  961. // Function: TinyGraphicsStateGuardian::set_state_and_transform
  962. // Access: Public, Virtual
  963. // Description: Simultaneously resets the render state and the
  964. // transform state.
  965. //
  966. // This transform specified is the "internal" net
  967. // transform, already converted into the GSG's internal
  968. // coordinate space by composing it to
  969. // get_cs_transform(). (Previously, this used to be the
  970. // "external" net transform, with the assumption that
  971. // that GSG would convert it internally, but that is no
  972. // longer the case.)
  973. //
  974. // Special case: if (state==NULL), then the target
  975. // state is already stored in _target.
  976. ////////////////////////////////////////////////////////////////////
  977. void TinyGraphicsStateGuardian::
  978. set_state_and_transform(const RenderState *target,
  979. const TransformState *transform) {
  980. #ifndef NDEBUG
  981. if (tinydisplay_cat.is_spam()) {
  982. tinydisplay_cat.spam()
  983. << "Setting GSG state to " << (void *)target << ":\n";
  984. target->write(tinydisplay_cat.spam(false), 2);
  985. }
  986. #endif
  987. _state_pcollector.add_level(1);
  988. PStatTimer timer1(_draw_set_state_pcollector);
  989. if (transform != _internal_transform) {
  990. PStatTimer timer(_draw_set_state_transform_pcollector);
  991. _state_pcollector.add_level(1);
  992. _internal_transform = transform;
  993. do_issue_transform();
  994. }
  995. if (target == _state_rs) {
  996. return;
  997. }
  998. _target_rs = target;
  999. _target.clear_to_defaults();
  1000. target->store_into_slots(&_target);
  1001. _state_rs = 0;
  1002. if (_target._color != _state._color ||
  1003. _target._color_scale != _state._color_scale) {
  1004. PStatTimer timer(_draw_set_state_color_pcollector);
  1005. do_issue_color();
  1006. do_issue_color_scale();
  1007. _state._color = _target._color;
  1008. _state._color_scale = _target._color_scale;
  1009. }
  1010. if (_target._cull_face != _state._cull_face) {
  1011. PStatTimer timer(_draw_set_state_cull_face_pcollector);
  1012. do_issue_cull_face();
  1013. _state._cull_face = _target._cull_face;
  1014. }
  1015. if (_target._render_mode != _state._render_mode) {
  1016. PStatTimer timer(_draw_set_state_render_mode_pcollector);
  1017. do_issue_render_mode();
  1018. _state._render_mode = _target._render_mode;
  1019. }
  1020. if (_target._texture != _state._texture) {
  1021. PStatTimer timer(_draw_set_state_texture_pcollector);
  1022. determine_effective_texture();
  1023. do_issue_texture();
  1024. _state._texture = _target._texture;
  1025. }
  1026. if (_target._material != _state._material) {
  1027. PStatTimer timer(_draw_set_state_material_pcollector);
  1028. do_issue_material();
  1029. _state._material = _target._material;
  1030. }
  1031. if (_target._light != _state._light) {
  1032. PStatTimer timer(_draw_set_state_light_pcollector);
  1033. do_issue_light();
  1034. _state._light = _target._light;
  1035. }
  1036. _state_rs = _target_rs;
  1037. }
  1038. ////////////////////////////////////////////////////////////////////
  1039. // Function: TinyGraphicsStateGuardian::prepare_texture
  1040. // Access: Public, Virtual
  1041. // Description: Creates whatever structures the GSG requires to
  1042. // represent the texture internally, and returns a
  1043. // newly-allocated TextureContext object with this data.
  1044. // It is the responsibility of the calling function to
  1045. // later call release_texture() with this same pointer
  1046. // (which will also delete the pointer).
  1047. //
  1048. // This function should not be called directly to
  1049. // prepare a texture. Instead, call Texture::prepare().
  1050. ////////////////////////////////////////////////////////////////////
  1051. TextureContext *TinyGraphicsStateGuardian::
  1052. prepare_texture(Texture *tex) {
  1053. if (tex->get_texture_type() != Texture::TT_2d_texture) {
  1054. tinydisplay_cat.info()
  1055. << "not loading texture " << tex->get_name() << ": "
  1056. << tex->get_texture_type() << "\n";
  1057. return NULL;
  1058. }
  1059. if (tex->get_ram_image_compression() != Texture::CM_off) {
  1060. tinydisplay_cat.info()
  1061. << "not loading texture " << tex->get_name() << ": "
  1062. << tex->get_ram_image_compression() << "\n";
  1063. return NULL;
  1064. }
  1065. TinyTextureContext *gtc = new TinyTextureContext(_prepared_objects, tex);
  1066. gtc->_gltex = (GLTexture *)gl_zalloc(sizeof(GLTexture));
  1067. return gtc;
  1068. }
  1069. ////////////////////////////////////////////////////////////////////
  1070. // Function: TinyGraphicsStateGuardian::release_texture
  1071. // Access: Public, Virtual
  1072. // Description: Frees the GL resources previously allocated for the
  1073. // texture. This function should never be called
  1074. // directly; instead, call Texture::release() (or simply
  1075. // let the Texture destruct).
  1076. ////////////////////////////////////////////////////////////////////
  1077. void TinyGraphicsStateGuardian::
  1078. release_texture(TextureContext *tc) {
  1079. TinyTextureContext *gtc = DCAST(TinyTextureContext, tc);
  1080. GLTexture *gltex = gtc->_gltex;
  1081. gtc->_gltex = NULL;
  1082. _texturing_state = 0; // just in case
  1083. for (int i = 0; i < gltex->num_levels; ++i) {
  1084. gl_free(gltex->levels[i].pixmap);
  1085. }
  1086. if (gltex->levels != NULL) {
  1087. gl_free(gltex->levels);
  1088. gltex->levels = NULL;
  1089. }
  1090. gl_free(gltex);
  1091. gtc->dequeue_lru();
  1092. delete gtc;
  1093. }
  1094. ////////////////////////////////////////////////////////////////////
  1095. // Function: TinyGraphicsStateGuardian::do_issue_light
  1096. // Access: Protected, Virtual
  1097. // Description:
  1098. ////////////////////////////////////////////////////////////////////
  1099. void TinyGraphicsStateGuardian::
  1100. do_issue_light() {
  1101. // Initialize the current ambient light total and newly enabled
  1102. // light list
  1103. Colorf cur_ambient_light(0.0f, 0.0f, 0.0f, 0.0f);
  1104. int num_enabled = 0;
  1105. int num_on_lights = 0;
  1106. if (display_cat.is_spam()) {
  1107. display_cat.spam()
  1108. << "do_issue_light: " << _target._light << "\n";
  1109. }
  1110. // First, release all of the previously-assigned lights.
  1111. _c->lighting_enabled = false;
  1112. GLLight *gl_light = _c->first_light;
  1113. while (gl_light != (GLLight *)NULL) {
  1114. GLLight *next = gl_light->next;
  1115. gl_light->next = NULL;
  1116. gl_light = next;
  1117. }
  1118. _c->first_light = NULL;
  1119. // Now, assign new lights.
  1120. if (_target._light != (LightAttrib *)NULL) {
  1121. CPT(LightAttrib) new_light = _target._light->filter_to_max(_max_lights);
  1122. if (display_cat.is_spam()) {
  1123. new_light->write(display_cat.spam(false), 2);
  1124. }
  1125. num_on_lights = new_light->get_num_on_lights();
  1126. for (int li = 0; li < num_on_lights; li++) {
  1127. NodePath light = new_light->get_on_light(li);
  1128. nassertv(!light.is_empty());
  1129. Light *light_obj = light.node()->as_light();
  1130. nassertv(light_obj != (Light *)NULL);
  1131. _lighting_enabled = true;
  1132. _c->lighting_enabled = true;
  1133. if (light_obj->get_type() == AmbientLight::get_class_type()) {
  1134. // Accumulate all of the ambient lights together into one.
  1135. cur_ambient_light += light_obj->get_color();
  1136. } else {
  1137. // Other kinds of lights each get their own GLLight object.
  1138. nassertv(num_enabled < MAX_LIGHTS);
  1139. GLLight *gl_light = &_c->lights[num_enabled];
  1140. memset(gl_light, 0, sizeof(GLLight));
  1141. gl_light->next = _c->first_light;
  1142. _c->first_light = gl_light;
  1143. const Colorf &diffuse = light_obj->get_color();
  1144. gl_light->diffuse.X = diffuse[0];
  1145. gl_light->diffuse.Y = diffuse[1];
  1146. gl_light->diffuse.Z = diffuse[2];
  1147. gl_light->diffuse.W = diffuse[3];
  1148. light_obj->bind(this, light, num_enabled);
  1149. num_enabled++;
  1150. }
  1151. }
  1152. }
  1153. _c->ambient_light_model.X = cur_ambient_light[0];
  1154. _c->ambient_light_model.Y = cur_ambient_light[1];
  1155. _c->ambient_light_model.Z = cur_ambient_light[2];
  1156. _c->ambient_light_model.W = cur_ambient_light[3];
  1157. }
  1158. ////////////////////////////////////////////////////////////////////
  1159. // Function: TinyGraphicsStateGuardian::bind_light
  1160. // Access: Public, Virtual
  1161. // Description: Called the first time a particular light has been
  1162. // bound to a given id within a frame, this should set
  1163. // up the associated hardware light with the light's
  1164. // properties.
  1165. ////////////////////////////////////////////////////////////////////
  1166. void TinyGraphicsStateGuardian::
  1167. bind_light(PointLight *light_obj, const NodePath &light, int light_id) {
  1168. GLLight *gl_light = _c->first_light;
  1169. nassertv(gl_light != (GLLight *)NULL);
  1170. const Colorf &specular = light_obj->get_specular_color();
  1171. gl_light->specular.X = specular[0];
  1172. gl_light->specular.Y = specular[1];
  1173. gl_light->specular.Z = specular[2];
  1174. gl_light->specular.W = specular[3];
  1175. // Position needs to specify x, y, z, and w
  1176. // w == 1 implies non-infinite position
  1177. CPT(TransformState) render_transform =
  1178. _cs_transform->compose(_scene_setup->get_world_transform());
  1179. CPT(TransformState) transform = light.get_transform(_scene_setup->get_scene_root().get_parent());
  1180. CPT(TransformState) net_transform = render_transform->compose(transform);
  1181. LPoint3f pos = light_obj->get_point() * net_transform->get_mat();
  1182. gl_light->position.X = pos[0];
  1183. gl_light->position.Y = pos[1];
  1184. gl_light->position.Z = pos[2];
  1185. gl_light->position.W = 1.0f;
  1186. // Exponent == 0 implies uniform light distribution
  1187. gl_light->spot_exponent = 0.0f;
  1188. // Cutoff == 180 means uniform point light source
  1189. gl_light->spot_cutoff = 180.0f;
  1190. const LVecBase3f &att = light_obj->get_attenuation();
  1191. gl_light->attenuation[0] = att[0];
  1192. gl_light->attenuation[1] = att[1];
  1193. gl_light->attenuation[2] = att[2];
  1194. }
  1195. ////////////////////////////////////////////////////////////////////
  1196. // Function: TinyGraphicsStateGuardian::bind_light
  1197. // Access: Public, Virtual
  1198. // Description: Called the first time a particular light has been
  1199. // bound to a given id within a frame, this should set
  1200. // up the associated hardware light with the light's
  1201. // properties.
  1202. ////////////////////////////////////////////////////////////////////
  1203. void TinyGraphicsStateGuardian::
  1204. bind_light(DirectionalLight *light_obj, const NodePath &light, int light_id) {
  1205. GLLight *gl_light = _c->first_light;
  1206. nassertv(gl_light != (GLLight *)NULL);
  1207. const Colorf &specular = light_obj->get_specular_color();
  1208. gl_light->specular.X = specular[0];
  1209. gl_light->specular.Y = specular[1];
  1210. gl_light->specular.Z = specular[2];
  1211. gl_light->specular.W = specular[3];
  1212. // Position needs to specify x, y, z, and w
  1213. // w == 0 implies light is at infinity
  1214. CPT(TransformState) render_transform =
  1215. _cs_transform->compose(_scene_setup->get_world_transform());
  1216. CPT(TransformState) transform = light.get_transform(_scene_setup->get_scene_root().get_parent());
  1217. CPT(TransformState) net_transform = render_transform->compose(transform);
  1218. LVector3f dir = light_obj->get_direction() * net_transform->get_mat();
  1219. gl_light->position.X = -dir[0];
  1220. gl_light->position.Y = -dir[1];
  1221. gl_light->position.Z = -dir[2];
  1222. gl_light->position.W = 0.0f;
  1223. gl_light->norm_position.X = -dir[0];
  1224. gl_light->norm_position.Y = -dir[1];
  1225. gl_light->norm_position.Z = -dir[2];
  1226. gl_V3_Norm(&gl_light->norm_position);
  1227. // Exponent == 0 implies uniform light distribution
  1228. gl_light->spot_exponent = 0.0f;
  1229. // Cutoff == 180 means uniform point light source
  1230. gl_light->spot_cutoff = 180.0f;
  1231. // Default attenuation values (only spotlight and point light can
  1232. // modify these)
  1233. gl_light->attenuation[0] = 1.0f;
  1234. gl_light->attenuation[1] = 0.0f;
  1235. gl_light->attenuation[2] = 0.0f;
  1236. }
  1237. ////////////////////////////////////////////////////////////////////
  1238. // Function: TinyGraphicsStateGuardian::bind_light
  1239. // Access: Public, Virtual
  1240. // Description: Called the first time a particular light has been
  1241. // bound to a given id within a frame, this should set
  1242. // up the associated hardware light with the light's
  1243. // properties.
  1244. ////////////////////////////////////////////////////////////////////
  1245. void TinyGraphicsStateGuardian::
  1246. bind_light(Spotlight *light_obj, const NodePath &light, int light_id) {
  1247. GLLight *gl_light = _c->first_light;
  1248. nassertv(gl_light != (GLLight *)NULL);
  1249. const Colorf &specular = light_obj->get_specular_color();
  1250. gl_light->specular.X = specular[0];
  1251. gl_light->specular.Y = specular[1];
  1252. gl_light->specular.Z = specular[2];
  1253. gl_light->specular.W = specular[3];
  1254. Lens *lens = light_obj->get_lens();
  1255. nassertv(lens != (Lens *)NULL);
  1256. // Position needs to specify x, y, z, and w
  1257. // w == 1 implies non-infinite position
  1258. CPT(TransformState) render_transform =
  1259. _cs_transform->compose(_scene_setup->get_world_transform());
  1260. CPT(TransformState) transform = light.get_transform(_scene_setup->get_scene_root().get_parent());
  1261. CPT(TransformState) net_transform = render_transform->compose(transform);
  1262. const LMatrix4f &light_mat = net_transform->get_mat();
  1263. LPoint3f pos = lens->get_nodal_point() * light_mat;
  1264. LVector3f dir = lens->get_view_vector() * light_mat;
  1265. gl_light->position.X = pos[0];
  1266. gl_light->position.Y = pos[1];
  1267. gl_light->position.Z = pos[2];
  1268. gl_light->position.W = 1.0f;
  1269. gl_light->spot_direction.X = dir[0];
  1270. gl_light->spot_direction.Y = dir[1];
  1271. gl_light->spot_direction.Z = dir[2];
  1272. gl_light->norm_spot_direction.X = dir[0];
  1273. gl_light->norm_spot_direction.Y = dir[1];
  1274. gl_light->norm_spot_direction.Z = dir[2];
  1275. gl_V3_Norm(&gl_light->norm_spot_direction);
  1276. gl_light->spot_exponent = light_obj->get_exponent();
  1277. gl_light->spot_cutoff = lens->get_hfov() * 0.5f;
  1278. const LVecBase3f &att = light_obj->get_attenuation();
  1279. gl_light->attenuation[0] = att[0];
  1280. gl_light->attenuation[1] = att[1];
  1281. gl_light->attenuation[2] = att[2];
  1282. }
  1283. ////////////////////////////////////////////////////////////////////
  1284. // Function: TinyGraphicsStateGuardian::do_issue_transform
  1285. // Access: Protected
  1286. // Description: Sends the indicated transform matrix to the graphics
  1287. // API to be applied to future vertices.
  1288. //
  1289. // This transform is the internal_transform, already
  1290. // converted into the GSG's internal coordinate system.
  1291. ////////////////////////////////////////////////////////////////////
  1292. void TinyGraphicsStateGuardian::
  1293. do_issue_transform() {
  1294. _transform_state_pcollector.add_level(1);
  1295. _transform_stale = true;
  1296. }
  1297. ////////////////////////////////////////////////////////////////////
  1298. // Function: TinyGraphicsStateGuardian::do_issue_render_mode
  1299. // Access: Protected
  1300. // Description:
  1301. ////////////////////////////////////////////////////////////////////
  1302. void TinyGraphicsStateGuardian::
  1303. do_issue_render_mode() {
  1304. const RenderModeAttrib *attrib = _target._render_mode;
  1305. switch (attrib->get_mode()) {
  1306. case RenderModeAttrib::M_unchanged:
  1307. case RenderModeAttrib::M_filled:
  1308. _c->draw_triangle_front = gl_draw_triangle_fill;
  1309. _c->draw_triangle_back = gl_draw_triangle_fill;
  1310. break;
  1311. case RenderModeAttrib::M_wireframe:
  1312. _c->draw_triangle_front = gl_draw_triangle_line;
  1313. _c->draw_triangle_back = gl_draw_triangle_line;
  1314. break;
  1315. case RenderModeAttrib::M_point:
  1316. _c->draw_triangle_front = gl_draw_triangle_point;
  1317. _c->draw_triangle_back = gl_draw_triangle_point;
  1318. break;
  1319. default:
  1320. tinydisplay_cat.error()
  1321. << "Unknown render mode " << (int)attrib->get_mode() << endl;
  1322. }
  1323. }
  1324. ////////////////////////////////////////////////////////////////////
  1325. // Function: TinyGraphicsStateGuardian::do_issue_cull_face
  1326. // Access: Protected
  1327. // Description:
  1328. ////////////////////////////////////////////////////////////////////
  1329. void TinyGraphicsStateGuardian::
  1330. do_issue_cull_face() {
  1331. const CullFaceAttrib *attrib = _target._cull_face;
  1332. CullFaceAttrib::Mode mode = attrib->get_effective_mode();
  1333. switch (mode) {
  1334. case CullFaceAttrib::M_cull_none:
  1335. _c->cull_face_enabled = false;
  1336. break;
  1337. case CullFaceAttrib::M_cull_clockwise:
  1338. _c->cull_face_enabled = true;
  1339. _c->cull_clockwise = true;
  1340. break;
  1341. case CullFaceAttrib::M_cull_counter_clockwise:
  1342. _c->cull_face_enabled = true;
  1343. _c->cull_clockwise = false;
  1344. break;
  1345. default:
  1346. tinydisplay_cat.error()
  1347. << "invalid cull face mode " << (int)mode << endl;
  1348. break;
  1349. }
  1350. }
  1351. ////////////////////////////////////////////////////////////////////
  1352. // Function: TinyGraphicsStateGuardian::do_issue_material
  1353. // Access: Protected
  1354. // Description:
  1355. ////////////////////////////////////////////////////////////////////
  1356. void TinyGraphicsStateGuardian::
  1357. do_issue_material() {
  1358. static Material empty;
  1359. const Material *material;
  1360. if (_target._material == (MaterialAttrib *)NULL ||
  1361. _target._material->is_off()) {
  1362. material = &empty;
  1363. } else {
  1364. material = _target._material->get_material();
  1365. }
  1366. // Apply the material parameters to the front face.
  1367. setup_material(&_c->materials[0], material);
  1368. if (material->get_twoside()) {
  1369. // Also apply the material parameters to the back face.
  1370. setup_material(&_c->materials[1], material);
  1371. }
  1372. _c->local_light_model = material->get_local();
  1373. _c->light_model_two_side = material->get_twoside();
  1374. }
  1375. ////////////////////////////////////////////////////////////////////
  1376. // Function: TinyGraphicsStateGuardian::do_issue_texture
  1377. // Access: Protected
  1378. // Description:
  1379. ////////////////////////////////////////////////////////////////////
  1380. void TinyGraphicsStateGuardian::
  1381. do_issue_texture() {
  1382. _texturing_state = 0; // untextured
  1383. _c->texture_2d_enabled = false;
  1384. int num_stages = _effective_texture->get_num_on_ff_stages();
  1385. if (num_stages == 0) {
  1386. // No texturing.
  1387. return;
  1388. }
  1389. nassertv(num_stages == 1);
  1390. TextureStage *stage = _effective_texture->get_on_ff_stage(0);
  1391. Texture *texture = _effective_texture->get_on_texture(stage);
  1392. nassertv(texture != (Texture *)NULL);
  1393. TextureContext *tc = texture->prepare_now(_prepared_objects, this);
  1394. if (tc == (TextureContext *)NULL) {
  1395. // Something wrong with this texture; skip it.
  1396. return;
  1397. }
  1398. // Then, turn on the current texture mode.
  1399. if (!apply_texture(tc)) {
  1400. return;
  1401. }
  1402. // Set a few state cache values.
  1403. _c->texture_2d_enabled = true;
  1404. _texturing_state = 2; // perspective (perspective-correct texturing)
  1405. if (!td_perspective_textures) {
  1406. _texturing_state = 1; // textured (not perspective correct)
  1407. }
  1408. Texture::QualityLevel quality_level = _texture_quality_override;
  1409. if (quality_level == Texture::QL_default) {
  1410. quality_level = texture->get_quality_level();
  1411. if (quality_level == Texture::QL_default) {
  1412. quality_level = texture_quality_level;
  1413. }
  1414. }
  1415. if (quality_level == Texture::QL_best) {
  1416. // This is the most generic texture filter. Slow, but pretty.
  1417. _texfilter_state = 2; // tgeneral
  1418. _c->zb->tex_minfilter_func = get_tex_filter_func(texture->get_minfilter());
  1419. _c->zb->tex_magfilter_func = get_tex_filter_func(texture->get_magfilter());
  1420. if (texture->get_minfilter() == Texture::FT_nearest &&
  1421. texture->get_magfilter() == Texture::FT_nearest) {
  1422. // This case is inlined.
  1423. _texfilter_state = 0; // tnearest
  1424. } else if (texture->get_minfilter() == Texture::FT_nearest_mipmap_nearest &&
  1425. texture->get_magfilter() == Texture::FT_nearest) {
  1426. // So is this case.
  1427. _texfilter_state = 1; // tmipmap
  1428. }
  1429. } else if (quality_level == Texture::QL_fastest) {
  1430. // This is the cheapest texture filter. We disallow mipmaps and
  1431. // perspective correctness.
  1432. _texfilter_state = 0; // tnearest
  1433. _texturing_state = 1; // textured (not perspective correct)
  1434. } else {
  1435. // This is the default texture filter. We use nearest sampling if
  1436. // there are no mipmaps, and mipmap_nearest if there are any
  1437. // mipmaps--these are the two inlined filters.
  1438. _texfilter_state = 0; // tnearest
  1439. if (texture->uses_mipmaps() && !td_ignore_mipmaps) {
  1440. _texfilter_state = 1; // tmipmap
  1441. }
  1442. }
  1443. // M_replace means M_replace; anything else is treated the same as
  1444. // M_modulate.
  1445. _texture_replace = (stage->get_mode() == TextureStage::M_replace);
  1446. }
  1447. ////////////////////////////////////////////////////////////////////
  1448. // Function: TinyGraphicsStateGuardian::apply_texture
  1449. // Access: Protected
  1450. // Description: Updates TinyGL with the current information for this
  1451. // texture, and makes it the current texture available
  1452. // for rendering.
  1453. ////////////////////////////////////////////////////////////////////
  1454. bool TinyGraphicsStateGuardian::
  1455. apply_texture(TextureContext *tc) {
  1456. TinyTextureContext *gtc = DCAST(TinyTextureContext, tc);
  1457. gtc->set_active(true);
  1458. GLTexture *gltex = gtc->_gltex;
  1459. if (gtc->was_image_modified() || gltex->num_levels == 0) {
  1460. // If the texture image was modified, reload the texture.
  1461. bool okflag = upload_texture(gtc);
  1462. if (!okflag) {
  1463. tinydisplay_cat.error()
  1464. << "Could not load " << *gtc->get_texture()
  1465. << ": inappropriate size.\n";
  1466. }
  1467. gtc->mark_loaded();
  1468. if (!okflag) {
  1469. return false;
  1470. }
  1471. }
  1472. gtc->enqueue_lru(&_textures_lru);
  1473. _c->current_texture = gltex;
  1474. _c->zb->current_texture = gltex->levels;
  1475. return true;
  1476. }
  1477. ////////////////////////////////////////////////////////////////////
  1478. // Function: TinyGraphicsStateGuardian::upload_texture
  1479. // Access: Protected
  1480. // Description: Uploads the texture image to TinyGL.
  1481. //
  1482. // The return value is true if successful, or false if
  1483. // the texture has no image.
  1484. ////////////////////////////////////////////////////////////////////
  1485. bool TinyGraphicsStateGuardian::
  1486. upload_texture(TinyTextureContext *gtc) {
  1487. Texture *tex = gtc->get_texture();
  1488. PStatTimer timer(_load_texture_pcollector);
  1489. CPTA_uchar src_image = tex->get_ram_image();
  1490. if (src_image.is_null()) {
  1491. return false;
  1492. }
  1493. if (tinydisplay_cat.is_debug()) {
  1494. tinydisplay_cat.debug()
  1495. << "loading texture " << tex->get_name() << "\n";
  1496. }
  1497. #ifdef DO_PSTATS
  1498. _data_transferred_pcollector.add_level(tex->get_ram_image_size());
  1499. #endif
  1500. GLTexture *gltex = gtc->_gltex;
  1501. int num_levels = 1;
  1502. if (tex->uses_mipmaps()) {
  1503. if (!tex->has_all_ram_mipmap_images()) {
  1504. tex->generate_ram_mipmap_images();
  1505. }
  1506. num_levels = tex->get_num_ram_mipmap_images();
  1507. }
  1508. if (!setup_gltex(gltex, tex->get_x_size(), tex->get_y_size(), num_levels)) {
  1509. return false;
  1510. }
  1511. int bytecount = 0;
  1512. int xsize = gltex->xsize;
  1513. int ysize = gltex->ysize;
  1514. for (int level = 0; level < gltex->num_levels; ++level) {
  1515. ZTextureLevel *dest = &gltex->levels[level];
  1516. switch (tex->get_format()) {
  1517. case Texture::F_rgb:
  1518. case Texture::F_rgb5:
  1519. case Texture::F_rgb8:
  1520. case Texture::F_rgb12:
  1521. case Texture::F_rgb332:
  1522. copy_rgb_image(dest, xsize, ysize, tex, level);
  1523. break;
  1524. case Texture::F_rgba:
  1525. case Texture::F_rgbm:
  1526. case Texture::F_rgba4:
  1527. case Texture::F_rgba5:
  1528. case Texture::F_rgba8:
  1529. case Texture::F_rgba12:
  1530. case Texture::F_rgba16:
  1531. case Texture::F_rgba32:
  1532. copy_rgba_image(dest, xsize, ysize, tex, level);
  1533. break;
  1534. case Texture::F_luminance:
  1535. copy_lum_image(dest, xsize, ysize, tex, level);
  1536. break;
  1537. case Texture::F_red:
  1538. copy_one_channel_image(dest, xsize, ysize, tex, level, 0);
  1539. break;
  1540. case Texture::F_green:
  1541. copy_one_channel_image(dest, xsize, ysize, tex, level, 1);
  1542. break;
  1543. case Texture::F_blue:
  1544. copy_one_channel_image(dest, xsize, ysize, tex, level, 2);
  1545. break;
  1546. case Texture::F_alpha:
  1547. copy_alpha_image(dest, xsize, ysize, tex, level);
  1548. break;
  1549. case Texture::F_luminance_alphamask:
  1550. case Texture::F_luminance_alpha:
  1551. copy_la_image(dest, xsize, ysize, tex, level);
  1552. break;
  1553. }
  1554. bytecount += xsize * ysize * 4;
  1555. xsize = max(xsize >> 1, 1);
  1556. ysize = max(ysize >> 1, 1);
  1557. }
  1558. gtc->update_data_size_bytes(bytecount);
  1559. tex->texture_uploaded();
  1560. return true;
  1561. }
  1562. ////////////////////////////////////////////////////////////////////
  1563. // Function: TinyGraphicsStateGuardian::setup_gltex
  1564. // Access: Private
  1565. // Description: Sets the GLTexture size, bits, and masks appropriate,
  1566. // and allocates space for a pixmap. Does not fill the
  1567. // pixmap contents. Returns true if the texture is a
  1568. // valid size, false otherwise.
  1569. ////////////////////////////////////////////////////////////////////
  1570. bool TinyGraphicsStateGuardian::
  1571. setup_gltex(GLTexture *gltex, int x_size, int y_size, int num_levels) {
  1572. int s_bits = get_tex_shift(x_size);
  1573. int t_bits = get_tex_shift(y_size);
  1574. if (s_bits < 0 || t_bits < 0) {
  1575. return false;
  1576. }
  1577. gltex->xsize = x_size;
  1578. gltex->ysize = y_size;
  1579. gltex->s_max = 1 << (s_bits + ZB_POINT_ST_FRAC_BITS);
  1580. gltex->t_max = 1 << (t_bits + ZB_POINT_ST_FRAC_BITS);
  1581. for (int i = 0; i < gltex->num_levels; ++i) {
  1582. gl_free(gltex->levels[i].pixmap);
  1583. }
  1584. if (gltex->levels != NULL) {
  1585. gl_free(gltex->levels);
  1586. gltex->levels = NULL;
  1587. }
  1588. gltex->levels = (ZTextureLevel *)gl_malloc(sizeof(ZTextureLevel) * MAX_MIPMAP_LEVELS);
  1589. gltex->num_levels = num_levels;
  1590. int level = 0;
  1591. ZTextureLevel *dest = NULL;
  1592. while (level < num_levels) {
  1593. dest = &gltex->levels[level];
  1594. int bytecount = x_size * y_size * 4;
  1595. dest->pixmap = (PIXEL *)gl_malloc(bytecount);
  1596. dest->s_mask = (1 << (s_bits + ZB_POINT_ST_FRAC_BITS)) - (1 << ZB_POINT_ST_FRAC_BITS);
  1597. dest->t_mask = (1 << (t_bits + ZB_POINT_ST_FRAC_BITS)) - (1 << ZB_POINT_ST_FRAC_BITS);
  1598. dest->t_shift = (ZB_POINT_ST_FRAC_BITS - s_bits);
  1599. x_size = max((x_size >> 1), 1);
  1600. y_size = max((y_size >> 1), 1);
  1601. s_bits = max(s_bits - 1, 0);
  1602. t_bits = max(t_bits - 1, 0);
  1603. ++level;
  1604. }
  1605. // Fill out the remaining mipmap arrays with copies of the last
  1606. // level, so we don't have to be concerned with running off the end
  1607. // of this array while scanning out triangles.
  1608. while (level < MAX_MIPMAP_LEVELS) {
  1609. gltex->levels[level] = *dest;
  1610. ++level;
  1611. }
  1612. return true;
  1613. }
  1614. ////////////////////////////////////////////////////////////////////
  1615. // Function: TinyGraphicsStateGuardian::get_tex_shift
  1616. // Access: Private
  1617. // Description: Calculates the bit shift count, such that (1 << shift)
  1618. // == size. Returns -1 if the size is not a power of 2
  1619. // or is larger than our largest allowable size.
  1620. ////////////////////////////////////////////////////////////////////
  1621. int TinyGraphicsStateGuardian::
  1622. get_tex_shift(int orig_size) {
  1623. if ((orig_size & (orig_size - 1)) != 0) {
  1624. // Not a power of 2.
  1625. return -1;
  1626. }
  1627. if (orig_size > _max_texture_dimension) {
  1628. return -1;
  1629. }
  1630. return count_bits_in_word((unsigned int)orig_size - 1);
  1631. }
  1632. ////////////////////////////////////////////////////////////////////
  1633. // Function: TinyGraphicsStateGuardian::copy_lum_image
  1634. // Access: Private, Static
  1635. // Description: Copies and scales the one-channel luminance image
  1636. // from the texture into the indicated ZTexture pixmap.
  1637. ////////////////////////////////////////////////////////////////////
  1638. void TinyGraphicsStateGuardian::
  1639. copy_lum_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level) {
  1640. nassertv(tex->get_num_components() == 1);
  1641. nassertv(tex->get_expected_mipmap_x_size(level) == xsize &&
  1642. tex->get_expected_mipmap_y_size(level) == ysize);
  1643. CPTA_uchar src_image = tex->get_ram_mipmap_image(level);
  1644. nassertv(!src_image.is_null());
  1645. const unsigned char *src = src_image.p();
  1646. // Component width, and offset to the high-order byte.
  1647. int cw = tex->get_component_width();
  1648. #ifdef WORDS_BIGENDIAN
  1649. // Big-endian: the high-order byte is always first.
  1650. static const int co = 0;
  1651. #else
  1652. // Little-endian: the high-order byte is last.
  1653. int co = cw - 1;
  1654. #endif
  1655. unsigned char *dpix = (unsigned char *)dest->pixmap;
  1656. nassertv(dpix != NULL);
  1657. const unsigned char *spix = src;
  1658. int pixel_count = xsize * ysize;
  1659. while (pixel_count-- > 0) {
  1660. dpix[0] = spix[co];
  1661. dpix[1] = spix[co];
  1662. dpix[2] = spix[co];
  1663. dpix[3] = 0xff;
  1664. dpix += 4;
  1665. spix += cw;
  1666. }
  1667. }
  1668. ////////////////////////////////////////////////////////////////////
  1669. // Function: TinyGraphicsStateGuardian::copy_alpha_image
  1670. // Access: Private, Static
  1671. // Description: Copies and scales the one-channel alpha image
  1672. // from the texture into the indicated ZTexture pixmap.
  1673. ////////////////////////////////////////////////////////////////////
  1674. void TinyGraphicsStateGuardian::
  1675. copy_alpha_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level) {
  1676. nassertv(tex->get_num_components() == 1);
  1677. CPTA_uchar src_image = tex->get_ram_mipmap_image(level);
  1678. nassertv(!src_image.is_null());
  1679. const unsigned char *src = src_image.p();
  1680. // Component width, and offset to the high-order byte.
  1681. int cw = tex->get_component_width();
  1682. #ifdef WORDS_BIGENDIAN
  1683. // Big-endian: the high-order byte is always first.
  1684. static const int co = 0;
  1685. #else
  1686. // Little-endian: the high-order byte is last.
  1687. int co = cw - 1;
  1688. #endif
  1689. unsigned char *dpix = (unsigned char *)dest->pixmap;
  1690. nassertv(dpix != NULL);
  1691. const unsigned char *spix = src;
  1692. int pixel_count = xsize * ysize;
  1693. while (pixel_count-- > 0) {
  1694. dpix[0] = 0xff;
  1695. dpix[1] = 0xff;
  1696. dpix[2] = 0xff;
  1697. dpix[3] = spix[co];
  1698. dpix += 4;
  1699. spix += cw;
  1700. }
  1701. }
  1702. ////////////////////////////////////////////////////////////////////
  1703. // Function: TinyGraphicsStateGuardian::copy_one_channel_image
  1704. // Access: Private, Static
  1705. // Description: Copies and scales the one-channel image (with a
  1706. // single channel, e.g. red, green, or blue) from
  1707. // the texture into the indicated ZTexture pixmap.
  1708. ////////////////////////////////////////////////////////////////////
  1709. void TinyGraphicsStateGuardian::
  1710. copy_one_channel_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level, int channel) {
  1711. nassertv(tex->get_num_components() == 1);
  1712. CPTA_uchar src_image = tex->get_ram_mipmap_image(level);
  1713. nassertv(!src_image.is_null());
  1714. const unsigned char *src = src_image.p();
  1715. // Component width, and offset to the high-order byte.
  1716. int cw = tex->get_component_width();
  1717. #ifdef WORDS_BIGENDIAN
  1718. // Big-endian: the high-order byte is always first.
  1719. static const int co = 0;
  1720. #else
  1721. // Little-endian: the high-order byte is last.
  1722. int co = cw - 1;
  1723. #endif
  1724. unsigned char *dpix = (unsigned char *)dest->pixmap;
  1725. nassertv(dpix != NULL);
  1726. const unsigned char *spix = src;
  1727. int pixel_count = xsize * ysize;
  1728. while (pixel_count-- > 0) {
  1729. dpix[0] = 0;
  1730. dpix[1] = 0;
  1731. dpix[2] = 0;
  1732. dpix[3] = 0xff;
  1733. dpix[channel] = spix[co];
  1734. dpix += 4;
  1735. spix += cw;
  1736. }
  1737. }
  1738. ////////////////////////////////////////////////////////////////////
  1739. // Function: TinyGraphicsStateGuardian::copy_la_image
  1740. // Access: Private, Static
  1741. // Description: Copies and scales the two-channel luminance-alpha
  1742. // image from the texture into the indicated ZTexture
  1743. // pixmap.
  1744. ////////////////////////////////////////////////////////////////////
  1745. void TinyGraphicsStateGuardian::
  1746. copy_la_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level) {
  1747. nassertv(tex->get_num_components() == 2);
  1748. CPTA_uchar src_image = tex->get_ram_mipmap_image(level);
  1749. nassertv(!src_image.is_null());
  1750. const unsigned char *src = src_image.p();
  1751. // Component width, and offset to the high-order byte.
  1752. int cw = tex->get_component_width();
  1753. #ifdef WORDS_BIGENDIAN
  1754. // Big-endian: the high-order byte is always first.
  1755. static const int co = 0;
  1756. #else
  1757. // Little-endian: the high-order byte is last.
  1758. int co = cw - 1;
  1759. #endif
  1760. unsigned char *dpix = (unsigned char *)dest->pixmap;
  1761. nassertv(dpix != NULL);
  1762. const unsigned char *spix = src;
  1763. int pixel_count = xsize * ysize;
  1764. while (pixel_count-- > 0) {
  1765. dpix[0] = spix[co];
  1766. dpix[1] = spix[co];
  1767. dpix[2] = spix[co];
  1768. dpix[3] = spix[cw + co];
  1769. dpix += 4;
  1770. spix += 2 * cw;
  1771. }
  1772. }
  1773. ////////////////////////////////////////////////////////////////////
  1774. // Function: TinyGraphicsStateGuardian::copy_rgb_image
  1775. // Access: Private, Static
  1776. // Description: Copies and scales the three-channel RGB image from
  1777. // the texture into the indicated ZTexture pixmap.
  1778. ////////////////////////////////////////////////////////////////////
  1779. void TinyGraphicsStateGuardian::
  1780. copy_rgb_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level) {
  1781. nassertv(tex->get_num_components() == 3);
  1782. CPTA_uchar src_image = tex->get_ram_mipmap_image(level);
  1783. nassertv(!src_image.is_null());
  1784. const unsigned char *src = src_image.p();
  1785. // Component width, and offset to the high-order byte.
  1786. int cw = tex->get_component_width();
  1787. #ifdef WORDS_BIGENDIAN
  1788. // Big-endian: the high-order byte is always first.
  1789. static const int co = 0;
  1790. #else
  1791. // Little-endian: the high-order byte is last.
  1792. int co = cw - 1;
  1793. #endif
  1794. unsigned char *dpix = (unsigned char *)dest->pixmap;
  1795. nassertv(dpix != NULL);
  1796. const unsigned char *spix = src;
  1797. int pixel_count = xsize * ysize;
  1798. while (pixel_count-- > 0) {
  1799. dpix[0] = spix[co];
  1800. dpix[1] = spix[cw + co];
  1801. dpix[2] = spix[cw + cw + co];
  1802. dpix[3] = 0xff;
  1803. dpix += 4;
  1804. spix += 3 * cw;
  1805. }
  1806. }
  1807. ////////////////////////////////////////////////////////////////////
  1808. // Function: TinyGraphicsStateGuardian::copy_rgba_image
  1809. // Access: Private, Static
  1810. // Description: Copies and scales the four-channel RGBA image from
  1811. // the texture into the indicated ZTexture pixmap.
  1812. ////////////////////////////////////////////////////////////////////
  1813. void TinyGraphicsStateGuardian::
  1814. copy_rgba_image(ZTextureLevel *dest, int xsize, int ysize, Texture *tex, int level) {
  1815. nassertv(tex->get_num_components() == 4);
  1816. CPTA_uchar src_image = tex->get_ram_mipmap_image(level);
  1817. nassertv(!src_image.is_null());
  1818. const unsigned char *src = src_image.p();
  1819. // Component width, and offset to the high-order byte.
  1820. int cw = tex->get_component_width();
  1821. #ifdef WORDS_BIGENDIAN
  1822. // Big-endian: the high-order byte is always first.
  1823. static const int co = 0;
  1824. #else
  1825. // Little-endian: the high-order byte is last.
  1826. int co = cw - 1;
  1827. #endif
  1828. unsigned char *dpix = (unsigned char *)dest->pixmap;
  1829. nassertv(dpix != NULL);
  1830. const unsigned char *spix = src;
  1831. int pixel_count = xsize * ysize;
  1832. while (pixel_count-- > 0) {
  1833. dpix[0] = spix[co];
  1834. dpix[1] = spix[cw + co];
  1835. dpix[2] = spix[cw + cw + co];
  1836. dpix[3] = spix[cw + cw + cw + co];
  1837. dpix += 4;
  1838. spix += 4 * cw;
  1839. }
  1840. }
  1841. ////////////////////////////////////////////////////////////////////
  1842. // Function: TinyGraphicsStateGuardian::setup_material
  1843. // Access: Private
  1844. // Description: Applies the desired parametesr to the indicated
  1845. // GLMaterial object.
  1846. ////////////////////////////////////////////////////////////////////
  1847. void TinyGraphicsStateGuardian::
  1848. setup_material(GLMaterial *gl_material, const Material *material) {
  1849. const Colorf &specular = material->get_specular();
  1850. gl_material->specular.X = specular[0];
  1851. gl_material->specular.Y = specular[1];
  1852. gl_material->specular.Z = specular[2];
  1853. gl_material->specular.W = specular[3];
  1854. const Colorf &emission = material->get_emission();
  1855. gl_material->emission.X = emission[0];
  1856. gl_material->emission.Y = emission[1];
  1857. gl_material->emission.Z = emission[2];
  1858. gl_material->emission.W = emission[3];
  1859. gl_material->shininess = material->get_shininess();
  1860. gl_material->shininess_i = (int)((material->get_shininess() / 128.0f) * SPECULAR_BUFFER_RESOLUTION);
  1861. _color_material_flags = CMF_ambient | CMF_diffuse;
  1862. if (material->has_ambient()) {
  1863. const Colorf &ambient = material->get_ambient();
  1864. gl_material->ambient.X = ambient[0];
  1865. gl_material->ambient.Y = ambient[1];
  1866. gl_material->ambient.Z = ambient[2];
  1867. gl_material->ambient.W = ambient[3];
  1868. _color_material_flags &= ~CMF_ambient;
  1869. }
  1870. if (material->has_diffuse()) {
  1871. const Colorf &diffuse = material->get_diffuse();
  1872. gl_material->diffuse.X = diffuse[0];
  1873. gl_material->diffuse.Y = diffuse[1];
  1874. gl_material->diffuse.Z = diffuse[2];
  1875. gl_material->diffuse.W = diffuse[3];
  1876. _color_material_flags &= ~CMF_diffuse;
  1877. }
  1878. }
  1879. ////////////////////////////////////////////////////////////////////
  1880. // Function: TinyGraphicsStateGuardian::load_matrix
  1881. // Access: Private, Static
  1882. // Description: Copies the Panda matrix stored in the indicated
  1883. // TransformState object into the indicated TinyGL
  1884. // matrix.
  1885. ////////////////////////////////////////////////////////////////////
  1886. void TinyGraphicsStateGuardian::
  1887. load_matrix(M4 *matrix, const TransformState *transform) {
  1888. const LMatrix4f &pm = transform->get_mat();
  1889. for (int i = 0; i < 4; ++i) {
  1890. matrix->m[0][i] = pm.get_cell(i, 0);
  1891. matrix->m[1][i] = pm.get_cell(i, 1);
  1892. matrix->m[2][i] = pm.get_cell(i, 2);
  1893. matrix->m[3][i] = pm.get_cell(i, 3);
  1894. }
  1895. }
  1896. ////////////////////////////////////////////////////////////////////
  1897. // Function: TinyGraphicsStateGuardian::get_color_blend_op
  1898. // Access: Private, Static
  1899. // Description: Returns the integer element of store_pixel_funcs (as
  1900. // defined by store_pixel.py) that corresponds to the
  1901. // indicated ColorBlendAttrib operand code.
  1902. ////////////////////////////////////////////////////////////////////
  1903. int TinyGraphicsStateGuardian::
  1904. get_color_blend_op(ColorBlendAttrib::Operand operand) {
  1905. switch (operand) {
  1906. case ColorBlendAttrib::O_zero:
  1907. return 0;
  1908. case ColorBlendAttrib::O_one:
  1909. return 1;
  1910. case ColorBlendAttrib::O_incoming_color:
  1911. return 2;
  1912. case ColorBlendAttrib::O_one_minus_incoming_color:
  1913. return 3;
  1914. case ColorBlendAttrib::O_fbuffer_color:
  1915. return 4;
  1916. case ColorBlendAttrib::O_one_minus_fbuffer_color:
  1917. return 5;
  1918. case ColorBlendAttrib::O_incoming_alpha:
  1919. return 6;
  1920. case ColorBlendAttrib::O_one_minus_incoming_alpha:
  1921. return 7;
  1922. case ColorBlendAttrib::O_fbuffer_alpha:
  1923. return 8;
  1924. case ColorBlendAttrib::O_one_minus_fbuffer_alpha:
  1925. return 9;
  1926. case ColorBlendAttrib::O_constant_color:
  1927. return 10;
  1928. case ColorBlendAttrib::O_one_minus_constant_color:
  1929. return 11;
  1930. case ColorBlendAttrib::O_constant_alpha:
  1931. return 12;
  1932. case ColorBlendAttrib::O_one_minus_constant_alpha:
  1933. return 13;
  1934. case ColorBlendAttrib::O_incoming_color_saturate:
  1935. return 1;
  1936. case ColorBlendAttrib::O_color_scale:
  1937. return 10;
  1938. case ColorBlendAttrib::O_one_minus_color_scale:
  1939. return 11;
  1940. case ColorBlendAttrib::O_alpha_scale:
  1941. return 12;
  1942. case ColorBlendAttrib::O_one_minus_alpha_scale:
  1943. return 13;
  1944. }
  1945. return 0;
  1946. }
  1947. ////////////////////////////////////////////////////////////////////
  1948. // Function: TinyGraphicsStateGuardian::get_tex_filter_func
  1949. // Access: Private, Static
  1950. // Description: Returns the pointer to the appropriate filter
  1951. // function according to the texture's filter type.
  1952. ////////////////////////////////////////////////////////////////////
  1953. ZB_lookupTextureFunc TinyGraphicsStateGuardian::
  1954. get_tex_filter_func(Texture::FilterType filter) {
  1955. switch (filter) {
  1956. case Texture::FT_nearest:
  1957. return &lookup_texture_nearest;
  1958. case Texture::FT_linear:
  1959. return &lookup_texture_bilinear;
  1960. case Texture::FT_nearest_mipmap_nearest:
  1961. if (td_ignore_mipmaps) {
  1962. return &lookup_texture_nearest;
  1963. }
  1964. return &lookup_texture_mipmap_nearest;
  1965. case Texture::FT_nearest_mipmap_linear:
  1966. if (td_ignore_mipmaps) {
  1967. return &lookup_texture_nearest;
  1968. }
  1969. return &lookup_texture_mipmap_linear;
  1970. case Texture::FT_linear_mipmap_nearest:
  1971. if (td_ignore_mipmaps) {
  1972. return &lookup_texture_bilinear;
  1973. }
  1974. return &lookup_texture_mipmap_bilinear;
  1975. case Texture::FT_linear_mipmap_linear:
  1976. if (td_ignore_mipmaps) {
  1977. return &lookup_texture_bilinear;
  1978. }
  1979. return &lookup_texture_mipmap_trilinear;
  1980. default:
  1981. return &lookup_texture_nearest;
  1982. }
  1983. }