CmGpuProgramParams.cpp 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGpuProgramParams.h"
  25. #include "CmMatrix4.h"
  26. #include "CmVector3.h"
  27. #include "CmVector4.h"
  28. #include "CmRenderSystemCapabilities.h"
  29. #include "CmException.h"
  30. namespace CamelotEngine
  31. {
  32. //---------------------------------------------------------------------
  33. GpuProgramParameters::AutoConstantDefinition GpuProgramParameters::AutoConstantDictionary[] = {
  34. AutoConstantDefinition(ACT_WORLD_MATRIX, "world_matrix", 16, ET_REAL, ACDT_NONE),
  35. AutoConstantDefinition(ACT_INVERSE_WORLD_MATRIX, "inverse_world_matrix", 16, ET_REAL, ACDT_NONE),
  36. AutoConstantDefinition(ACT_TRANSPOSE_WORLD_MATRIX, "transpose_world_matrix", 16, ET_REAL, ACDT_NONE),
  37. AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_WORLD_MATRIX, "inverse_transpose_world_matrix", 16, ET_REAL, ACDT_NONE),
  38. AutoConstantDefinition(ACT_WORLD_MATRIX_ARRAY_3x4, "world_matrix_array_3x4", 12, ET_REAL, ACDT_NONE),
  39. AutoConstantDefinition(ACT_WORLD_MATRIX_ARRAY, "world_matrix_array", 16, ET_REAL, ACDT_NONE),
  40. AutoConstantDefinition(ACT_VIEW_MATRIX, "view_matrix", 16, ET_REAL, ACDT_NONE),
  41. AutoConstantDefinition(ACT_INVERSE_VIEW_MATRIX, "inverse_view_matrix", 16, ET_REAL, ACDT_NONE),
  42. AutoConstantDefinition(ACT_TRANSPOSE_VIEW_MATRIX, "transpose_view_matrix", 16, ET_REAL, ACDT_NONE),
  43. AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_VIEW_MATRIX, "inverse_transpose_view_matrix", 16, ET_REAL, ACDT_NONE),
  44. AutoConstantDefinition(ACT_PROJECTION_MATRIX, "projection_matrix", 16, ET_REAL, ACDT_NONE),
  45. AutoConstantDefinition(ACT_INVERSE_PROJECTION_MATRIX, "inverse_projection_matrix", 16, ET_REAL, ACDT_NONE),
  46. AutoConstantDefinition(ACT_TRANSPOSE_PROJECTION_MATRIX, "transpose_projection_matrix", 16, ET_REAL, ACDT_NONE),
  47. AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_PROJECTION_MATRIX, "inverse_transpose_projection_matrix", 16, ET_REAL, ACDT_NONE),
  48. AutoConstantDefinition(ACT_VIEWPROJ_MATRIX, "viewproj_matrix", 16, ET_REAL, ACDT_NONE),
  49. AutoConstantDefinition(ACT_INVERSE_VIEWPROJ_MATRIX, "inverse_viewproj_matrix", 16, ET_REAL, ACDT_NONE),
  50. AutoConstantDefinition(ACT_TRANSPOSE_VIEWPROJ_MATRIX, "transpose_viewproj_matrix", 16, ET_REAL, ACDT_NONE),
  51. AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_VIEWPROJ_MATRIX, "inverse_transpose_viewproj_matrix", 16, ET_REAL, ACDT_NONE),
  52. AutoConstantDefinition(ACT_WORLDVIEW_MATRIX, "worldview_matrix", 16, ET_REAL, ACDT_NONE),
  53. AutoConstantDefinition(ACT_INVERSE_WORLDVIEW_MATRIX, "inverse_worldview_matrix", 16, ET_REAL, ACDT_NONE),
  54. AutoConstantDefinition(ACT_TRANSPOSE_WORLDVIEW_MATRIX, "transpose_worldview_matrix", 16, ET_REAL, ACDT_NONE),
  55. AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX, "inverse_transpose_worldview_matrix", 16, ET_REAL, ACDT_NONE),
  56. AutoConstantDefinition(ACT_WORLDVIEWPROJ_MATRIX, "worldviewproj_matrix", 16, ET_REAL, ACDT_NONE),
  57. AutoConstantDefinition(ACT_INVERSE_WORLDVIEWPROJ_MATRIX, "inverse_worldviewproj_matrix", 16, ET_REAL, ACDT_NONE),
  58. AutoConstantDefinition(ACT_TRANSPOSE_WORLDVIEWPROJ_MATRIX, "transpose_worldviewproj_matrix", 16, ET_REAL, ACDT_NONE),
  59. AutoConstantDefinition(ACT_INVERSE_TRANSPOSE_WORLDVIEWPROJ_MATRIX, "inverse_transpose_worldviewproj_matrix", 16, ET_REAL, ACDT_NONE),
  60. AutoConstantDefinition(ACT_RENDER_TARGET_FLIPPING, "render_target_flipping", 1, ET_REAL, ACDT_NONE),
  61. AutoConstantDefinition(ACT_VERTEX_WINDING, "vertex_winding", 1, ET_REAL, ACDT_NONE),
  62. AutoConstantDefinition(ACT_FOG_COLOUR, "fog_colour", 4, ET_REAL, ACDT_NONE),
  63. AutoConstantDefinition(ACT_FOG_PARAMS, "fog_params", 4, ET_REAL, ACDT_NONE),
  64. AutoConstantDefinition(ACT_SURFACE_AMBIENT_COLOUR, "surface_ambient_colour", 4, ET_REAL, ACDT_NONE),
  65. AutoConstantDefinition(ACT_SURFACE_DIFFUSE_COLOUR, "surface_diffuse_colour", 4, ET_REAL, ACDT_NONE),
  66. AutoConstantDefinition(ACT_SURFACE_SPECULAR_COLOUR, "surface_specular_colour", 4, ET_REAL, ACDT_NONE),
  67. AutoConstantDefinition(ACT_SURFACE_EMISSIVE_COLOUR, "surface_emissive_colour", 4, ET_REAL, ACDT_NONE),
  68. AutoConstantDefinition(ACT_SURFACE_SHININESS, "surface_shininess", 1, ET_REAL, ACDT_NONE),
  69. AutoConstantDefinition(ACT_LIGHT_COUNT, "light_count", 1, ET_REAL, ACDT_NONE),
  70. AutoConstantDefinition(ACT_AMBIENT_LIGHT_COLOUR, "ambient_light_colour", 4, ET_REAL, ACDT_NONE),
  71. AutoConstantDefinition(ACT_LIGHT_DIFFUSE_COLOUR, "light_diffuse_colour", 4, ET_REAL, ACDT_INT),
  72. AutoConstantDefinition(ACT_LIGHT_SPECULAR_COLOUR, "light_specular_colour", 4, ET_REAL, ACDT_INT),
  73. AutoConstantDefinition(ACT_LIGHT_ATTENUATION, "light_attenuation", 4, ET_REAL, ACDT_INT),
  74. AutoConstantDefinition(ACT_SPOTLIGHT_PARAMS, "spotlight_params", 4, ET_REAL, ACDT_INT),
  75. AutoConstantDefinition(ACT_LIGHT_POSITION, "light_position", 4, ET_REAL, ACDT_INT),
  76. AutoConstantDefinition(ACT_LIGHT_POSITION_OBJECT_SPACE, "light_position_object_space", 4, ET_REAL, ACDT_INT),
  77. AutoConstantDefinition(ACT_LIGHT_POSITION_VIEW_SPACE, "light_position_view_space", 4, ET_REAL, ACDT_INT),
  78. AutoConstantDefinition(ACT_LIGHT_DIRECTION, "light_direction", 4, ET_REAL, ACDT_INT),
  79. AutoConstantDefinition(ACT_LIGHT_DIRECTION_OBJECT_SPACE, "light_direction_object_space", 4, ET_REAL, ACDT_INT),
  80. AutoConstantDefinition(ACT_LIGHT_DIRECTION_VIEW_SPACE, "light_direction_view_space", 4, ET_REAL, ACDT_INT),
  81. AutoConstantDefinition(ACT_LIGHT_DISTANCE_OBJECT_SPACE, "light_distance_object_space", 1, ET_REAL, ACDT_INT),
  82. AutoConstantDefinition(ACT_LIGHT_POWER_SCALE, "light_power", 1, ET_REAL, ACDT_INT),
  83. AutoConstantDefinition(ACT_LIGHT_DIFFUSE_COLOUR_POWER_SCALED, "light_diffuse_colour_power_scaled", 4, ET_REAL, ACDT_INT),
  84. AutoConstantDefinition(ACT_LIGHT_SPECULAR_COLOUR_POWER_SCALED, "light_specular_colour_power_scaled", 4, ET_REAL, ACDT_INT),
  85. AutoConstantDefinition(ACT_LIGHT_DIFFUSE_COLOUR_ARRAY, "light_diffuse_colour_array", 4, ET_REAL, ACDT_INT),
  86. AutoConstantDefinition(ACT_LIGHT_SPECULAR_COLOUR_ARRAY, "light_specular_colour_array", 4, ET_REAL, ACDT_INT),
  87. AutoConstantDefinition(ACT_LIGHT_DIFFUSE_COLOUR_POWER_SCALED_ARRAY, "light_diffuse_colour_power_scaled_array", 4, ET_REAL, ACDT_INT),
  88. AutoConstantDefinition(ACT_LIGHT_SPECULAR_COLOUR_POWER_SCALED_ARRAY, "light_specular_colour_power_scaled_array", 4, ET_REAL, ACDT_INT),
  89. AutoConstantDefinition(ACT_LIGHT_ATTENUATION_ARRAY, "light_attenuation_array", 4, ET_REAL, ACDT_INT),
  90. AutoConstantDefinition(ACT_LIGHT_POSITION_ARRAY, "light_position_array", 4, ET_REAL, ACDT_INT),
  91. AutoConstantDefinition(ACT_LIGHT_POSITION_OBJECT_SPACE_ARRAY, "light_position_object_space_array", 4, ET_REAL, ACDT_INT),
  92. AutoConstantDefinition(ACT_LIGHT_POSITION_VIEW_SPACE_ARRAY, "light_position_view_space_array", 4, ET_REAL, ACDT_INT),
  93. AutoConstantDefinition(ACT_LIGHT_DIRECTION_ARRAY, "light_direction_array", 4, ET_REAL, ACDT_INT),
  94. AutoConstantDefinition(ACT_LIGHT_DIRECTION_OBJECT_SPACE_ARRAY, "light_direction_object_space_array", 4, ET_REAL, ACDT_INT),
  95. AutoConstantDefinition(ACT_LIGHT_DIRECTION_VIEW_SPACE_ARRAY, "light_direction_view_space_array", 4, ET_REAL, ACDT_INT),
  96. AutoConstantDefinition(ACT_LIGHT_DISTANCE_OBJECT_SPACE_ARRAY, "light_distance_object_space_array", 1, ET_REAL, ACDT_INT),
  97. AutoConstantDefinition(ACT_LIGHT_POWER_SCALE_ARRAY, "light_power_array", 1, ET_REAL, ACDT_INT),
  98. AutoConstantDefinition(ACT_SPOTLIGHT_PARAMS_ARRAY, "spotlight_params_array", 4, ET_REAL, ACDT_INT),
  99. AutoConstantDefinition(ACT_DERIVED_AMBIENT_LIGHT_COLOUR, "derived_ambient_light_colour", 4, ET_REAL, ACDT_NONE),
  100. AutoConstantDefinition(ACT_DERIVED_SCENE_COLOUR, "derived_scene_colour", 4, ET_REAL, ACDT_NONE),
  101. AutoConstantDefinition(ACT_DERIVED_LIGHT_DIFFUSE_COLOUR, "derived_light_diffuse_colour", 4, ET_REAL, ACDT_INT),
  102. AutoConstantDefinition(ACT_DERIVED_LIGHT_SPECULAR_COLOUR, "derived_light_specular_colour", 4, ET_REAL, ACDT_INT),
  103. AutoConstantDefinition(ACT_DERIVED_LIGHT_DIFFUSE_COLOUR_ARRAY, "derived_light_diffuse_colour_array", 4, ET_REAL, ACDT_INT),
  104. AutoConstantDefinition(ACT_DERIVED_LIGHT_SPECULAR_COLOUR_ARRAY, "derived_light_specular_colour_array", 4, ET_REAL, ACDT_INT),
  105. AutoConstantDefinition(ACT_LIGHT_NUMBER, "light_number", 1, ET_REAL, ACDT_INT),
  106. AutoConstantDefinition(ACT_LIGHT_CASTS_SHADOWS, "light_casts_shadows", 1, ET_REAL, ACDT_INT),
  107. AutoConstantDefinition(ACT_SHADOW_EXTRUSION_DISTANCE, "shadow_extrusion_distance", 1, ET_REAL, ACDT_INT),
  108. AutoConstantDefinition(ACT_CAMERA_POSITION, "camera_position", 3, ET_REAL, ACDT_NONE),
  109. AutoConstantDefinition(ACT_CAMERA_POSITION_OBJECT_SPACE, "camera_position_object_space", 3, ET_REAL, ACDT_NONE),
  110. AutoConstantDefinition(ACT_TEXTURE_VIEWPROJ_MATRIX, "texture_viewproj_matrix", 16, ET_REAL, ACDT_INT),
  111. AutoConstantDefinition(ACT_TEXTURE_VIEWPROJ_MATRIX_ARRAY, "texture_viewproj_matrix_array", 16, ET_REAL, ACDT_INT),
  112. AutoConstantDefinition(ACT_TEXTURE_WORLDVIEWPROJ_MATRIX, "texture_worldviewproj_matrix",16, ET_REAL, ACDT_INT),
  113. AutoConstantDefinition(ACT_TEXTURE_WORLDVIEWPROJ_MATRIX_ARRAY, "texture_worldviewproj_matrix_array",16, ET_REAL, ACDT_INT),
  114. AutoConstantDefinition(ACT_SPOTLIGHT_VIEWPROJ_MATRIX, "spotlight_viewproj_matrix", 16, ET_REAL, ACDT_INT),
  115. AutoConstantDefinition(ACT_SPOTLIGHT_VIEWPROJ_MATRIX_ARRAY, "spotlight_viewproj_matrix_array", 16, ET_REAL, ACDT_INT),
  116. AutoConstantDefinition(ACT_SPOTLIGHT_WORLDVIEWPROJ_MATRIX, "spotlight_worldviewproj_matrix",16, ET_REAL, ACDT_INT),
  117. AutoConstantDefinition(ACT_CUSTOM, "custom", 4, ET_REAL, ACDT_INT), // *** needs to be tested
  118. AutoConstantDefinition(ACT_TIME, "time", 1, ET_REAL, ACDT_REAL),
  119. AutoConstantDefinition(ACT_TIME_0_X, "time_0_x", 4, ET_REAL, ACDT_REAL),
  120. AutoConstantDefinition(ACT_COSTIME_0_X, "costime_0_x", 4, ET_REAL, ACDT_REAL),
  121. AutoConstantDefinition(ACT_SINTIME_0_X, "sintime_0_x", 4, ET_REAL, ACDT_REAL),
  122. AutoConstantDefinition(ACT_TANTIME_0_X, "tantime_0_x", 4, ET_REAL, ACDT_REAL),
  123. AutoConstantDefinition(ACT_TIME_0_X_PACKED, "time_0_x_packed", 4, ET_REAL, ACDT_REAL),
  124. AutoConstantDefinition(ACT_TIME_0_1, "time_0_1", 4, ET_REAL, ACDT_REAL),
  125. AutoConstantDefinition(ACT_COSTIME_0_1, "costime_0_1", 4, ET_REAL, ACDT_REAL),
  126. AutoConstantDefinition(ACT_SINTIME_0_1, "sintime_0_1", 4, ET_REAL, ACDT_REAL),
  127. AutoConstantDefinition(ACT_TANTIME_0_1, "tantime_0_1", 4, ET_REAL, ACDT_REAL),
  128. AutoConstantDefinition(ACT_TIME_0_1_PACKED, "time_0_1_packed", 4, ET_REAL, ACDT_REAL),
  129. AutoConstantDefinition(ACT_TIME_0_2PI, "time_0_2pi", 4, ET_REAL, ACDT_REAL),
  130. AutoConstantDefinition(ACT_COSTIME_0_2PI, "costime_0_2pi", 4, ET_REAL, ACDT_REAL),
  131. AutoConstantDefinition(ACT_SINTIME_0_2PI, "sintime_0_2pi", 4, ET_REAL, ACDT_REAL),
  132. AutoConstantDefinition(ACT_TANTIME_0_2PI, "tantime_0_2pi", 4, ET_REAL, ACDT_REAL),
  133. AutoConstantDefinition(ACT_TIME_0_2PI_PACKED, "time_0_2pi_packed", 4, ET_REAL, ACDT_REAL),
  134. AutoConstantDefinition(ACT_FRAME_TIME, "frame_time", 1, ET_REAL, ACDT_REAL),
  135. AutoConstantDefinition(ACT_FPS, "fps", 1, ET_REAL, ACDT_NONE),
  136. AutoConstantDefinition(ACT_VIEWPORT_WIDTH, "viewport_width", 1, ET_REAL, ACDT_NONE),
  137. AutoConstantDefinition(ACT_VIEWPORT_HEIGHT, "viewport_height", 1, ET_REAL, ACDT_NONE),
  138. AutoConstantDefinition(ACT_INVERSE_VIEWPORT_WIDTH, "inverse_viewport_width", 1, ET_REAL, ACDT_NONE),
  139. AutoConstantDefinition(ACT_INVERSE_VIEWPORT_HEIGHT, "inverse_viewport_height", 1, ET_REAL, ACDT_NONE),
  140. AutoConstantDefinition(ACT_VIEWPORT_SIZE, "viewport_size", 4, ET_REAL, ACDT_NONE),
  141. AutoConstantDefinition(ACT_VIEW_DIRECTION, "view_direction", 3, ET_REAL, ACDT_NONE),
  142. AutoConstantDefinition(ACT_VIEW_SIDE_VECTOR, "view_side_vector", 3, ET_REAL, ACDT_NONE),
  143. AutoConstantDefinition(ACT_VIEW_UP_VECTOR, "view_up_vector", 3, ET_REAL, ACDT_NONE),
  144. AutoConstantDefinition(ACT_FOV, "fov", 1, ET_REAL, ACDT_NONE),
  145. AutoConstantDefinition(ACT_NEAR_CLIP_DISTANCE, "near_clip_distance", 1, ET_REAL, ACDT_NONE),
  146. AutoConstantDefinition(ACT_FAR_CLIP_DISTANCE, "far_clip_distance", 1, ET_REAL, ACDT_NONE),
  147. AutoConstantDefinition(ACT_PASS_NUMBER, "pass_number", 1, ET_REAL, ACDT_NONE),
  148. AutoConstantDefinition(ACT_PASS_ITERATION_NUMBER, "pass_iteration_number", 1, ET_REAL, ACDT_NONE),
  149. AutoConstantDefinition(ACT_ANIMATION_PARAMETRIC, "animation_parametric", 4, ET_REAL, ACDT_INT),
  150. AutoConstantDefinition(ACT_TEXEL_OFFSETS, "texel_offsets", 4, ET_REAL, ACDT_NONE),
  151. AutoConstantDefinition(ACT_SCENE_DEPTH_RANGE, "scene_depth_range", 4, ET_REAL, ACDT_NONE),
  152. AutoConstantDefinition(ACT_SHADOW_SCENE_DEPTH_RANGE, "shadow_scene_depth_range", 4, ET_REAL, ACDT_INT),
  153. AutoConstantDefinition(ACT_SHADOW_COLOUR, "shadow_colour", 4, ET_REAL, ACDT_NONE),
  154. AutoConstantDefinition(ACT_TEXTURE_SIZE, "texture_size", 4, ET_REAL, ACDT_INT),
  155. AutoConstantDefinition(ACT_INVERSE_TEXTURE_SIZE, "inverse_texture_size", 4, ET_REAL, ACDT_INT),
  156. AutoConstantDefinition(ACT_PACKED_TEXTURE_SIZE, "packed_texture_size", 4, ET_REAL, ACDT_INT),
  157. AutoConstantDefinition(ACT_TEXTURE_MATRIX, "texture_matrix", 16, ET_REAL, ACDT_INT),
  158. AutoConstantDefinition(ACT_LOD_CAMERA_POSITION, "lod_camera_position", 3, ET_REAL, ACDT_NONE),
  159. AutoConstantDefinition(ACT_LOD_CAMERA_POSITION_OBJECT_SPACE, "lod_camera_position_object_space", 3, ET_REAL, ACDT_NONE),
  160. AutoConstantDefinition(ACT_LIGHT_CUSTOM, "light_custom", 4, ET_REAL, ACDT_INT)
  161. };
  162. bool GpuNamedConstants::msGenerateAllConstantDefinitionArrayEntries = false;
  163. //---------------------------------------------------------------------
  164. void GpuNamedConstants::generateConstantDefinitionArrayEntries(
  165. const String& paramName, const GpuConstantDefinition& baseDef)
  166. {
  167. // Copy definition for use with arrays
  168. GpuConstantDefinition arrayDef = baseDef;
  169. arrayDef.arraySize = 1;
  170. String arrayName;
  171. // Add parameters for array accessors
  172. // [0] will refer to the same location, [1+] will increment
  173. // only populate others individually up to 16 array slots so as not to get out of hand,
  174. // unless the system has been explicitly configured to allow all the parameters to be added
  175. // paramName[0] version will always exist
  176. size_t maxArrayIndex = 1;
  177. if (baseDef.arraySize <= 16 || msGenerateAllConstantDefinitionArrayEntries)
  178. maxArrayIndex = baseDef.arraySize;
  179. for (size_t i = 0; i < maxArrayIndex; i++)
  180. {
  181. arrayName = paramName + "[" + toString(i) + "]";
  182. map.insert(GpuConstantDefinitionMap::value_type(arrayName, arrayDef));
  183. // increment location
  184. arrayDef.physicalIndex += arrayDef.elementSize;
  185. }
  186. // note no increment of buffer sizes since this is shared with main array def
  187. }
  188. //---------------------------------------------------------------------
  189. bool GpuNamedConstants::getGenerateAllConstantDefinitionArrayEntries()
  190. {
  191. return msGenerateAllConstantDefinitionArrayEntries;
  192. }
  193. //---------------------------------------------------------------------
  194. void GpuNamedConstants::setGenerateAllConstantDefinitionArrayEntries(bool generateAll)
  195. {
  196. msGenerateAllConstantDefinitionArrayEntries = generateAll;
  197. }
  198. //-----------------------------------------------------------------------------
  199. // GpuSharedParameters Methods
  200. //-----------------------------------------------------------------------------
  201. GpuSharedParameters::GpuSharedParameters(const String& name)
  202. :mName(name)
  203. // TODO PORT - Commented out because I don't have root in port. But I dont think this will be needed in final version
  204. /*, mFrameLastUpdated(Root::getSingleton().getNextFrameNumber())*/
  205. , mFrameLastUpdated(0)
  206. , mVersion(0)
  207. {
  208. }
  209. //---------------------------------------------------------------------
  210. GpuSharedParameters::~GpuSharedParameters()
  211. {
  212. }
  213. //---------------------------------------------------------------------
  214. void GpuSharedParameters::addConstantDefinition(const String& name, GpuConstantType constType, size_t arraySize)
  215. {
  216. if (mNamedConstants.map.find(name) != mNamedConstants.map.end())
  217. {
  218. CM_EXCEPT(InvalidParametersException,
  219. "Constant entry with name '" + name + "' already exists. ");
  220. }
  221. GpuConstantDefinition def;
  222. def.arraySize = arraySize;
  223. def.constType = constType;
  224. // for compatibility we do not pad values to multiples of 4
  225. // when it comes to arrays, user is responsible for creating matching defs
  226. def.elementSize = GpuConstantDefinition::getElementSize(constType, false);
  227. // not used
  228. def.logicalIndex = 0;
  229. def.variability = (UINT16)GPV_GLOBAL;
  230. if (def.isFloat())
  231. {
  232. def.physicalIndex = mFloatConstants.size();
  233. mFloatConstants.resize(mFloatConstants.size() + def.arraySize * def.elementSize);
  234. }
  235. else
  236. {
  237. def.physicalIndex = mIntConstants.size();
  238. mIntConstants.resize(mIntConstants.size() + def.arraySize * def.elementSize);
  239. }
  240. mNamedConstants.map[name] = def;
  241. ++mVersion;
  242. }
  243. //---------------------------------------------------------------------
  244. void GpuSharedParameters::removeConstantDefinition(const String& name)
  245. {
  246. GpuConstantDefinitionMap::iterator i = mNamedConstants.map.find(name);
  247. if (i != mNamedConstants.map.end())
  248. {
  249. GpuConstantDefinition& def = i->second;
  250. bool isFloat = def.isFloat();
  251. size_t numElems = def.elementSize * def.arraySize;
  252. for (GpuConstantDefinitionMap::iterator j = mNamedConstants.map.begin();
  253. j != mNamedConstants.map.end(); ++j)
  254. {
  255. GpuConstantDefinition& otherDef = j->second;
  256. bool otherIsFloat = otherDef.isFloat();
  257. // same type, and comes after in the buffer
  258. if ( ((isFloat && otherIsFloat) || (!isFloat && !otherIsFloat)) &&
  259. otherDef.physicalIndex > def.physicalIndex)
  260. {
  261. // adjust index
  262. otherDef.physicalIndex -= numElems;
  263. }
  264. }
  265. // remove floats and reduce buffer
  266. if (isFloat)
  267. {
  268. mNamedConstants.floatBufferSize -= numElems;
  269. FloatConstantList::iterator beg = mFloatConstants.begin();
  270. std::advance(beg, def.physicalIndex);
  271. FloatConstantList::iterator en = beg;
  272. std::advance(en, numElems);
  273. mFloatConstants.erase(beg, en);
  274. }
  275. else
  276. {
  277. mNamedConstants.intBufferSize -= numElems;
  278. IntConstantList::iterator beg = mIntConstants.begin();
  279. std::advance(beg, def.physicalIndex);
  280. IntConstantList::iterator en = beg;
  281. std::advance(en, numElems);
  282. mIntConstants.erase(beg, en);
  283. }
  284. ++mVersion;
  285. }
  286. }
  287. //---------------------------------------------------------------------
  288. void GpuSharedParameters::removeAllConstantDefinitions()
  289. {
  290. mNamedConstants.map.clear();
  291. mNamedConstants.floatBufferSize = 0;
  292. mNamedConstants.intBufferSize = 0;
  293. mFloatConstants.clear();
  294. mIntConstants.clear();
  295. }
  296. //---------------------------------------------------------------------
  297. GpuConstantDefinitionIterator GpuSharedParameters::getConstantDefinitionIterator(void) const
  298. {
  299. return mNamedConstants.map.begin();
  300. }
  301. //---------------------------------------------------------------------
  302. const GpuConstantDefinition& GpuSharedParameters::getConstantDefinition(const String& name) const
  303. {
  304. GpuConstantDefinitionMap::const_iterator i = mNamedConstants.map.find(name);
  305. if (i == mNamedConstants.map.end())
  306. {
  307. CM_EXCEPT(InvalidParametersException,
  308. "Constant entry with name '" + name + "' does not exist. ");
  309. }
  310. return i->second;
  311. }
  312. //---------------------------------------------------------------------
  313. const GpuNamedConstants& GpuSharedParameters::getConstantDefinitions() const
  314. {
  315. return mNamedConstants;
  316. }
  317. //---------------------------------------------------------------------
  318. void GpuSharedParameters::setNamedConstant(const String& name, float val)
  319. {
  320. setNamedConstant(name, &val, 1);
  321. }
  322. //---------------------------------------------------------------------
  323. void GpuSharedParameters::setNamedConstant(const String& name, int val)
  324. {
  325. setNamedConstant(name, &val, 1);
  326. }
  327. //---------------------------------------------------------------------
  328. void GpuSharedParameters::setNamedConstant(const String& name, const Vector4& vec)
  329. {
  330. setNamedConstant(name, vec.ptr(), 4);
  331. }
  332. //---------------------------------------------------------------------
  333. void GpuSharedParameters::setNamedConstant(const String& name, const Vector3& vec)
  334. {
  335. setNamedConstant(name, vec.ptr(), 3);
  336. }
  337. //---------------------------------------------------------------------
  338. void GpuSharedParameters::setNamedConstant(const String& name, const Matrix4& m)
  339. {
  340. setNamedConstant(name, m[0], 16);
  341. }
  342. //---------------------------------------------------------------------
  343. void GpuSharedParameters::setNamedConstant(const String& name, const Matrix4* m, size_t numEntries)
  344. {
  345. setNamedConstant(name, m[0][0], 16 * numEntries);
  346. }
  347. //---------------------------------------------------------------------
  348. void GpuSharedParameters::setNamedConstant(const String& name, const float *val, size_t count)
  349. {
  350. GpuConstantDefinitionMap::const_iterator i = mNamedConstants.map.find(name);
  351. if (i != mNamedConstants.map.end())
  352. {
  353. const GpuConstantDefinition& def = i->second;
  354. memcpy(&mFloatConstants[def.physicalIndex], val,
  355. sizeof(float) * std::min(count, def.elementSize * def.arraySize));
  356. }
  357. _markDirty();
  358. }
  359. //---------------------------------------------------------------------
  360. void GpuSharedParameters::setNamedConstant(const String& name, const double *val, size_t count)
  361. {
  362. GpuConstantDefinitionMap::const_iterator i = mNamedConstants.map.find(name);
  363. if (i != mNamedConstants.map.end())
  364. {
  365. const GpuConstantDefinition& def = i->second;
  366. count = std::min(count, def.elementSize * def.arraySize);
  367. const double* src = val;
  368. float* dst = &mFloatConstants[def.physicalIndex];
  369. for (size_t v = 0; v < count; ++v)
  370. {
  371. *dst++ = static_cast<float>(*src++);
  372. }
  373. }
  374. _markDirty();
  375. }
  376. //---------------------------------------------------------------------
  377. void GpuSharedParameters::setNamedConstant(const String& name, const Color& colour)
  378. {
  379. setNamedConstant(name, colour.ptr(), 4);
  380. }
  381. //---------------------------------------------------------------------
  382. void GpuSharedParameters::setNamedConstant(const String& name, const int *val, size_t count)
  383. {
  384. GpuConstantDefinitionMap::const_iterator i = mNamedConstants.map.find(name);
  385. if (i != mNamedConstants.map.end())
  386. {
  387. const GpuConstantDefinition& def = i->second;
  388. memcpy(&mIntConstants[def.physicalIndex], val,
  389. sizeof(int) * std::min(count, def.elementSize * def.arraySize));
  390. }
  391. _markDirty();
  392. }
  393. //---------------------------------------------------------------------
  394. void GpuSharedParameters::_markDirty()
  395. {
  396. // TODO PORT - Don't have access to Root in port. Set it to 0 for now. Will probably be able to remove this completely
  397. mFrameLastUpdated = 0;
  398. /*mFrameLastUpdated = Root::getSingleton().getNextFrameNumber();*/
  399. }
  400. //-----------------------------------------------------------------------------
  401. // GpuSharedParametersUsage Methods
  402. //-----------------------------------------------------------------------------
  403. GpuSharedParametersUsage::GpuSharedParametersUsage(GpuSharedParametersPtr sharedParams,
  404. GpuProgramParameters* params)
  405. : mSharedParams(sharedParams)
  406. , mParams(params)
  407. {
  408. initCopyData();
  409. }
  410. //---------------------------------------------------------------------
  411. void GpuSharedParametersUsage::initCopyData()
  412. {
  413. mCopyDataList.clear();
  414. const GpuConstantDefinitionMap& sharedmap = mSharedParams->getConstantDefinitions().map;
  415. for (GpuConstantDefinitionMap::const_iterator i = sharedmap.begin(); i != sharedmap.end(); ++i)
  416. {
  417. const String& pName = i->first;
  418. const GpuConstantDefinition& shareddef = i->second;
  419. const GpuConstantDefinition* instdef = mParams->_findNamedConstantDefinition(pName, false);
  420. if (instdef)
  421. {
  422. // Check that the definitions are the same
  423. if (instdef->constType == shareddef.constType &&
  424. instdef->arraySize == shareddef.arraySize)
  425. {
  426. CopyDataEntry e;
  427. e.srcDefinition = &shareddef;
  428. e.dstDefinition = instdef;
  429. mCopyDataList.push_back(e);
  430. }
  431. }
  432. }
  433. mCopyDataVersion = mSharedParams->getVersion();
  434. }
  435. //---------------------------------------------------------------------
  436. void GpuSharedParametersUsage::_copySharedParamsToTargetParams()
  437. {
  438. // check copy data version
  439. if (mCopyDataVersion != mSharedParams->getVersion())
  440. initCopyData();
  441. for (CopyDataList::iterator i = mCopyDataList.begin(); i != mCopyDataList.end(); ++i)
  442. {
  443. CopyDataEntry& e = *i;
  444. if (e.dstDefinition->isFloat())
  445. {
  446. const float* pSrc = mSharedParams->getFloatPointer(e.srcDefinition->physicalIndex);
  447. float* pDst = mParams->getFloatPointer(e.dstDefinition->physicalIndex);
  448. // Deal with matrix transposition here!!!
  449. // transposition is specific to the dest param set, shared params don't do it
  450. if (mParams->getTransposeMatrices() && e.dstDefinition->constType == GCT_MATRIX_4X4)
  451. {
  452. for (int row = 0; row < 4; ++row)
  453. for (int col = 0; col < 4; ++col)
  454. pDst[row * 4 + col] = pSrc[col * 4 + row];
  455. }
  456. else
  457. {
  458. if (e.dstDefinition->elementSize == e.srcDefinition->elementSize)
  459. {
  460. // simple copy
  461. memcpy(pDst, pSrc, sizeof(float) * e.dstDefinition->elementSize * e.dstDefinition->arraySize);
  462. }
  463. else
  464. {
  465. // target params may be padded to 4 elements, shared params are packed
  466. assert(e.dstDefinition->elementSize % 4 == 0);
  467. size_t iterations = e.dstDefinition->elementSize / 4
  468. * e.dstDefinition->arraySize;
  469. size_t valsPerIteration = e.srcDefinition->elementSize / iterations;
  470. for (size_t l = 0; l < iterations; ++l)
  471. {
  472. memcpy(pDst, pSrc, sizeof(float) * valsPerIteration);
  473. pSrc += valsPerIteration;
  474. pDst += 4;
  475. }
  476. }
  477. }
  478. }
  479. else
  480. {
  481. const int* pSrc = mSharedParams->getIntPointer(e.srcDefinition->physicalIndex);
  482. int* pDst = mParams->getIntPointer(e.dstDefinition->physicalIndex);
  483. if (e.dstDefinition->elementSize == e.srcDefinition->elementSize)
  484. {
  485. // simple copy
  486. memcpy(pDst, pSrc, sizeof(int) * e.dstDefinition->elementSize * e.dstDefinition->arraySize);
  487. }
  488. else
  489. {
  490. // target params may be padded to 4 elements, shared params are packed
  491. assert(e.dstDefinition->elementSize % 4 == 0);
  492. size_t iterations = (e.dstDefinition->elementSize / 4)
  493. * e.dstDefinition->arraySize;
  494. size_t valsPerIteration = e.srcDefinition->elementSize / iterations;
  495. for (size_t l = 0; l < iterations; ++l)
  496. {
  497. memcpy(pDst, pSrc, sizeof(int) * valsPerIteration);
  498. pSrc += valsPerIteration;
  499. pDst += 4;
  500. }
  501. }
  502. }
  503. }
  504. }
  505. //-----------------------------------------------------------------------------
  506. // GpuProgramParameters Methods
  507. //-----------------------------------------------------------------------------
  508. GpuProgramParameters::GpuProgramParameters() :
  509. mCombinedVariability(GPV_GLOBAL)
  510. , mTransposeMatrices(false)
  511. , mIgnoreMissingParams(false)
  512. , mActivePassIterationIndex(std::numeric_limits<size_t>::max())
  513. {
  514. }
  515. //-----------------------------------------------------------------------------
  516. GpuProgramParameters::GpuProgramParameters(const GpuProgramParameters& oth)
  517. {
  518. *this = oth;
  519. }
  520. //-----------------------------------------------------------------------------
  521. GpuProgramParameters& GpuProgramParameters::operator=(const GpuProgramParameters& oth)
  522. {
  523. // let compiler perform shallow copies of structures
  524. // AutoConstantEntry, RealConstantEntry, IntConstantEntry
  525. mFloatConstants = oth.mFloatConstants;
  526. mIntConstants = oth.mIntConstants;
  527. mAutoConstants = oth.mAutoConstants;
  528. mFloatLogicalToPhysical = oth.mFloatLogicalToPhysical;
  529. mIntLogicalToPhysical = oth.mIntLogicalToPhysical;
  530. mNamedConstants = oth.mNamedConstants;
  531. copySharedParamSetUsage(oth.mSharedParamSets);
  532. mCombinedVariability = oth.mCombinedVariability;
  533. mTransposeMatrices = oth.mTransposeMatrices;
  534. mIgnoreMissingParams = oth.mIgnoreMissingParams;
  535. mActivePassIterationIndex = oth.mActivePassIterationIndex;
  536. return *this;
  537. }
  538. //---------------------------------------------------------------------
  539. void GpuProgramParameters::copySharedParamSetUsage(const GpuSharedParamUsageList& srcList)
  540. {
  541. mSharedParamSets.clear();
  542. for (GpuSharedParamUsageList::const_iterator i = srcList.begin(); i != srcList.end(); ++i)
  543. {
  544. mSharedParamSets.push_back(GpuSharedParametersUsage(i->getSharedParams(), this));
  545. }
  546. }
  547. //---------------------------------------------------------------------
  548. void GpuProgramParameters::_setNamedConstants(
  549. const GpuNamedConstantsPtr& namedConstants)
  550. {
  551. mNamedConstants = namedConstants;
  552. // Determine any extension to local buffers
  553. // Size and reset buffer (fill with zero to make comparison later ok)
  554. if (namedConstants->floatBufferSize > mFloatConstants.size())
  555. {
  556. mFloatConstants.insert(mFloatConstants.end(),
  557. namedConstants->floatBufferSize - mFloatConstants.size(), 0.0f);
  558. }
  559. if (namedConstants->intBufferSize > mIntConstants.size())
  560. {
  561. mIntConstants.insert(mIntConstants.end(),
  562. namedConstants->intBufferSize - mIntConstants.size(), 0);
  563. }
  564. }
  565. //---------------------------------------------------------------------
  566. void GpuProgramParameters::_setLogicalIndexes(
  567. const GpuLogicalBufferStructPtr& floatIndexMap,
  568. const GpuLogicalBufferStructPtr& intIndexMap)
  569. {
  570. mFloatLogicalToPhysical = floatIndexMap;
  571. mIntLogicalToPhysical = intIndexMap;
  572. // resize the internal buffers
  573. // Note that these will only contain something after the first parameter
  574. // set has set some parameters
  575. // Size and reset buffer (fill with zero to make comparison later ok)
  576. if ((floatIndexMap != nullptr) && floatIndexMap->bufferSize > mFloatConstants.size())
  577. {
  578. mFloatConstants.insert(mFloatConstants.end(),
  579. floatIndexMap->bufferSize - mFloatConstants.size(), 0.0f);
  580. }
  581. if ((intIndexMap != nullptr) && intIndexMap->bufferSize > mIntConstants.size())
  582. {
  583. mIntConstants.insert(mIntConstants.end(),
  584. intIndexMap->bufferSize - mIntConstants.size(), 0);
  585. }
  586. }
  587. //---------------------------------------------------------------------()
  588. void GpuProgramParameters::setConstant(size_t index, const Vector4& vec)
  589. {
  590. setConstant(index, vec.ptr(), 1);
  591. }
  592. //-----------------------------------------------------------------------------
  593. void GpuProgramParameters::setConstant(size_t index, float val)
  594. {
  595. setConstant(index, Vector4(val, 0.0f, 0.0f, 0.0f));
  596. }
  597. //-----------------------------------------------------------------------------
  598. void GpuProgramParameters::setConstant(size_t index, const Vector3& vec)
  599. {
  600. setConstant(index, Vector4(vec.x, vec.y, vec.z, 1.0f));
  601. }
  602. //-----------------------------------------------------------------------------
  603. void GpuProgramParameters::setConstant(size_t index, const Matrix4& m)
  604. {
  605. // set as 4x 4-element floats
  606. if (mTransposeMatrices)
  607. {
  608. Matrix4 t = m.transpose();
  609. GpuProgramParameters::setConstant(index, t[0], 4);
  610. }
  611. else
  612. {
  613. GpuProgramParameters::setConstant(index, m[0], 4);
  614. }
  615. }
  616. //-----------------------------------------------------------------------------
  617. void GpuProgramParameters::setConstant(size_t index, const Matrix4* pMatrix,
  618. size_t numEntries)
  619. {
  620. if (mTransposeMatrices)
  621. {
  622. for (size_t i = 0; i < numEntries; ++i)
  623. {
  624. Matrix4 t = pMatrix[i].transpose();
  625. GpuProgramParameters::setConstant(index, t[0], 4);
  626. index += 4;
  627. }
  628. }
  629. else
  630. {
  631. GpuProgramParameters::setConstant(index, pMatrix[0][0], 4 * numEntries);
  632. }
  633. }
  634. //-----------------------------------------------------------------------------
  635. void GpuProgramParameters::setConstant(size_t index, const Color& colour)
  636. {
  637. setConstant(index, colour.ptr(), 1);
  638. }
  639. //-----------------------------------------------------------------------------
  640. void GpuProgramParameters::setConstant(size_t index, const float *val, size_t count)
  641. {
  642. // Raw buffer size is 4x count
  643. size_t rawCount = count * 4;
  644. // get physical index
  645. assert((mFloatLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  646. size_t physicalIndex = _getFloatConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  647. // Copy
  648. _writeRawConstants(physicalIndex, val, rawCount);
  649. }
  650. //-----------------------------------------------------------------------------
  651. void GpuProgramParameters::setConstant(size_t index, const double *val, size_t count)
  652. {
  653. // Raw buffer size is 4x count
  654. size_t rawCount = count * 4;
  655. // get physical index
  656. assert((mFloatLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  657. size_t physicalIndex = _getFloatConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  658. assert(physicalIndex + rawCount <= mFloatConstants.size());
  659. // Copy manually since cast required
  660. for (size_t i = 0; i < rawCount; ++i)
  661. {
  662. mFloatConstants[physicalIndex + i] =
  663. static_cast<float>(val[i]);
  664. }
  665. }
  666. //-----------------------------------------------------------------------------
  667. void GpuProgramParameters::setConstant(size_t index, const int *val, size_t count)
  668. {
  669. // Raw buffer size is 4x count
  670. size_t rawCount = count * 4;
  671. // get physical index
  672. assert((mIntLogicalToPhysical != nullptr) && "GpuProgram hasn't set up the logical -> physical map!");
  673. size_t physicalIndex = _getIntConstantPhysicalIndex(index, rawCount, GPV_GLOBAL);
  674. // Copy
  675. _writeRawConstants(physicalIndex, val, rawCount);
  676. }
  677. //-----------------------------------------------------------------------------
  678. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Vector4& vec,
  679. size_t count)
  680. {
  681. // remember, raw content access uses raw float count rather than float4
  682. // write either the number requested (for packed types) or up to 4
  683. _writeRawConstants(physicalIndex, vec.ptr(), std::min(count, (size_t)4));
  684. }
  685. //-----------------------------------------------------------------------------
  686. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, float val)
  687. {
  688. _writeRawConstants(physicalIndex, &val, 1);
  689. }
  690. //-----------------------------------------------------------------------------
  691. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, int val)
  692. {
  693. _writeRawConstants(physicalIndex, &val, 1);
  694. }
  695. //-----------------------------------------------------------------------------
  696. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Vector3& vec)
  697. {
  698. _writeRawConstants(physicalIndex, vec.ptr(), 3);
  699. }
  700. //-----------------------------------------------------------------------------
  701. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Matrix4& m,size_t elementCount)
  702. {
  703. // remember, raw content access uses raw float count rather than float4
  704. if (mTransposeMatrices)
  705. {
  706. Matrix4 t = m.transpose();
  707. _writeRawConstants(physicalIndex, t[0], elementCount>16?16:elementCount);
  708. }
  709. else
  710. {
  711. _writeRawConstants(physicalIndex, m[0], elementCount>16?16:elementCount);
  712. }
  713. }
  714. //-----------------------------------------------------------------------------
  715. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex, const Matrix4* pMatrix, size_t numEntries)
  716. {
  717. // remember, raw content access uses raw float count rather than float4
  718. if (mTransposeMatrices)
  719. {
  720. for (size_t i = 0; i < numEntries; ++i)
  721. {
  722. Matrix4 t = pMatrix[i].transpose();
  723. _writeRawConstants(physicalIndex, t[0], 16);
  724. physicalIndex += 16;
  725. }
  726. }
  727. else
  728. {
  729. _writeRawConstants(physicalIndex, pMatrix[0][0], 16 * numEntries);
  730. }
  731. }
  732. //-----------------------------------------------------------------------------
  733. void GpuProgramParameters::_writeRawConstant(size_t physicalIndex,
  734. const Color& colour, size_t count)
  735. {
  736. // write either the number requested (for packed types) or up to 4
  737. _writeRawConstants(physicalIndex, colour.ptr(), std::min(count, (size_t)4));
  738. }
  739. //-----------------------------------------------------------------------------
  740. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const double* val, size_t count)
  741. {
  742. assert(physicalIndex + count <= mFloatConstants.size());
  743. for (size_t i = 0; i < count; ++i)
  744. {
  745. mFloatConstants[physicalIndex+i] = static_cast<float>(val[i]);
  746. }
  747. }
  748. //-----------------------------------------------------------------------------
  749. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const float* val, size_t count)
  750. {
  751. assert(physicalIndex + count <= mFloatConstants.size());
  752. memcpy(&mFloatConstants[physicalIndex], val, sizeof(float) * count);
  753. }
  754. //-----------------------------------------------------------------------------
  755. void GpuProgramParameters::_writeRawConstants(size_t physicalIndex, const int* val, size_t count)
  756. {
  757. assert(physicalIndex + count <= mIntConstants.size());
  758. memcpy(&mIntConstants[physicalIndex], val, sizeof(int) * count);
  759. }
  760. //-----------------------------------------------------------------------------
  761. void GpuProgramParameters::_readRawConstants(size_t physicalIndex, size_t count, float* dest)
  762. {
  763. assert(physicalIndex + count <= mFloatConstants.size());
  764. memcpy(dest, &mFloatConstants[physicalIndex], sizeof(float) * count);
  765. }
  766. //-----------------------------------------------------------------------------
  767. void GpuProgramParameters::_readRawConstants(size_t physicalIndex, size_t count, int* dest)
  768. {
  769. assert(physicalIndex + count <= mIntConstants.size());
  770. memcpy(dest, &mIntConstants[physicalIndex], sizeof(int) * count);
  771. }
  772. //---------------------------------------------------------------------
  773. UINT16 GpuProgramParameters::deriveVariability(GpuProgramParameters::AutoConstantType act)
  774. {
  775. switch(act)
  776. {
  777. case ACT_VIEW_MATRIX:
  778. case ACT_INVERSE_VIEW_MATRIX:
  779. case ACT_TRANSPOSE_VIEW_MATRIX:
  780. case ACT_INVERSE_TRANSPOSE_VIEW_MATRIX:
  781. case ACT_PROJECTION_MATRIX:
  782. case ACT_INVERSE_PROJECTION_MATRIX:
  783. case ACT_TRANSPOSE_PROJECTION_MATRIX:
  784. case ACT_INVERSE_TRANSPOSE_PROJECTION_MATRIX:
  785. case ACT_VIEWPROJ_MATRIX:
  786. case ACT_INVERSE_VIEWPROJ_MATRIX:
  787. case ACT_TRANSPOSE_VIEWPROJ_MATRIX:
  788. case ACT_INVERSE_TRANSPOSE_VIEWPROJ_MATRIX:
  789. case ACT_RENDER_TARGET_FLIPPING:
  790. case ACT_VERTEX_WINDING:
  791. case ACT_AMBIENT_LIGHT_COLOUR:
  792. case ACT_DERIVED_AMBIENT_LIGHT_COLOUR:
  793. case ACT_DERIVED_SCENE_COLOUR:
  794. case ACT_FOG_COLOUR:
  795. case ACT_FOG_PARAMS:
  796. case ACT_SURFACE_AMBIENT_COLOUR:
  797. case ACT_SURFACE_DIFFUSE_COLOUR:
  798. case ACT_SURFACE_SPECULAR_COLOUR:
  799. case ACT_SURFACE_EMISSIVE_COLOUR:
  800. case ACT_SURFACE_SHININESS:
  801. case ACT_CAMERA_POSITION:
  802. case ACT_TIME:
  803. case ACT_TIME_0_X:
  804. case ACT_COSTIME_0_X:
  805. case ACT_SINTIME_0_X:
  806. case ACT_TANTIME_0_X:
  807. case ACT_TIME_0_X_PACKED:
  808. case ACT_TIME_0_1:
  809. case ACT_COSTIME_0_1:
  810. case ACT_SINTIME_0_1:
  811. case ACT_TANTIME_0_1:
  812. case ACT_TIME_0_1_PACKED:
  813. case ACT_TIME_0_2PI:
  814. case ACT_COSTIME_0_2PI:
  815. case ACT_SINTIME_0_2PI:
  816. case ACT_TANTIME_0_2PI:
  817. case ACT_TIME_0_2PI_PACKED:
  818. case ACT_FRAME_TIME:
  819. case ACT_FPS:
  820. case ACT_VIEWPORT_WIDTH:
  821. case ACT_VIEWPORT_HEIGHT:
  822. case ACT_INVERSE_VIEWPORT_WIDTH:
  823. case ACT_INVERSE_VIEWPORT_HEIGHT:
  824. case ACT_VIEWPORT_SIZE:
  825. case ACT_TEXEL_OFFSETS:
  826. case ACT_TEXTURE_SIZE:
  827. case ACT_INVERSE_TEXTURE_SIZE:
  828. case ACT_PACKED_TEXTURE_SIZE:
  829. case ACT_SCENE_DEPTH_RANGE:
  830. case ACT_VIEW_DIRECTION:
  831. case ACT_VIEW_SIDE_VECTOR:
  832. case ACT_VIEW_UP_VECTOR:
  833. case ACT_FOV:
  834. case ACT_NEAR_CLIP_DISTANCE:
  835. case ACT_FAR_CLIP_DISTANCE:
  836. case ACT_PASS_NUMBER:
  837. case ACT_TEXTURE_MATRIX:
  838. case ACT_LOD_CAMERA_POSITION:
  839. return (UINT16)GPV_GLOBAL;
  840. case ACT_WORLD_MATRIX:
  841. case ACT_INVERSE_WORLD_MATRIX:
  842. case ACT_TRANSPOSE_WORLD_MATRIX:
  843. case ACT_INVERSE_TRANSPOSE_WORLD_MATRIX:
  844. case ACT_WORLD_MATRIX_ARRAY_3x4:
  845. case ACT_WORLD_MATRIX_ARRAY:
  846. case ACT_WORLDVIEW_MATRIX:
  847. case ACT_INVERSE_WORLDVIEW_MATRIX:
  848. case ACT_TRANSPOSE_WORLDVIEW_MATRIX:
  849. case ACT_INVERSE_TRANSPOSE_WORLDVIEW_MATRIX:
  850. case ACT_WORLDVIEWPROJ_MATRIX:
  851. case ACT_INVERSE_WORLDVIEWPROJ_MATRIX:
  852. case ACT_TRANSPOSE_WORLDVIEWPROJ_MATRIX:
  853. case ACT_INVERSE_TRANSPOSE_WORLDVIEWPROJ_MATRIX:
  854. case ACT_CAMERA_POSITION_OBJECT_SPACE:
  855. case ACT_LOD_CAMERA_POSITION_OBJECT_SPACE:
  856. case ACT_CUSTOM:
  857. case ACT_ANIMATION_PARAMETRIC:
  858. return (UINT16)GPV_PER_OBJECT;
  859. case ACT_LIGHT_POSITION_OBJECT_SPACE:
  860. case ACT_LIGHT_DIRECTION_OBJECT_SPACE:
  861. case ACT_LIGHT_DISTANCE_OBJECT_SPACE:
  862. case ACT_LIGHT_POSITION_OBJECT_SPACE_ARRAY:
  863. case ACT_LIGHT_DIRECTION_OBJECT_SPACE_ARRAY:
  864. case ACT_LIGHT_DISTANCE_OBJECT_SPACE_ARRAY:
  865. case ACT_TEXTURE_WORLDVIEWPROJ_MATRIX:
  866. case ACT_TEXTURE_WORLDVIEWPROJ_MATRIX_ARRAY:
  867. case ACT_SPOTLIGHT_WORLDVIEWPROJ_MATRIX:
  868. // These depend on BOTH lights and objects
  869. return ((UINT16)GPV_PER_OBJECT) | ((UINT16)GPV_LIGHTS);
  870. case ACT_LIGHT_COUNT:
  871. case ACT_LIGHT_DIFFUSE_COLOUR:
  872. case ACT_LIGHT_SPECULAR_COLOUR:
  873. case ACT_LIGHT_POSITION:
  874. case ACT_LIGHT_DIRECTION:
  875. case ACT_LIGHT_POSITION_VIEW_SPACE:
  876. case ACT_LIGHT_DIRECTION_VIEW_SPACE:
  877. case ACT_SHADOW_EXTRUSION_DISTANCE:
  878. case ACT_SHADOW_SCENE_DEPTH_RANGE:
  879. case ACT_SHADOW_COLOUR:
  880. case ACT_LIGHT_POWER_SCALE:
  881. case ACT_LIGHT_DIFFUSE_COLOUR_POWER_SCALED:
  882. case ACT_LIGHT_SPECULAR_COLOUR_POWER_SCALED:
  883. case ACT_LIGHT_NUMBER:
  884. case ACT_LIGHT_CASTS_SHADOWS:
  885. case ACT_LIGHT_ATTENUATION:
  886. case ACT_SPOTLIGHT_PARAMS:
  887. case ACT_LIGHT_DIFFUSE_COLOUR_ARRAY:
  888. case ACT_LIGHT_SPECULAR_COLOUR_ARRAY:
  889. case ACT_LIGHT_DIFFUSE_COLOUR_POWER_SCALED_ARRAY:
  890. case ACT_LIGHT_SPECULAR_COLOUR_POWER_SCALED_ARRAY:
  891. case ACT_LIGHT_POSITION_ARRAY:
  892. case ACT_LIGHT_DIRECTION_ARRAY:
  893. case ACT_LIGHT_POSITION_VIEW_SPACE_ARRAY:
  894. case ACT_LIGHT_DIRECTION_VIEW_SPACE_ARRAY:
  895. case ACT_LIGHT_POWER_SCALE_ARRAY:
  896. case ACT_LIGHT_ATTENUATION_ARRAY:
  897. case ACT_SPOTLIGHT_PARAMS_ARRAY:
  898. case ACT_TEXTURE_VIEWPROJ_MATRIX:
  899. case ACT_TEXTURE_VIEWPROJ_MATRIX_ARRAY:
  900. case ACT_SPOTLIGHT_VIEWPROJ_MATRIX:
  901. case ACT_SPOTLIGHT_VIEWPROJ_MATRIX_ARRAY:
  902. case ACT_LIGHT_CUSTOM:
  903. return (UINT16)GPV_LIGHTS;
  904. case ACT_DERIVED_LIGHT_DIFFUSE_COLOUR:
  905. case ACT_DERIVED_LIGHT_SPECULAR_COLOUR:
  906. case ACT_DERIVED_LIGHT_DIFFUSE_COLOUR_ARRAY:
  907. case ACT_DERIVED_LIGHT_SPECULAR_COLOUR_ARRAY:
  908. return ((UINT16)GPV_GLOBAL | (UINT16)GPV_LIGHTS);
  909. case ACT_PASS_ITERATION_NUMBER:
  910. return (UINT16)GPV_PASS_ITERATION_NUMBER;
  911. default:
  912. return (UINT16)GPV_GLOBAL;
  913. };
  914. }
  915. //---------------------------------------------------------------------
  916. GpuLogicalIndexUse* GpuProgramParameters::_getFloatConstantLogicalIndexUse(
  917. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  918. {
  919. if (mFloatLogicalToPhysical == nullptr)
  920. return 0;
  921. GpuLogicalIndexUse* indexUse = 0;
  922. CM_LOCK_MUTEX(mFloatLogicalToPhysical->mutex)
  923. GpuLogicalIndexUseMap::iterator logi = mFloatLogicalToPhysical->map.find(logicalIndex);
  924. if (logi == mFloatLogicalToPhysical->map.end())
  925. {
  926. if (requestedSize)
  927. {
  928. size_t physicalIndex = mFloatConstants.size();
  929. // Expand at buffer end
  930. mFloatConstants.insert(mFloatConstants.end(), requestedSize, 0.0f);
  931. // Record extended size for future GPU params re-using this information
  932. mFloatLogicalToPhysical->bufferSize = mFloatConstants.size();
  933. // low-level programs will not know about mapping ahead of time, so
  934. // populate it. Other params objects will be able to just use this
  935. // accepted mapping since the constant structure will be the same
  936. // Set up a mapping for all items in the count
  937. size_t currPhys = physicalIndex;
  938. size_t count = requestedSize / 4;
  939. GpuLogicalIndexUseMap::iterator insertedIterator;
  940. for (size_t logicalNum = 0; logicalNum < count; ++logicalNum)
  941. {
  942. GpuLogicalIndexUseMap::iterator it =
  943. mFloatLogicalToPhysical->map.insert(
  944. GpuLogicalIndexUseMap::value_type(
  945. logicalIndex + logicalNum,
  946. GpuLogicalIndexUse(currPhys, requestedSize, variability))).first;
  947. currPhys += 4;
  948. if (logicalNum == 0)
  949. insertedIterator = it;
  950. }
  951. indexUse = &(insertedIterator->second);
  952. }
  953. else
  954. {
  955. // no match & ignore
  956. return 0;
  957. }
  958. }
  959. else
  960. {
  961. size_t physicalIndex = logi->second.physicalIndex;
  962. indexUse = &(logi->second);
  963. // check size
  964. if (logi->second.currentSize < requestedSize)
  965. {
  966. // init buffer entry wasn't big enough; could be a mistake on the part
  967. // of the original use, or perhaps a variable length we can't predict
  968. // until first actual runtime use e.g. world matrix array
  969. size_t insertCount = requestedSize - logi->second.currentSize;
  970. FloatConstantList::iterator insertPos = mFloatConstants.begin();
  971. std::advance(insertPos, physicalIndex);
  972. mFloatConstants.insert(insertPos, insertCount, 0.0f);
  973. // shift all physical positions after this one
  974. for (GpuLogicalIndexUseMap::iterator i = mFloatLogicalToPhysical->map.begin();
  975. i != mFloatLogicalToPhysical->map.end(); ++i)
  976. {
  977. if (i->second.physicalIndex > physicalIndex)
  978. i->second.physicalIndex += insertCount;
  979. }
  980. mFloatLogicalToPhysical->bufferSize += insertCount;
  981. for (AutoConstantList::iterator i = mAutoConstants.begin();
  982. i != mAutoConstants.end(); ++i)
  983. {
  984. if (i->physicalIndex > physicalIndex &&
  985. getAutoConstantDefinition(i->paramType)->elementType == ET_REAL)
  986. {
  987. i->physicalIndex += insertCount;
  988. }
  989. }
  990. if (mNamedConstants != nullptr)
  991. {
  992. for (GpuConstantDefinitionMap::iterator i = mNamedConstants->map.begin();
  993. i != mNamedConstants->map.end(); ++i)
  994. {
  995. if (i->second.isFloat() && i->second.physicalIndex > physicalIndex)
  996. i->second.physicalIndex += insertCount;
  997. }
  998. mNamedConstants->floatBufferSize += insertCount;
  999. }
  1000. logi->second.currentSize += insertCount;
  1001. }
  1002. }
  1003. if (indexUse)
  1004. indexUse->variability = variability;
  1005. return indexUse;
  1006. }
  1007. //---------------------------------------------------------------------()
  1008. GpuLogicalIndexUse* GpuProgramParameters::_getIntConstantLogicalIndexUse(size_t logicalIndex, size_t requestedSize, UINT16 variability)
  1009. {
  1010. if (mIntLogicalToPhysical == nullptr)
  1011. {
  1012. CM_EXCEPT(InvalidParametersException,
  1013. "This is not a low-level parameter parameter object");
  1014. }
  1015. GpuLogicalIndexUse* indexUse = 0;
  1016. CM_LOCK_MUTEX(mIntLogicalToPhysical->mutex)
  1017. GpuLogicalIndexUseMap::iterator logi = mIntLogicalToPhysical->map.find(logicalIndex);
  1018. if (logi == mIntLogicalToPhysical->map.end())
  1019. {
  1020. if (requestedSize)
  1021. {
  1022. size_t physicalIndex = mIntConstants.size();
  1023. // Expand at buffer end
  1024. mIntConstants.insert(mIntConstants.end(), requestedSize, 0);
  1025. // Record extended size for future GPU params re-using this information
  1026. mIntLogicalToPhysical->bufferSize = mIntConstants.size();
  1027. // low-level programs will not know about mapping ahead of time, so
  1028. // populate it. Other params objects will be able to just use this
  1029. // accepted mapping since the constant structure will be the same
  1030. // Set up a mapping for all items in the count
  1031. size_t currPhys = physicalIndex;
  1032. size_t count = requestedSize / 4;
  1033. GpuLogicalIndexUseMap::iterator insertedIterator;
  1034. for (size_t logicalNum = 0; logicalNum < count; ++logicalNum)
  1035. {
  1036. GpuLogicalIndexUseMap::iterator it =
  1037. mIntLogicalToPhysical->map.insert(
  1038. GpuLogicalIndexUseMap::value_type(
  1039. logicalIndex + logicalNum,
  1040. GpuLogicalIndexUse(currPhys, requestedSize, variability))).first;
  1041. if (logicalNum == 0)
  1042. insertedIterator = it;
  1043. currPhys += 4;
  1044. }
  1045. indexUse = &(insertedIterator->second);
  1046. }
  1047. else
  1048. {
  1049. // no match
  1050. return 0;
  1051. }
  1052. }
  1053. else
  1054. {
  1055. size_t physicalIndex = logi->second.physicalIndex;
  1056. indexUse = &(logi->second);
  1057. // check size
  1058. if (logi->second.currentSize < requestedSize)
  1059. {
  1060. // init buffer entry wasn't big enough; could be a mistake on the part
  1061. // of the original use, or perhaps a variable length we can't predict
  1062. // until first actual runtime use e.g. world matrix array
  1063. size_t insertCount = requestedSize - logi->second.currentSize;
  1064. IntConstantList::iterator insertPos = mIntConstants.begin();
  1065. std::advance(insertPos, physicalIndex);
  1066. mIntConstants.insert(insertPos, insertCount, 0);
  1067. // shift all physical positions after this one
  1068. for (GpuLogicalIndexUseMap::iterator i = mIntLogicalToPhysical->map.begin();
  1069. i != mIntLogicalToPhysical->map.end(); ++i)
  1070. {
  1071. if (i->second.physicalIndex > physicalIndex)
  1072. i->second.physicalIndex += insertCount;
  1073. }
  1074. mIntLogicalToPhysical->bufferSize += insertCount;
  1075. for (AutoConstantList::iterator i = mAutoConstants.begin();
  1076. i != mAutoConstants.end(); ++i)
  1077. {
  1078. if (i->physicalIndex > physicalIndex &&
  1079. getAutoConstantDefinition(i->paramType)->elementType == ET_INT)
  1080. {
  1081. i->physicalIndex += insertCount;
  1082. }
  1083. }
  1084. if (mNamedConstants != nullptr)
  1085. {
  1086. for (GpuConstantDefinitionMap::iterator i = mNamedConstants->map.begin();
  1087. i != mNamedConstants->map.end(); ++i)
  1088. {
  1089. if (!i->second.isFloat() && i->second.physicalIndex > physicalIndex)
  1090. i->second.physicalIndex += insertCount;
  1091. }
  1092. mNamedConstants->intBufferSize += insertCount;
  1093. }
  1094. logi->second.currentSize += insertCount;
  1095. }
  1096. }
  1097. if (indexUse)
  1098. indexUse->variability = variability;
  1099. return indexUse;
  1100. }
  1101. //-----------------------------------------------------------------------------
  1102. size_t GpuProgramParameters::_getFloatConstantPhysicalIndex(
  1103. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  1104. {
  1105. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(logicalIndex, requestedSize, variability);
  1106. return indexUse ? indexUse->physicalIndex : 0;
  1107. }
  1108. //-----------------------------------------------------------------------------
  1109. size_t GpuProgramParameters::_getIntConstantPhysicalIndex(
  1110. size_t logicalIndex, size_t requestedSize, UINT16 variability)
  1111. {
  1112. GpuLogicalIndexUse* indexUse = _getIntConstantLogicalIndexUse(logicalIndex, requestedSize, variability);
  1113. return indexUse ? indexUse->physicalIndex : 0;
  1114. }
  1115. //-----------------------------------------------------------------------------
  1116. size_t GpuProgramParameters::getFloatLogicalIndexForPhysicalIndex(size_t physicalIndex)
  1117. {
  1118. // perhaps build a reverse map of this sometime (shared in GpuProgram)
  1119. for (GpuLogicalIndexUseMap::iterator i = mFloatLogicalToPhysical->map.begin();
  1120. i != mFloatLogicalToPhysical->map.end(); ++i)
  1121. {
  1122. if (i->second.physicalIndex == physicalIndex)
  1123. return i->first;
  1124. }
  1125. return std::numeric_limits<size_t>::max();
  1126. }
  1127. //-----------------------------------------------------------------------------
  1128. size_t GpuProgramParameters::getIntLogicalIndexForPhysicalIndex(size_t physicalIndex)
  1129. {
  1130. // perhaps build a reverse map of this sometime (shared in GpuProgram)
  1131. for (GpuLogicalIndexUseMap::iterator i = mIntLogicalToPhysical->map.begin();
  1132. i != mIntLogicalToPhysical->map.end(); ++i)
  1133. {
  1134. if (i->second.physicalIndex == physicalIndex)
  1135. return i->first;
  1136. }
  1137. return std::numeric_limits<size_t>::max();
  1138. }
  1139. //-----------------------------------------------------------------------------
  1140. GpuConstantDefinitionIterator GpuProgramParameters::getConstantDefinitionIterator(void) const
  1141. {
  1142. if (mNamedConstants == nullptr)
  1143. {
  1144. CM_EXCEPT(InvalidParametersException,
  1145. "This params object is not based on a program with named parameters.");
  1146. }
  1147. return mNamedConstants->map.begin();
  1148. }
  1149. //-----------------------------------------------------------------------------
  1150. const GpuNamedConstants& GpuProgramParameters::getConstantDefinitions() const
  1151. {
  1152. if (mNamedConstants == nullptr)
  1153. {
  1154. CM_EXCEPT(InvalidParametersException,
  1155. "This params object is not based on a program with named parameters.");
  1156. }
  1157. return *mNamedConstants;
  1158. }
  1159. //-----------------------------------------------------------------------------
  1160. const GpuConstantDefinition& GpuProgramParameters::getConstantDefinition(const String& name) const
  1161. {
  1162. if (mNamedConstants == nullptr)
  1163. {
  1164. CM_EXCEPT(InvalidParametersException,
  1165. "This params object is not based on a program with named parameters.");
  1166. }
  1167. // locate, and throw exception if not found
  1168. const GpuConstantDefinition* def = _findNamedConstantDefinition(name, true);
  1169. return *def;
  1170. }
  1171. //-----------------------------------------------------------------------------
  1172. const GpuConstantDefinition*
  1173. GpuProgramParameters::_findNamedConstantDefinition(const String& name,
  1174. bool throwExceptionIfNotFound) const
  1175. {
  1176. if (mNamedConstants == nullptr)
  1177. {
  1178. if (throwExceptionIfNotFound)
  1179. {
  1180. CM_EXCEPT(InvalidParametersException,
  1181. "Named constants have not been initialised, perhaps a compile error.");
  1182. }
  1183. return 0;
  1184. }
  1185. GpuConstantDefinitionMap::const_iterator i = mNamedConstants->map.find(name);
  1186. if (i == mNamedConstants->map.end())
  1187. {
  1188. if (throwExceptionIfNotFound)
  1189. {
  1190. CM_EXCEPT(InvalidParametersException,
  1191. "Parameter called " + name + " does not exist. ");
  1192. }
  1193. return 0;
  1194. }
  1195. else
  1196. {
  1197. return &(i->second);
  1198. }
  1199. }
  1200. //-----------------------------------------------------------------------------
  1201. void GpuProgramParameters::setAutoConstant(size_t index, AutoConstantType acType, size_t extraInfo)
  1202. {
  1203. // Get auto constant definition for sizing
  1204. const AutoConstantDefinition* autoDef = getAutoConstantDefinition(acType);
  1205. // round up to nearest multiple of 4
  1206. size_t sz = autoDef->elementCount;
  1207. if (sz % 4 > 0)
  1208. {
  1209. sz += 4 - (sz % 4);
  1210. }
  1211. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(index, sz, deriveVariability(acType));
  1212. _setRawAutoConstant(indexUse->physicalIndex, acType, extraInfo, indexUse->variability, sz);
  1213. }
  1214. //-----------------------------------------------------------------------------
  1215. void GpuProgramParameters::_setRawAutoConstant(size_t physicalIndex,
  1216. AutoConstantType acType, size_t extraInfo, UINT16 variability, size_t elementSize)
  1217. {
  1218. // update existing index if it exists
  1219. bool found = false;
  1220. for (AutoConstantList::iterator i = mAutoConstants.begin();
  1221. i != mAutoConstants.end(); ++i)
  1222. {
  1223. if (i->physicalIndex == physicalIndex)
  1224. {
  1225. i->paramType = acType;
  1226. i->data = extraInfo;
  1227. i->elementCount = elementSize;
  1228. i->variability = variability;
  1229. found = true;
  1230. break;
  1231. }
  1232. }
  1233. if (!found)
  1234. mAutoConstants.push_back(AutoConstantEntry(acType, physicalIndex, extraInfo, variability, elementSize));
  1235. mCombinedVariability |= variability;
  1236. }
  1237. //-----------------------------------------------------------------------------
  1238. void GpuProgramParameters::setAutoConstant(size_t index, AutoConstantType acType, UINT16 extraInfo1, UINT16 extraInfo2)
  1239. {
  1240. size_t extraInfo = (size_t)extraInfo1 | ((size_t)extraInfo2) << 16;
  1241. // Get auto constant definition for sizing
  1242. const AutoConstantDefinition* autoDef = getAutoConstantDefinition(acType);
  1243. // round up to nearest multiple of 4
  1244. size_t sz = autoDef->elementCount;
  1245. if (sz % 4 > 0)
  1246. {
  1247. sz += 4 - (sz % 4);
  1248. }
  1249. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(index, sz, deriveVariability(acType));
  1250. _setRawAutoConstant(indexUse->physicalIndex, acType, extraInfo, indexUse->variability, sz);
  1251. }
  1252. //-----------------------------------------------------------------------------
  1253. void GpuProgramParameters::_setRawAutoConstantReal(size_t physicalIndex,
  1254. AutoConstantType acType, float rData, UINT16 variability, size_t elementSize)
  1255. {
  1256. // update existing index if it exists
  1257. bool found = false;
  1258. for (AutoConstantList::iterator i = mAutoConstants.begin();
  1259. i != mAutoConstants.end(); ++i)
  1260. {
  1261. if (i->physicalIndex == physicalIndex)
  1262. {
  1263. i->paramType = acType;
  1264. i->fData = rData;
  1265. i->elementCount = elementSize;
  1266. i->variability = variability;
  1267. found = true;
  1268. break;
  1269. }
  1270. }
  1271. if (!found)
  1272. mAutoConstants.push_back(AutoConstantEntry(acType, physicalIndex, rData, variability, elementSize));
  1273. mCombinedVariability |= variability;
  1274. }
  1275. //-----------------------------------------------------------------------------
  1276. void GpuProgramParameters::clearAutoConstant(size_t index)
  1277. {
  1278. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(index, 0, GPV_GLOBAL);
  1279. if (indexUse)
  1280. {
  1281. indexUse->variability = GPV_GLOBAL;
  1282. size_t physicalIndex = indexUse->physicalIndex;
  1283. // update existing index if it exists
  1284. for (AutoConstantList::iterator i = mAutoConstants.begin();
  1285. i != mAutoConstants.end(); ++i)
  1286. {
  1287. if (i->physicalIndex == physicalIndex)
  1288. {
  1289. mAutoConstants.erase(i);
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. }
  1295. //-----------------------------------------------------------------------------
  1296. void GpuProgramParameters::clearNamedAutoConstant(const String& name)
  1297. {
  1298. const GpuConstantDefinition* def = _findNamedConstantDefinition(name);
  1299. if (def)
  1300. {
  1301. def->variability = GPV_GLOBAL;
  1302. // Autos are always floating point
  1303. if (def->isFloat())
  1304. {
  1305. for (AutoConstantList::iterator i = mAutoConstants.begin();
  1306. i != mAutoConstants.end(); ++i)
  1307. {
  1308. if (i->physicalIndex == def->physicalIndex)
  1309. {
  1310. mAutoConstants.erase(i);
  1311. break;
  1312. }
  1313. }
  1314. }
  1315. }
  1316. }
  1317. //-----------------------------------------------------------------------------
  1318. void GpuProgramParameters::clearAutoConstants(void)
  1319. {
  1320. mAutoConstants.clear();
  1321. mCombinedVariability = GPV_GLOBAL;
  1322. }
  1323. //-----------------------------------------------------------------------------
  1324. GpuProgramParameters::AutoConstantIterator GpuProgramParameters::getAutoConstantIterator(void) const
  1325. {
  1326. return mAutoConstants.begin();
  1327. }
  1328. //-----------------------------------------------------------------------------
  1329. void GpuProgramParameters::setAutoConstantReal(size_t index, AutoConstantType acType, float rData)
  1330. {
  1331. // Get auto constant definition for sizing
  1332. const AutoConstantDefinition* autoDef = getAutoConstantDefinition(acType);
  1333. // round up to nearest multiple of 4
  1334. size_t sz = autoDef->elementCount;
  1335. if (sz % 4 > 0)
  1336. {
  1337. sz += 4 - (sz % 4);
  1338. }
  1339. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(index, sz, deriveVariability(acType));
  1340. _setRawAutoConstantReal(indexUse->physicalIndex, acType, rData, indexUse->variability, sz);
  1341. }
  1342. //-----------------------------------------------------------------------------
  1343. void GpuProgramParameters::setNamedConstant(const String& name, float val)
  1344. {
  1345. // look up, and throw an exception if we're not ignoring missing
  1346. const GpuConstantDefinition* def =
  1347. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1348. if (def)
  1349. _writeRawConstant(def->physicalIndex, val);
  1350. }
  1351. //---------------------------------------------------------------------------
  1352. void GpuProgramParameters::setNamedConstant(const String& name, int val)
  1353. {
  1354. // look up, and throw an exception if we're not ignoring missing
  1355. const GpuConstantDefinition* def =
  1356. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1357. if (def)
  1358. _writeRawConstant(def->physicalIndex, val);
  1359. }
  1360. //---------------------------------------------------------------------------
  1361. void GpuProgramParameters::setNamedConstant(const String& name, const Vector4& vec)
  1362. {
  1363. // look up, and throw an exception if we're not ignoring missing
  1364. const GpuConstantDefinition* def =
  1365. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1366. if (def)
  1367. _writeRawConstant(def->physicalIndex, vec, def->elementSize);
  1368. }
  1369. //---------------------------------------------------------------------------
  1370. void GpuProgramParameters::setNamedConstant(const String& name, const Vector3& vec)
  1371. {
  1372. // look up, and throw an exception if we're not ignoring missing
  1373. const GpuConstantDefinition* def =
  1374. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1375. if (def)
  1376. _writeRawConstant(def->physicalIndex, vec);
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4& m)
  1380. {
  1381. // look up, and throw an exception if we're not ignoring missing
  1382. const GpuConstantDefinition* def =
  1383. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1384. if (def)
  1385. _writeRawConstant(def->physicalIndex, m, def->elementSize);
  1386. }
  1387. //---------------------------------------------------------------------------
  1388. void GpuProgramParameters::setNamedConstant(const String& name, const Matrix4* m,
  1389. size_t numEntries)
  1390. {
  1391. // look up, and throw an exception if we're not ignoring missing
  1392. const GpuConstantDefinition* def =
  1393. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1394. if (def)
  1395. _writeRawConstant(def->physicalIndex, m, numEntries);
  1396. }
  1397. //---------------------------------------------------------------------------
  1398. void GpuProgramParameters::setNamedConstant(const String& name,
  1399. const float *val, size_t count, size_t multiple)
  1400. {
  1401. size_t rawCount = count * multiple;
  1402. // look up, and throw an exception if we're not ignoring missing
  1403. const GpuConstantDefinition* def =
  1404. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1405. if (def)
  1406. _writeRawConstants(def->physicalIndex, val, rawCount);
  1407. }
  1408. //---------------------------------------------------------------------------
  1409. void GpuProgramParameters::setNamedConstant(const String& name,
  1410. const double *val, size_t count, size_t multiple)
  1411. {
  1412. size_t rawCount = count * multiple;
  1413. // look up, and throw an exception if we're not ignoring missing
  1414. const GpuConstantDefinition* def =
  1415. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1416. if (def)
  1417. _writeRawConstants(def->physicalIndex, val, rawCount);
  1418. }
  1419. //---------------------------------------------------------------------------
  1420. void GpuProgramParameters::setNamedConstant(const String& name, const Color& colour)
  1421. {
  1422. // look up, and throw an exception if we're not ignoring missing
  1423. const GpuConstantDefinition* def =
  1424. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1425. if (def)
  1426. _writeRawConstant(def->physicalIndex, colour, def->elementSize);
  1427. }
  1428. //---------------------------------------------------------------------------
  1429. void GpuProgramParameters::setNamedConstant(const String& name,
  1430. const int *val, size_t count, size_t multiple)
  1431. {
  1432. size_t rawCount = count * multiple;
  1433. // look up, and throw an exception if we're not ignoring missing
  1434. const GpuConstantDefinition* def =
  1435. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1436. if (def)
  1437. _writeRawConstants(def->physicalIndex, val, rawCount);
  1438. }
  1439. //---------------------------------------------------------------------------
  1440. void GpuProgramParameters::setNamedAutoConstant(const String& name,
  1441. AutoConstantType acType, size_t extraInfo)
  1442. {
  1443. // look up, and throw an exception if we're not ignoring missing
  1444. const GpuConstantDefinition* def =
  1445. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1446. if (def)
  1447. {
  1448. def->variability = deriveVariability(acType);
  1449. // make sure we also set variability on the logical index map
  1450. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(def->logicalIndex, def->elementSize * def->arraySize, def->variability);
  1451. if (indexUse)
  1452. indexUse->variability = def->variability;
  1453. _setRawAutoConstant(def->physicalIndex, acType, extraInfo, def->variability, def->elementSize);
  1454. }
  1455. }
  1456. //---------------------------------------------------------------------------
  1457. void GpuProgramParameters::setNamedAutoConstantReal(const String& name,
  1458. AutoConstantType acType, float rData)
  1459. {
  1460. // look up, and throw an exception if we're not ignoring missing
  1461. const GpuConstantDefinition* def =
  1462. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1463. if (def)
  1464. {
  1465. def->variability = deriveVariability(acType);
  1466. // make sure we also set variability on the logical index map
  1467. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(def->logicalIndex, def->elementSize * def->arraySize, def->variability);
  1468. if (indexUse)
  1469. indexUse->variability = def->variability;
  1470. _setRawAutoConstantReal(def->physicalIndex, acType, rData, def->variability, def->elementSize);
  1471. }
  1472. }
  1473. //---------------------------------------------------------------------------
  1474. void GpuProgramParameters::setNamedAutoConstant(const String& name,
  1475. AutoConstantType acType, UINT16 extraInfo1, UINT16 extraInfo2)
  1476. {
  1477. size_t extraInfo = (size_t)extraInfo1 | ((size_t)extraInfo2) << 16;
  1478. // look up, and throw an exception if we're not ignoring missing
  1479. const GpuConstantDefinition* def =
  1480. _findNamedConstantDefinition(name, !mIgnoreMissingParams);
  1481. if (def)
  1482. {
  1483. def->variability = deriveVariability(acType);
  1484. // make sure we also set variability on the logical index map
  1485. GpuLogicalIndexUse* indexUse = _getFloatConstantLogicalIndexUse(def->logicalIndex, def->elementSize * def->arraySize, def->variability);
  1486. if (indexUse)
  1487. indexUse->variability = def->variability;
  1488. _setRawAutoConstant(def->physicalIndex, acType, extraInfo, def->variability, def->elementSize);
  1489. }
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. void GpuProgramParameters::setConstantFromTime(size_t index, float factor)
  1493. {
  1494. setAutoConstantReal(index, ACT_TIME, factor);
  1495. }
  1496. //---------------------------------------------------------------------------
  1497. void GpuProgramParameters::setNamedConstantFromTime(const String& name, float factor)
  1498. {
  1499. setNamedAutoConstantReal(name, ACT_TIME, factor);
  1500. }
  1501. //---------------------------------------------------------------------------
  1502. GpuProgramParameters::AutoConstantEntry* GpuProgramParameters::getAutoConstantEntry(const size_t index)
  1503. {
  1504. if (index < mAutoConstants.size())
  1505. {
  1506. return &(mAutoConstants[index]);
  1507. }
  1508. else
  1509. {
  1510. return NULL;
  1511. }
  1512. }
  1513. //---------------------------------------------------------------------------
  1514. const GpuProgramParameters::AutoConstantEntry*
  1515. GpuProgramParameters::findFloatAutoConstantEntry(size_t logicalIndex)
  1516. {
  1517. if (mFloatLogicalToPhysical == nullptr)
  1518. {
  1519. CM_EXCEPT(InvalidParametersException,
  1520. "This is not a low-level parameter parameter object");
  1521. }
  1522. return _findRawAutoConstantEntryFloat(
  1523. _getFloatConstantPhysicalIndex(logicalIndex, 0, GPV_GLOBAL));
  1524. }
  1525. //---------------------------------------------------------------------------
  1526. const GpuProgramParameters::AutoConstantEntry*
  1527. GpuProgramParameters::findIntAutoConstantEntry(size_t logicalIndex)
  1528. {
  1529. if (mIntLogicalToPhysical == nullptr)
  1530. {
  1531. CM_EXCEPT(InvalidParametersException,
  1532. "This is not a low-level parameter parameter object");
  1533. }
  1534. return _findRawAutoConstantEntryInt(
  1535. _getIntConstantPhysicalIndex(logicalIndex, 0, GPV_GLOBAL));
  1536. }
  1537. //---------------------------------------------------------------------------
  1538. const GpuProgramParameters::AutoConstantEntry*
  1539. GpuProgramParameters::findAutoConstantEntry(const String& paramName)
  1540. {
  1541. if (mNamedConstants == nullptr)
  1542. {
  1543. CM_EXCEPT(InvalidParametersException,
  1544. "This params object is not based on a program with named parameters.");
  1545. }
  1546. const GpuConstantDefinition& def = getConstantDefinition(paramName);
  1547. if (def.isFloat())
  1548. {
  1549. return _findRawAutoConstantEntryFloat(def.physicalIndex);
  1550. }
  1551. else
  1552. {
  1553. return _findRawAutoConstantEntryInt(def.physicalIndex);
  1554. }
  1555. }
  1556. //---------------------------------------------------------------------------
  1557. const GpuProgramParameters::AutoConstantEntry*
  1558. GpuProgramParameters::_findRawAutoConstantEntryFloat(size_t physicalIndex)
  1559. {
  1560. for(AutoConstantList::iterator i = mAutoConstants.begin();
  1561. i != mAutoConstants.end(); ++i)
  1562. {
  1563. AutoConstantEntry& ac = *i;
  1564. // should check that auto is float and not int so that physicalIndex
  1565. // doesn't have any ambiguity
  1566. // However, all autos are float I think so no need
  1567. if (ac.physicalIndex == physicalIndex)
  1568. return &ac;
  1569. }
  1570. return 0;
  1571. }
  1572. //---------------------------------------------------------------------------
  1573. const GpuProgramParameters::AutoConstantEntry*
  1574. GpuProgramParameters::_findRawAutoConstantEntryInt(size_t physicalIndex)
  1575. {
  1576. // No autos are float?
  1577. return 0;
  1578. }
  1579. //---------------------------------------------------------------------------
  1580. void GpuProgramParameters::copyConstantsFrom(const GpuProgramParameters& source)
  1581. {
  1582. // Pull buffers & auto constant list over directly
  1583. mFloatConstants = source.getFloatConstantList();
  1584. mIntConstants = source.getIntConstantList();
  1585. mAutoConstants = source.getAutoConstantList();
  1586. mCombinedVariability = source.mCombinedVariability;
  1587. copySharedParamSetUsage(source.mSharedParamSets);
  1588. }
  1589. //---------------------------------------------------------------------
  1590. void GpuProgramParameters::copyMatchingNamedConstantsFrom(const GpuProgramParameters& source)
  1591. {
  1592. if ((mNamedConstants != nullptr) && (source.mNamedConstants != nullptr))
  1593. {
  1594. std::map<size_t, String> srcToDestNamedMap;
  1595. for (GpuConstantDefinitionMap::const_iterator i = source.mNamedConstants->map.begin();
  1596. i != source.mNamedConstants->map.end(); ++i)
  1597. {
  1598. const String& paramName = i->first;
  1599. const GpuConstantDefinition& olddef = i->second;
  1600. const GpuConstantDefinition* newdef = _findNamedConstantDefinition(paramName, false);
  1601. if (newdef)
  1602. {
  1603. // Copy data across, based on smallest common definition size
  1604. size_t srcsz = olddef.elementSize * olddef.arraySize;
  1605. size_t destsz = newdef->elementSize * newdef->arraySize;
  1606. size_t sz = std::min(srcsz, destsz);
  1607. if (newdef->isFloat())
  1608. {
  1609. memcpy(getFloatPointer(newdef->physicalIndex),
  1610. source.getFloatPointer(olddef.physicalIndex),
  1611. sz * sizeof(float));
  1612. }
  1613. else
  1614. {
  1615. memcpy(getIntPointer(newdef->physicalIndex),
  1616. source.getIntPointer(olddef.physicalIndex),
  1617. sz * sizeof(int));
  1618. }
  1619. // we'll use this map to resolve autos later
  1620. // ignore the [0] aliases
  1621. if (!StringUtil::endsWith(paramName, "[0]"))
  1622. srcToDestNamedMap[olddef.physicalIndex] = paramName;
  1623. }
  1624. }
  1625. for (AutoConstantList::const_iterator i = source.mAutoConstants.begin();
  1626. i != source.mAutoConstants.end(); ++i)
  1627. {
  1628. const GpuProgramParameters::AutoConstantEntry& autoEntry = *i;
  1629. // find dest physical index
  1630. std::map<size_t, String>::iterator mi = srcToDestNamedMap.find(autoEntry.physicalIndex);
  1631. if (mi != srcToDestNamedMap.end())
  1632. {
  1633. if (autoEntry.fData)
  1634. {
  1635. setNamedAutoConstantReal(mi->second, autoEntry.paramType, autoEntry.fData);
  1636. }
  1637. else
  1638. {
  1639. setNamedAutoConstant(mi->second, autoEntry.paramType, autoEntry.data);
  1640. }
  1641. }
  1642. }
  1643. // Copy shared param sets
  1644. for (GpuSharedParamUsageList::const_iterator i = source.mSharedParamSets.begin();
  1645. i != source.mSharedParamSets.end(); ++i)
  1646. {
  1647. const GpuSharedParametersUsage& usage = *i;
  1648. if (!isUsingSharedParameters(usage.getName()))
  1649. {
  1650. addSharedParameters(usage.getSharedParams());
  1651. }
  1652. }
  1653. }
  1654. }
  1655. //-----------------------------------------------------------------------
  1656. const GpuProgramParameters::AutoConstantDefinition*
  1657. GpuProgramParameters::getAutoConstantDefinition(const String& name)
  1658. {
  1659. // find a constant definition that matches name by iterating through the
  1660. // constant definition array
  1661. bool nameFound = false;
  1662. size_t i = 0;
  1663. const size_t numDefs = getNumAutoConstantDefinitions();
  1664. while (!nameFound && (i < numDefs))
  1665. {
  1666. if (name == AutoConstantDictionary[i].name)
  1667. nameFound = true;
  1668. else
  1669. ++i;
  1670. }
  1671. if (nameFound)
  1672. return &AutoConstantDictionary[i];
  1673. else
  1674. return 0;
  1675. }
  1676. //-----------------------------------------------------------------------
  1677. const GpuProgramParameters::AutoConstantDefinition*
  1678. GpuProgramParameters::getAutoConstantDefinition(const size_t idx)
  1679. {
  1680. if (idx < getNumAutoConstantDefinitions())
  1681. {
  1682. // verify index is equal to acType
  1683. // if they are not equal then the dictionary was not setup properly
  1684. assert(idx == static_cast<size_t>(AutoConstantDictionary[idx].acType));
  1685. return &AutoConstantDictionary[idx];
  1686. }
  1687. else
  1688. return 0;
  1689. }
  1690. //-----------------------------------------------------------------------
  1691. size_t GpuProgramParameters::getNumAutoConstantDefinitions(void)
  1692. {
  1693. return sizeof(AutoConstantDictionary)/sizeof(AutoConstantDefinition);
  1694. }
  1695. //-----------------------------------------------------------------------
  1696. void GpuProgramParameters::incPassIterationNumber(void)
  1697. {
  1698. if (mActivePassIterationIndex != std::numeric_limits<size_t>::max())
  1699. {
  1700. // This is a physical index
  1701. ++mFloatConstants[mActivePassIterationIndex];
  1702. }
  1703. }
  1704. //---------------------------------------------------------------------
  1705. void GpuProgramParameters::addSharedParameters(GpuSharedParametersPtr sharedParams)
  1706. {
  1707. if (!isUsingSharedParameters(sharedParams->getName()))
  1708. {
  1709. mSharedParamSets.push_back(GpuSharedParametersUsage(sharedParams, this));
  1710. }
  1711. }
  1712. //---------------------------------------------------------------------
  1713. void GpuProgramParameters::addSharedParameters(const String& sharedParamsName)
  1714. {
  1715. // TODO PORT - I don't think I'll be needing this. I dont plan on including GpuProgramManager in the port
  1716. //addSharedParameters(GpuProgramManager::getSingleton().getSharedParameters(sharedParamsName));
  1717. }
  1718. //---------------------------------------------------------------------
  1719. bool GpuProgramParameters::isUsingSharedParameters(const String& sharedParamsName) const
  1720. {
  1721. for (GpuSharedParamUsageList::const_iterator i = mSharedParamSets.begin();
  1722. i != mSharedParamSets.end(); ++i)
  1723. {
  1724. if (i->getName() == sharedParamsName)
  1725. return true;
  1726. }
  1727. return false;
  1728. }
  1729. //---------------------------------------------------------------------
  1730. void GpuProgramParameters::removeSharedParameters(const String& sharedParamsName)
  1731. {
  1732. for (GpuSharedParamUsageList::iterator i = mSharedParamSets.begin();
  1733. i != mSharedParamSets.end(); ++i)
  1734. {
  1735. if (i->getName() == sharedParamsName)
  1736. {
  1737. mSharedParamSets.erase(i);
  1738. break;
  1739. }
  1740. }
  1741. }
  1742. //---------------------------------------------------------------------
  1743. void GpuProgramParameters::removeAllSharedParameters()
  1744. {
  1745. mSharedParamSets.clear();
  1746. }
  1747. //---------------------------------------------------------------------
  1748. const GpuProgramParameters::GpuSharedParamUsageList&
  1749. GpuProgramParameters::getSharedParameters() const
  1750. {
  1751. return mSharedParamSets;
  1752. }
  1753. //---------------------------------------------------------------------
  1754. void GpuProgramParameters::_copySharedParams()
  1755. {
  1756. for (GpuSharedParamUsageList::iterator i = mSharedParamSets.begin();
  1757. i != mSharedParamSets.end(); ++i )
  1758. {
  1759. i->_copySharedParamsToTargetParams();
  1760. }
  1761. }
  1762. }