CmGpuProgramParams.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. #ifndef __GpuProgramParams_H_
  25. #define __GpuProgramParams_H_
  26. // Precompiler options
  27. #include "CmPrerequisites.h"
  28. #include "CmRenderOperation.h"
  29. namespace CamelotEngine {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup Materials
  34. * @{
  35. */
  36. /** Enumeration of the types of constant we may encounter in programs.
  37. @note Low-level programs, by definition, will always use either
  38. float4 or int4 constant types since that is the fundamental underlying
  39. type in assembler.
  40. */
  41. enum GpuConstantType
  42. {
  43. GCT_FLOAT1 = 1,
  44. GCT_FLOAT2 = 2,
  45. GCT_FLOAT3 = 3,
  46. GCT_FLOAT4 = 4,
  47. GCT_SAMPLER1D = 5,
  48. GCT_SAMPLER2D = 6,
  49. GCT_SAMPLER3D = 7,
  50. GCT_SAMPLERCUBE = 8,
  51. GCT_SAMPLER1DSHADOW = 9,
  52. GCT_SAMPLER2DSHADOW = 10,
  53. GCT_MATRIX_2X2 = 11,
  54. GCT_MATRIX_2X3 = 12,
  55. GCT_MATRIX_2X4 = 13,
  56. GCT_MATRIX_3X2 = 14,
  57. GCT_MATRIX_3X3 = 15,
  58. GCT_MATRIX_3X4 = 16,
  59. GCT_MATRIX_4X2 = 17,
  60. GCT_MATRIX_4X3 = 18,
  61. GCT_MATRIX_4X4 = 19,
  62. GCT_INT1 = 20,
  63. GCT_INT2 = 21,
  64. GCT_INT3 = 22,
  65. GCT_INT4 = 23,
  66. GCT_UNKNOWN = 99
  67. };
  68. /** The variability of a GPU parameter, as derived from auto-params targetting it.
  69. These values must be powers of two since they are used in masks.
  70. */
  71. enum GpuParamVariability
  72. {
  73. /// No variation except by manual setting - the default
  74. GPV_GLOBAL = 1,
  75. /// Varies per object (based on an auto param usually), but not per light setup
  76. GPV_PER_OBJECT = 2,
  77. /// Varies with light setup
  78. GPV_LIGHTS = 4,
  79. /// Varies with pass iteration number
  80. GPV_PASS_ITERATION_NUMBER = 8,
  81. /// Full mask (16-bit)
  82. GPV_ALL = 0xFFFF
  83. };
  84. /** Information about predefined program constants.
  85. @note Only available for high-level programs but is referenced generically
  86. by GpuProgramParameters.
  87. */
  88. struct CM_EXPORT GpuConstantDefinition
  89. {
  90. /// Data type
  91. GpuConstantType constType;
  92. /// Physical start index in buffer (either float or int buffer)
  93. size_t physicalIndex;
  94. /// Logical index - used to communicate this constant to the rendersystem
  95. size_t logicalIndex;
  96. /** Number of raw buffer slots per element
  97. (some programs pack each array element to float4, some do not) */
  98. size_t elementSize;
  99. /// Length of array
  100. size_t arraySize;
  101. /// How this parameter varies (bitwise combination of GpuProgramVariability)
  102. mutable UINT16 variability;
  103. bool isFloat() const
  104. {
  105. return isFloat(constType);
  106. }
  107. static bool isFloat(GpuConstantType c)
  108. {
  109. switch(c)
  110. {
  111. case GCT_INT1:
  112. case GCT_INT2:
  113. case GCT_INT3:
  114. case GCT_INT4:
  115. case GCT_SAMPLER1D:
  116. case GCT_SAMPLER2D:
  117. case GCT_SAMPLER3D:
  118. case GCT_SAMPLERCUBE:
  119. case GCT_SAMPLER1DSHADOW:
  120. case GCT_SAMPLER2DSHADOW:
  121. return false;
  122. default:
  123. return true;
  124. };
  125. }
  126. bool isSampler() const
  127. {
  128. return isSampler(constType);
  129. }
  130. static bool isSampler(GpuConstantType c)
  131. {
  132. switch(c)
  133. {
  134. case GCT_SAMPLER1D:
  135. case GCT_SAMPLER2D:
  136. case GCT_SAMPLER3D:
  137. case GCT_SAMPLERCUBE:
  138. case GCT_SAMPLER1DSHADOW:
  139. case GCT_SAMPLER2DSHADOW:
  140. return true;
  141. default:
  142. return false;
  143. };
  144. }
  145. /** Get the element size of a given type, including whether to pad the
  146. elements into multiples of 4 (e.g. SM1 and D3D does, GLSL doesn't)
  147. */
  148. static size_t getElementSize(GpuConstantType ctype, bool padToMultiplesOf4)
  149. {
  150. if (padToMultiplesOf4)
  151. {
  152. switch(ctype)
  153. {
  154. case GCT_FLOAT1:
  155. case GCT_INT1:
  156. case GCT_SAMPLER1D:
  157. case GCT_SAMPLER2D:
  158. case GCT_SAMPLER3D:
  159. case GCT_SAMPLERCUBE:
  160. case GCT_SAMPLER1DSHADOW:
  161. case GCT_SAMPLER2DSHADOW:
  162. case GCT_FLOAT2:
  163. case GCT_INT2:
  164. case GCT_FLOAT3:
  165. case GCT_INT3:
  166. case GCT_FLOAT4:
  167. case GCT_INT4:
  168. return 4;
  169. case GCT_MATRIX_2X2:
  170. case GCT_MATRIX_2X3:
  171. case GCT_MATRIX_2X4:
  172. return 8; // 2 float4s
  173. case GCT_MATRIX_3X2:
  174. case GCT_MATRIX_3X3:
  175. case GCT_MATRIX_3X4:
  176. return 12; // 3 float4s
  177. case GCT_MATRIX_4X2:
  178. case GCT_MATRIX_4X3:
  179. case GCT_MATRIX_4X4:
  180. return 16; // 4 float4s
  181. default:
  182. return 4;
  183. };
  184. }
  185. else
  186. {
  187. switch(ctype)
  188. {
  189. case GCT_FLOAT1:
  190. case GCT_INT1:
  191. case GCT_SAMPLER1D:
  192. case GCT_SAMPLER2D:
  193. case GCT_SAMPLER3D:
  194. case GCT_SAMPLERCUBE:
  195. case GCT_SAMPLER1DSHADOW:
  196. case GCT_SAMPLER2DSHADOW:
  197. return 1;
  198. case GCT_FLOAT2:
  199. case GCT_INT2:
  200. return 2;
  201. case GCT_FLOAT3:
  202. case GCT_INT3:
  203. return 3;
  204. case GCT_FLOAT4:
  205. case GCT_INT4:
  206. return 4;
  207. case GCT_MATRIX_2X2:
  208. return 4;
  209. case GCT_MATRIX_2X3:
  210. case GCT_MATRIX_3X2:
  211. return 6;
  212. case GCT_MATRIX_2X4:
  213. case GCT_MATRIX_4X2:
  214. return 8;
  215. case GCT_MATRIX_3X3:
  216. return 9;
  217. case GCT_MATRIX_3X4:
  218. case GCT_MATRIX_4X3:
  219. return 12;
  220. case GCT_MATRIX_4X4:
  221. return 16;
  222. default:
  223. return 4;
  224. };
  225. }
  226. }
  227. GpuConstantDefinition()
  228. : constType(GCT_UNKNOWN)
  229. , physicalIndex((std::numeric_limits<size_t>::max)())
  230. , elementSize(0)
  231. , arraySize(1)
  232. , variability(GPV_GLOBAL) {}
  233. };
  234. typedef map<String, GpuConstantDefinition>::type GpuConstantDefinitionMap;
  235. typedef GpuConstantDefinitionMap::const_iterator GpuConstantDefinitionIterator;
  236. /// Struct collecting together the information for named constants.
  237. struct CM_EXPORT GpuNamedConstants
  238. {
  239. /// Total size of the float buffer required
  240. size_t floatBufferSize;
  241. /// Total size of the int buffer required
  242. size_t intBufferSize;
  243. /// Total number of samplers referenced
  244. size_t samplerCount;
  245. /// Map of parameter names to GpuConstantDefinition
  246. GpuConstantDefinitionMap map;
  247. GpuNamedConstants() : floatBufferSize(0), intBufferSize(0), samplerCount(0) {}
  248. /** Generate additional constant entries for arrays based on a base definition.
  249. @remarks
  250. Array uniforms will be added just with their base name with no array
  251. suffix. This method will add named entries for array suffixes too
  252. so individual array entries can be addressed. Note that we only
  253. individually index array elements if the array size is up to 16
  254. entries in size. Anything larger than that only gets a [0] entry
  255. as well as the main entry, to save cluttering up the name map. After
  256. all, you can address the larger arrays in a bulk fashion much more
  257. easily anyway.
  258. */
  259. void generateConstantDefinitionArrayEntries(const String& paramName,
  260. const GpuConstantDefinition& baseDef);
  261. /// Indicates whether all array entries will be generated and added to the definitions map
  262. static bool getGenerateAllConstantDefinitionArrayEntries();
  263. /** Sets whether all array entries will be generated and added to the definitions map.
  264. @remarks
  265. Usually, array entries can only be individually indexed if they're up to 16 entries long,
  266. to save memory - arrays larger than that can be set but only via the bulk setting
  267. methods. This option allows you to choose to individually index every array entry.
  268. */
  269. static void setGenerateAllConstantDefinitionArrayEntries(bool generateAll);
  270. protected:
  271. /** Indicates whether all array entries will be generated and added to the definitions map
  272. @remarks
  273. Normally, the number of array entries added to the definitions map is capped at 16
  274. to save memory. Setting this value to <code>true</code> allows all of the entries
  275. to be generated and added to the map.
  276. */
  277. static bool msGenerateAllConstantDefinitionArrayEntries;
  278. };
  279. typedef std::shared_ptr<GpuNamedConstants> GpuNamedConstantsPtr;
  280. /** Structure recording the use of a physical buffer by a logical parameter
  281. index. Only used for low-level programs.
  282. */
  283. struct CM_EXPORT GpuLogicalIndexUse
  284. {
  285. /// Physical buffer index
  286. size_t physicalIndex;
  287. /// Current physical size allocation
  288. size_t currentSize;
  289. /// How the contents of this slot vary
  290. mutable UINT16 variability;
  291. GpuLogicalIndexUse()
  292. : physicalIndex(99999), currentSize(0), variability(GPV_GLOBAL) {}
  293. GpuLogicalIndexUse(size_t bufIdx, size_t curSz, UINT16 v)
  294. : physicalIndex(bufIdx), currentSize(curSz), variability(v) {}
  295. };
  296. typedef map<size_t, GpuLogicalIndexUse>::type GpuLogicalIndexUseMap;
  297. /// Container struct to allow params to safely & update shared list of logical buffer assignments
  298. struct CM_EXPORT GpuLogicalBufferStruct
  299. {
  300. CM_MUTEX(mutex)
  301. /// Map from logical index to physical buffer location
  302. GpuLogicalIndexUseMap map;
  303. /// Shortcut to know the buffer size needs
  304. size_t bufferSize;
  305. GpuLogicalBufferStruct() : bufferSize(0) {}
  306. };
  307. typedef std::shared_ptr<GpuLogicalBufferStruct> GpuLogicalBufferStructPtr;
  308. /** Definition of container that holds the current float constants.
  309. @note Not necessarily in direct index order to constant indexes, logical
  310. to physical index map is derived from GpuProgram
  311. */
  312. typedef vector<float>::type FloatConstantList;
  313. /** Definition of container that holds the current float constants.
  314. @note Not necessarily in direct index order to constant indexes, logical
  315. to physical index map is derived from GpuProgram
  316. */
  317. typedef vector<int>::type IntConstantList;
  318. /** Definition of container that holds the current float constants.
  319. @note Not necessarily in direct index order to constant indexes, logical
  320. to physical index map is derived from GpuProgram
  321. */
  322. typedef vector<TextureRef>::type TextureList;
  323. /** Collects together the program parameters used for a GpuProgram.
  324. @remarks
  325. Gpu program state includes constant parameters used by the program, and
  326. bindings to render system state which is propagated into the constants
  327. by the engine automatically if requested.
  328. @par
  329. GpuProgramParameters objects should be created through the GpuProgram and
  330. may be shared between multiple Pass instances. For this reason they
  331. are managed using a shared pointer, which will ensure they are automatically
  332. deleted when no Pass is using them anymore.
  333. @par
  334. High-level programs use named parameters (uniforms), low-level programs
  335. use indexed constants. This class supports both, but you can tell whether
  336. named constants are supported by calling hasNamedParameters(). There are
  337. references in the documentation below to 'logical' and 'physical' indexes;
  338. logical indexes are the indexes used by low-level programs and represent
  339. indexes into an array of float4's, some of which may be settable, some of
  340. which may be predefined constants in the program. We only store those
  341. constants which have actually been set, therefore our buffer could have
  342. gaps if we used the logical indexes in our own buffers. So instead we map
  343. these logical indexes to physical indexes in our buffer. When using
  344. high-level programs, logical indexes don't necessarily exist, although they
  345. might if the high-level program has a direct, exposed mapping from parameter
  346. names to logical indexes. In addition, high-level languages may or may not pack
  347. arrays of elements that are smaller than float4 (e.g. float2/vec2) contiguously.
  348. This kind of information is held in the ConstantDefinition structure which
  349. is only populated for high-level programs. You don't have to worry about
  350. any of this unless you intend to read parameters back from this structure
  351. rather than just setting them.
  352. */
  353. class CM_EXPORT GpuProgramParameters
  354. {
  355. public:
  356. /** Defines the type of the extra data item used by the auto constant.
  357. */
  358. enum ACDataType {
  359. /// no data is required
  360. ACDT_NONE,
  361. /// the auto constant requires data of type int
  362. ACDT_INT,
  363. /// the auto constant requires data of type real
  364. ACDT_REAL
  365. };
  366. /** Defines the base element type of the auto constant
  367. */
  368. enum ElementType {
  369. ET_INT,
  370. ET_REAL
  371. };
  372. protected:
  373. /// Packed list of floating-point constants (physical indexing)
  374. FloatConstantList mFloatConstants;
  375. /// Packed list of integer constants (physical indexing)
  376. IntConstantList mIntConstants;
  377. /// List of all texture parameters
  378. TextureList mTextures;
  379. /** Logical index to physical index map - for low-level programs
  380. or high-level programs which pass params this way. */
  381. GpuLogicalBufferStructPtr mFloatLogicalToPhysical;
  382. /** Logical index to physical index map - for low-level programs
  383. or high-level programs which pass params this way. */
  384. GpuLogicalBufferStructPtr mIntLogicalToPhysical;
  385. /** Logical index to physical index map - for low-level programs
  386. or high-level programs which pass params this way. */
  387. GpuLogicalBufferStructPtr mSamplerLogicalToPhysical;
  388. /// Mapping from parameter names to def - high-level programs are expected to populate this
  389. GpuNamedConstantsPtr mNamedConstants;
  390. /// The combined variability masks of all parameters
  391. UINT16 mCombinedVariability;
  392. /// Do we need to transpose matrices?
  393. bool mTransposeMatrices;
  394. /// flag to indicate if names not found will be ignored
  395. bool mIgnoreMissingParams;
  396. /// physical index for active pass iteration parameter real constant entry;
  397. size_t mActivePassIterationIndex;
  398. /** Gets the low-level structure for a logical index.
  399. */
  400. GpuLogicalIndexUse* _getFloatConstantLogicalIndexUse(size_t logicalIndex, size_t requestedSize, UINT16 variability);
  401. /** Gets the physical buffer index associated with a logical int constant index.
  402. */
  403. GpuLogicalIndexUse* _getIntConstantLogicalIndexUse(size_t logicalIndex, size_t requestedSize, UINT16 variability);
  404. public:
  405. GpuProgramParameters();
  406. ~GpuProgramParameters() {}
  407. /// Copy constructor
  408. GpuProgramParameters(const GpuProgramParameters& oth);
  409. /// Operator = overload
  410. GpuProgramParameters& operator=(const GpuProgramParameters& oth);
  411. /** Internal method for providing a link to a name->definition map for parameters. */
  412. void _setNamedConstants(const GpuNamedConstantsPtr& constantmap);
  413. /** Internal method for providing a link to a logical index->physical index map for parameters. */
  414. void _setLogicalIndexes(const GpuLogicalBufferStructPtr& floatIndexMap,
  415. const GpuLogicalBufferStructPtr& intIndexMap, const GpuLogicalBufferStructPtr& samplerIndexMap);
  416. /// Does this parameter set include named parameters?
  417. bool hasNamedParameters() const { return mNamedConstants != nullptr; }
  418. /** Does this parameter set include logically indexed parameters?
  419. @note Not mutually exclusive with hasNamedParameters since some high-level
  420. programs still use logical indexes to set the parameters on the
  421. rendersystem.
  422. */
  423. bool hasLogicalIndexedParameters() const { return mFloatLogicalToPhysical != nullptr; }
  424. /** Sets a 4-element floating-point parameter to the program.
  425. @param index The logical constant index at which to place the parameter
  426. (each constant is a 4D float)
  427. @param vec The value to set
  428. */
  429. void setConstant(size_t index, const Vector4& vec);
  430. /** Sets a single floating-point parameter to the program.
  431. @note This is actually equivalent to calling
  432. setConstant(index Vector4(val, 0, 0, 0)) since all constants are 4D.
  433. @param index The logical constant index at which to place the parameter (each constant is
  434. a 4D float)
  435. @param val The value to set
  436. */
  437. void setConstant(size_t index, float val);
  438. /** Sets a 4-element floating-point parameter to the program via Vector3.
  439. @param index The logical constant index at which to place the parameter (each constant is
  440. a 4D float).
  441. Note that since you're passing a Vector3, the last element of the 4-element
  442. value will be set to 1 (a homogeneous vector)
  443. @param vec The value to set
  444. */
  445. void setConstant(size_t index, const Vector3& vec);
  446. /** Sets a Matrix4 parameter to the program.
  447. @param index The logical constant index at which to place the parameter (each constant is
  448. a 4D float).
  449. NB since a Matrix4 is 16 floats long, this parameter will take up 4 indexes.
  450. @param m The value to set
  451. */
  452. void setConstant(size_t index, const Matrix4& m);
  453. /** Sets a list of Matrix4 parameters to the program.
  454. @param index The logical constant index at which to start placing the parameter (each constant is
  455. a 4D float).
  456. NB since a Matrix4 is 16 floats long, so each entry will take up 4 indexes.
  457. @param m Pointer to an array of matrices to set
  458. @param numEntries Number of Matrix4 entries
  459. */
  460. void setConstant(size_t index, const Matrix4* m, size_t numEntries);
  461. /** Sets a multiple value constant floating-point parameter to the program.
  462. @param index The logical constant index at which to start placing parameters (each constant is
  463. a 4D float)
  464. @param val Pointer to the values to write, must contain 4*count floats
  465. @param count The number of groups of 4 floats to write
  466. */
  467. void setConstant(size_t index, const float *val, size_t count);
  468. /** Sets a multiple value constant floating-point parameter to the program.
  469. @param index The logical constant index at which to start placing parameters (each constant is
  470. a 4D float)
  471. @param val Pointer to the values to write, must contain 4*count floats
  472. @param count The number of groups of 4 floats to write
  473. */
  474. void setConstant(size_t index, const double *val, size_t count);
  475. /** Sets a ColourValue parameter to the program.
  476. @param index The logical constant index at which to place the parameter (each constant is
  477. a 4D float)
  478. @param colour The value to set
  479. */
  480. void setConstant(size_t index, const Color& colour);
  481. /** Sets a multiple value constant integer parameter to the program.
  482. @remarks
  483. Different types of GPU programs support different types of constant parameters.
  484. For example, it's relatively common to find that vertex programs only support
  485. floating point constants, and that fragment programs only support integer (fixed point)
  486. parameters. This can vary depending on the program version supported by the
  487. graphics card being used. You should consult the documentation for the type of
  488. low level program you are using, or alternatively use the methods
  489. provided on RenderSystemCapabilities to determine the options.
  490. @param index The logical constant index at which to place the parameter (each constant is
  491. a 4D integer)
  492. @param val Pointer to the values to write, must contain 4*count ints
  493. @param count The number of groups of 4 ints to write
  494. */
  495. void setConstant(size_t index, const int *val, size_t count);
  496. /** Write a series of floating point values into the underlying float
  497. constant buffer at the given physical index.
  498. @param physicalIndex The buffer position to start writing
  499. @param val Pointer to a list of values to write
  500. @param count The number of floats to write
  501. */
  502. void _writeRawConstants(size_t physicalIndex, const float* val, size_t count);
  503. /** Write a series of floating point values into the underlying float
  504. constant buffer at the given physical index.
  505. @param physicalIndex The buffer position to start writing
  506. @param val Pointer to a list of values to write
  507. @param count The number of floats to write
  508. */
  509. void _writeRawConstants(size_t physicalIndex, const double* val, size_t count);
  510. /** Write a series of integer values into the underlying integer
  511. constant buffer at the given physical index.
  512. @param physicalIndex The buffer position to start writing
  513. @param val Pointer to a list of values to write
  514. @param count The number of ints to write
  515. */
  516. void _writeRawConstants(size_t physicalIndex, const int* val, size_t count);
  517. /** Read a series of floating point values from the underlying float
  518. constant buffer at the given physical index.
  519. @param physicalIndex The buffer position to start reading
  520. @param count The number of floats to read
  521. @param dest Pointer to a buffer to receive the values
  522. */
  523. void _readRawConstants(size_t physicalIndex, size_t count, float* dest);
  524. /** Read a series of integer values from the underlying integer
  525. constant buffer at the given physical index.
  526. @param physicalIndex The buffer position to start reading
  527. @param count The number of ints to read
  528. @param dest Pointer to a buffer to receive the values
  529. */
  530. void _readRawConstants(size_t physicalIndex, size_t count, int* dest);
  531. /** Write a 4-element floating-point parameter to the program directly to
  532. the underlying constants buffer.
  533. @note You can use these methods if you have already derived the physical
  534. constant buffer location, for a slight speed improvement over using
  535. the named / logical index versions.
  536. @param physicalIndex The physical buffer index at which to place the parameter
  537. @param vec The value to set
  538. @param count The number of floats to write; if for example
  539. the uniform constant 'slot' is smaller than a Vector4
  540. */
  541. void _writeRawConstant(size_t physicalIndex, const Vector4& vec,
  542. size_t count = 4);
  543. /** Write a single floating-point parameter to the program.
  544. @note You can use these methods if you have already derived the physical
  545. constant buffer location, for a slight speed improvement over using
  546. the named / logical index versions.
  547. @param physicalIndex The physical buffer index at which to place the parameter
  548. @param val The value to set
  549. */
  550. void _writeRawConstant(size_t physicalIndex, float val);
  551. /** Write a single integer parameter to the program.
  552. @note You can use these methods if you have already derived the physical
  553. constant buffer location, for a slight speed improvement over using
  554. the named / logical index versions.
  555. @param physicalIndex The physical buffer index at which to place the parameter
  556. @param val The value to set
  557. */
  558. void _writeRawConstant(size_t physicalIndex, int val);
  559. /** Write a 3-element floating-point parameter to the program via Vector3.
  560. @note You can use these methods if you have already derived the physical
  561. constant buffer location, for a slight speed improvement over using
  562. the named / logical index versions.
  563. @param physicalIndex The physical buffer index at which to place the parameter
  564. @param vec The value to set
  565. */
  566. void _writeRawConstant(size_t physicalIndex, const Vector3& vec);
  567. /** Write a 2-element floating-point parameter to the program via Vector2.
  568. @note You can use these methods if you have already derived the physical
  569. constant buffer location, for a slight speed improvement over using
  570. the named / logical index versions.
  571. @param physicalIndex The physical buffer index at which to place the parameter
  572. @param vec The value to set
  573. */
  574. void _writeRawConstant(size_t physicalIndex, const Vector2& vec);
  575. /** Write a Matrix4 parameter to the program.
  576. @note You can use these methods if you have already derived the physical
  577. constant buffer location, for a slight speed improvement over using
  578. the named / logical index versions.
  579. @param physicalIndex The physical buffer index at which to place the parameter
  580. @param m The value to set
  581. @param elementCount actual element count used with shader
  582. */
  583. void _writeRawConstant(size_t physicalIndex, const Matrix4& m, size_t elementCount);
  584. /** Write a list of Matrix4 parameters to the program.
  585. @note You can use these methods if you have already derived the physical
  586. constant buffer location, for a slight speed improvement over using
  587. the named / logical index versions.
  588. @param physicalIndex The physical buffer index at which to place the parameter
  589. @param numEntries Number of Matrix4 entries
  590. */
  591. void _writeRawConstant(size_t physicalIndex, const Matrix4* m, size_t numEntries);
  592. /** Write a Matrix3 parameter to the program.
  593. @note You can use these methods if you have already derived the physical
  594. constant buffer location, for a slight speed improvement over using
  595. the named / logical index versions.
  596. @param physicalIndex The physical buffer index at which to place the parameter
  597. @param m The value to set
  598. @param elementCount actual element count used with shader
  599. */
  600. void _writeRawConstant(size_t physicalIndex, const Matrix3& m, size_t elementCount);
  601. /** Write a ColourValue parameter to the program.
  602. @note You can use these methods if you have already derived the physical
  603. constant buffer location, for a slight speed improvement over using
  604. the named / logical index versions.
  605. @param physicalIndex The physical buffer index at which to place the parameter
  606. @param colour The value to set
  607. @param count The number of floats to write; if for example
  608. the uniform constant 'slot' is smaller than a Vector4
  609. */
  610. void _writeRawConstant(size_t physicalIndex, const Color& colour,
  611. size_t count = 4);
  612. /** Gets an iterator over the named GpuConstantDefinition instances as defined
  613. by the program for which these parameters exist.
  614. @note
  615. Only available if this parameters object has named parameters.
  616. */
  617. GpuConstantDefinitionIterator getConstantDefinitionIterator(void) const;
  618. /** Get a specific GpuConstantDefinition for a named parameter.
  619. @note
  620. Only available if this parameters object has named parameters.
  621. */
  622. const GpuConstantDefinition& getConstantDefinition(const String& name) const;
  623. /** Get the full list of GpuConstantDefinition instances.
  624. @note
  625. Only available if this parameters object has named parameters.
  626. */
  627. const GpuNamedConstants& getConstantDefinitions() const;
  628. /** Get the current list of mappings from low-level logical param indexes
  629. to physical buffer locations in the float buffer.
  630. @note
  631. Only applicable to low-level programs.
  632. */
  633. const GpuLogicalBufferStructPtr& getFloatLogicalBufferStruct() const { return mFloatLogicalToPhysical; }
  634. /** Retrieves the logical index relating to a physical index in the float
  635. buffer, for programs which support that (low-level programs and
  636. high-level programs which use logical parameter indexes).
  637. @returns std::numeric_limits<size_t>::max() if not found
  638. */
  639. size_t getFloatLogicalIndexForPhysicalIndex(size_t physicalIndex);
  640. /** Retrieves the logical index relating to a physical index in the int
  641. buffer, for programs which support that (low-level programs and
  642. high-level programs which use logical parameter indexes).
  643. @returns std::numeric_limits<size_t>::max() if not found
  644. */
  645. size_t getIntLogicalIndexForPhysicalIndex(size_t physicalIndex);
  646. /** Get the current list of mappings from low-level logical param indexes
  647. to physical buffer locations in the integer buffer.
  648. @note
  649. Only applicable to low-level programs.
  650. */
  651. const GpuLogicalBufferStructPtr& getIntLogicalBufferStruct() const { return mIntLogicalToPhysical; }
  652. /// Get a reference to the list of float constants
  653. const FloatConstantList& getFloatConstantList() const { return mFloatConstants; }
  654. /// Get a pointer to the 'nth' item in the float buffer
  655. float* getFloatPointer(size_t pos) { return &mFloatConstants[pos]; }
  656. /// Get a pointer to the 'nth' item in the float buffer
  657. const float* getFloatPointer(size_t pos) const { return &mFloatConstants[pos]; }
  658. /// Get a reference to the list of int constants
  659. const IntConstantList& getIntConstantList() const { return mIntConstants; }
  660. /// Get a pointer to the 'nth' item in the int buffer
  661. int* getIntPointer(size_t pos) { return &mIntConstants[pos]; }
  662. /// Get a pointer to the 'nth' item in the int buffer
  663. const int* getIntPointer(size_t pos) const { return &mIntConstants[pos]; }
  664. const GpuLogicalBufferStructPtr& getSamplerLogicalBufferStruct() const { return mSamplerLogicalToPhysical; }
  665. TextureRef getTexture(size_t pos) const { return mTextures[pos];}
  666. /// Get a reference to the list of textures
  667. const TextureList& getTextureList() const { return mTextures; }
  668. /** Tells the program whether to ignore missing parameters or not.
  669. */
  670. void setIgnoreMissingParams(bool state) { mIgnoreMissingParams = state; }
  671. /** Sets a texture parameter to the program.
  672. @remarks
  673. Different types of GPU programs support different types of constant parameters.
  674. For example, it's relatively common to find that vertex programs only support
  675. floating point constants, and that fragment programs only support integer (fixed point)
  676. parameters. This can vary depending on the program version supported by the
  677. graphics card being used. You should consult the documentation for the type of
  678. low level program you are using, or alternatively use the methods
  679. provided on RenderSystemCapabilities to determine the options.
  680. @par
  681. Another possible limitation is that some systems only allow constants to be set
  682. on certain boundaries, e.g. in sets of 4 values for example. Again, see
  683. RenderSystemCapabilities for full details.
  684. @note
  685. This named option will only work if you are using a parameters object created
  686. from a high-level program (HighLevelGpuProgram).
  687. @param name The name of the parameter
  688. @param val The value to set
  689. */
  690. void setNamedConstant(const String& name, TextureRef val);
  691. /** Sets a single value constant floating-point parameter to the program.
  692. @remarks
  693. Different types of GPU programs support different types of constant parameters.
  694. For example, it's relatively common to find that vertex programs only support
  695. floating point constants, and that fragment programs only support integer (fixed point)
  696. parameters. This can vary depending on the program version supported by the
  697. graphics card being used. You should consult the documentation for the type of
  698. low level program you are using, or alternatively use the methods
  699. provided on RenderSystemCapabilities to determine the options.
  700. @par
  701. Another possible limitation is that some systems only allow constants to be set
  702. on certain boundaries, e.g. in sets of 4 values for example. Again, see
  703. RenderSystemCapabilities for full details.
  704. @note
  705. This named option will only work if you are using a parameters object created
  706. from a high-level program (HighLevelGpuProgram).
  707. @param name The name of the parameter
  708. @param val The value to set
  709. */
  710. void setNamedConstant(const String& name, float val);
  711. /** Sets a single value constant integer parameter to the program.
  712. @remarks
  713. Different types of GPU programs support different types of constant parameters.
  714. For example, it's relatively common to find that vertex programs only support
  715. floating point constants, and that fragment programs only support integer (fixed point)
  716. parameters. This can vary depending on the program version supported by the
  717. graphics card being used. You should consult the documentation for the type of
  718. low level program you are using, or alternatively use the methods
  719. provided on RenderSystemCapabilities to determine the options.
  720. @par
  721. Another possible limitation is that some systems only allow constants to be set
  722. on certain boundaries, e.g. in sets of 4 values for example. Again, see
  723. RenderSystemCapabilities for full details.
  724. @note
  725. This named option will only work if you are using a parameters object created
  726. from a high-level program (HighLevelGpuProgram).
  727. @param name The name of the parameter
  728. @param val The value to set
  729. */
  730. void setNamedConstant(const String& name, int val);
  731. /** Sets a Vector4 parameter to the program.
  732. @param name The name of the parameter
  733. @param vec The value to set
  734. */
  735. void setNamedConstant(const String& name, const Vector4& vec);
  736. /** Sets a Vector3 parameter to the program.
  737. @note
  738. This named option will only work if you are using a parameters object created
  739. from a high-level program (HighLevelGpuProgram).
  740. @param index The index at which to place the parameter
  741. NB this index refers to the number of floats, so a Vector3 is 3. Note that many
  742. rendersystems & programs assume that every floating point parameter is passed in
  743. as a vector of 4 items, so you are strongly advised to check with
  744. RenderSystemCapabilities before using this version - if in doubt use Vector4
  745. or ColourValue instead (both are 4D).
  746. @param vec The value to set
  747. */
  748. void setNamedConstant(const String& name, const Vector3& vec);
  749. /** Sets a Vector2 parameter to the program.
  750. @note
  751. This named option will only work if you are using a parameters object created
  752. from a high-level program (HighLevelGpuProgram).
  753. @param vec The value to set
  754. */
  755. void setNamedConstant(const String& name, const Vector2& vec);
  756. /** Sets a Matrix4 parameter to the program.
  757. @param name The name of the parameter
  758. @param m The value to set
  759. */
  760. void setNamedConstant(const String& name, const Matrix4& m);
  761. /** Sets a list of Matrix4 parameters to the program.
  762. @param name The name of the parameter; this must be the first index of an array,
  763. for examples 'matrices[0]'
  764. NB since a Matrix4 is 16 floats long, so each entry will take up 4 indexes.
  765. @param m Pointer to an array of matrices to set
  766. @param numEntries Number of Matrix4 entries
  767. */
  768. void setNamedConstant(const String& name, const Matrix4* m, size_t numEntries);
  769. /** Sets a Matrix3 parameter to the program.
  770. @param name The name of the parameter
  771. @param m The value to set
  772. */
  773. void setNamedConstant(const String& name, const Matrix3& m);
  774. /** Sets a multiple value constant floating-point parameter to the program.
  775. @par
  776. Some systems only allow constants to be set on certain boundaries,
  777. e.g. in sets of 4 values for example. The 'multiple' parameter allows
  778. you to control that although you should only change it if you know
  779. your chosen language supports that (at the time of writing, only
  780. GLSL allows constants which are not a multiple of 4).
  781. @note
  782. This named option will only work if you are using a parameters object created
  783. from a high-level program (HighLevelGpuProgram).
  784. @param name The name of the parameter
  785. @param val Pointer to the values to write
  786. @param count The number of 'multiples' of floats to write
  787. @param multiple The number of raw entries in each element to write,
  788. the default is 4 so count = 1 would write 4 floats.
  789. */
  790. void setNamedConstant(const String& name, const float *val, size_t count,
  791. size_t multiple = 4);
  792. /** Sets a multiple value constant floating-point parameter to the program.
  793. @par
  794. Some systems only allow constants to be set on certain boundaries,
  795. e.g. in sets of 4 values for example. The 'multiple' parameter allows
  796. you to control that although you should only change it if you know
  797. your chosen language supports that (at the time of writing, only
  798. GLSL allows constants which are not a multiple of 4).
  799. @note
  800. This named option will only work if you are using a parameters object created
  801. from a high-level program (HighLevelGpuProgram).
  802. @param name The name of the parameter
  803. @param val Pointer to the values to write
  804. @param count The number of 'multiples' of floats to write
  805. @param multiple The number of raw entries in each element to write,
  806. the default is 4 so count = 1 would write 4 floats.
  807. */
  808. void setNamedConstant(const String& name, const double *val, size_t count,
  809. size_t multiple = 4);
  810. /** Sets a ColourValue parameter to the program.
  811. @param name The name of the parameter
  812. @param colour The value to set
  813. */
  814. void setNamedConstant(const String& name, const Color& colour);
  815. /** Sets a multiple value constant floating-point parameter to the program.
  816. @par
  817. Some systems only allow constants to be set on certain boundaries,
  818. e.g. in sets of 4 values for example. The 'multiple' parameter allows
  819. you to control that although you should only change it if you know
  820. your chosen language supports that (at the time of writing, only
  821. GLSL allows constants which are not a multiple of 4).
  822. @note
  823. This named option will only work if you are using a parameters object created
  824. from a high-level program (HighLevelGpuProgram).
  825. @param name The name of the parameter
  826. @param val Pointer to the values to write
  827. @param count The number of 'multiples' of floats to write
  828. @param multiple The number of raw entries in each element to write,
  829. the default is 4 so count = 1 would write 4 floats.
  830. */
  831. void setNamedConstant(const String& name, const int *val, size_t count,
  832. size_t multiple = 4);
  833. /**
  834. * @brief Returns true if a named constant with the specified name exists.
  835. */
  836. bool hasNamedConstant(const String& name) const;
  837. /** Find a constant definition for a named parameter.
  838. @remarks
  839. This method returns null if the named parameter did not exist, unlike
  840. getConstantDefinition which is more strict; unless you set the
  841. last parameter to true.
  842. @param name The name to look up
  843. @param throwExceptionIfMissing If set to true, failure to find an entry
  844. will throw an exception.
  845. */
  846. const GpuConstantDefinition* _findNamedConstantDefinition(
  847. const String& name, bool throwExceptionIfMissing = false) const;
  848. /** Gets the physical buffer index associated with a logical float constant index.
  849. @note Only applicable to low-level programs.
  850. @param logicalIndex The logical parameter index
  851. @param requestedSize The requested size - pass 0 to ignore missing entries
  852. and return std::numeric_limits<size_t>::max()
  853. */
  854. size_t _getFloatConstantPhysicalIndex(size_t logicalIndex, size_t requestedSize, UINT16 variability);
  855. /** Gets the physical buffer index associated with a logical int constant index.
  856. @note Only applicable to low-level programs.
  857. @param logicalIndex The logical parameter index
  858. @param requestedSize The requested size - pass 0 to ignore missing entries
  859. and return std::numeric_limits<size_t>::max()
  860. */
  861. size_t _getIntConstantPhysicalIndex(size_t logicalIndex, size_t requestedSize, UINT16 variability);
  862. /** Sets whether or not we need to transpose the matrices passed in from the rest of OGRE.
  863. @remarks
  864. D3D uses transposed matrices compared to GL and OGRE; this is not important when you
  865. use programs which are written to process row-major matrices, such as those generated
  866. by Cg, but if you use a program written to D3D's matrix layout you will need to enable
  867. this flag.
  868. */
  869. void setTransposeMatrices(bool val) { mTransposeMatrices = val; }
  870. /// Gets whether or not matrices are to be transposed when set
  871. bool getTransposeMatrices(void) const { return mTransposeMatrices; }
  872. /** Copies the values of all constants (including auto constants) from another
  873. GpuProgramParameters object.
  874. @note This copes the internal storage of the paarameters object and therefore
  875. can only be used for parameters objects created from the same GpuProgram.
  876. To merge parameters that match from different programs, use copyMatchingNamedConstantsFrom.
  877. */
  878. void copyConstantsFrom(const GpuProgramParameters& source);
  879. /** increments the multipass number entry by 1 if it exists
  880. */
  881. void incPassIterationNumber(void);
  882. /** Does this parameters object have a pass iteration number constant? */
  883. bool hasPassIterationNumber() const
  884. { return mActivePassIterationIndex != (std::numeric_limits<size_t>::max)(); }
  885. /** Get the physical buffer index of the pass iteration number constant */
  886. size_t getPassIterationNumberIndex() const
  887. { return mActivePassIterationIndex; }
  888. };
  889. /// Shared pointer used to hold references to GpuProgramParameters instances
  890. typedef std::shared_ptr<GpuProgramParameters> GpuProgramParametersSharedPtr;
  891. /** @} */
  892. /** @} */
  893. }
  894. #endif