CmGpuProgramParams.cpp 79 KB

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