CmGpuProgramParams.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. /** Read a texture from the underlying texture
  532. array at the given physical index.
  533. @param physicalIndex The array position of the texture
  534. @param dest Reference of the texture to store
  535. */
  536. void _readTexture(size_t physicalIndex, TextureRef& dest);
  537. /** Write a 4-element floating-point parameter to the program directly to
  538. the underlying constants buffer.
  539. @note You can use these methods if you have already derived the physical
  540. constant buffer location, for a slight speed improvement over using
  541. the named / logical index versions.
  542. @param physicalIndex The physical buffer index at which to place the parameter
  543. @param vec The value to set
  544. @param count The number of floats to write; if for example
  545. the uniform constant 'slot' is smaller than a Vector4
  546. */
  547. void _writeRawConstant(size_t physicalIndex, const Vector4& vec,
  548. size_t count = 4);
  549. /** Write a single floating-point parameter to the program.
  550. @note You can use these methods if you have already derived the physical
  551. constant buffer location, for a slight speed improvement over using
  552. the named / logical index versions.
  553. @param physicalIndex The physical buffer index at which to place the parameter
  554. @param val The value to set
  555. */
  556. void _writeRawConstant(size_t physicalIndex, float val);
  557. /** Write a single integer parameter to the program.
  558. @note You can use these methods if you have already derived the physical
  559. constant buffer location, for a slight speed improvement over using
  560. the named / logical index versions.
  561. @param physicalIndex The physical buffer index at which to place the parameter
  562. @param val The value to set
  563. */
  564. void _writeRawConstant(size_t physicalIndex, int val);
  565. /** Write a 3-element floating-point parameter to the program via Vector3.
  566. @note You can use these methods if you have already derived the physical
  567. constant buffer location, for a slight speed improvement over using
  568. the named / logical index versions.
  569. @param physicalIndex The physical buffer index at which to place the parameter
  570. @param vec The value to set
  571. */
  572. void _writeRawConstant(size_t physicalIndex, const Vector3& vec);
  573. /** Write a 2-element floating-point parameter to the program via Vector2.
  574. @note You can use these methods if you have already derived the physical
  575. constant buffer location, for a slight speed improvement over using
  576. the named / logical index versions.
  577. @param physicalIndex The physical buffer index at which to place the parameter
  578. @param vec The value to set
  579. */
  580. void _writeRawConstant(size_t physicalIndex, const Vector2& vec);
  581. /** Write a Matrix4 parameter to the program.
  582. @note You can use these methods if you have already derived the physical
  583. constant buffer location, for a slight speed improvement over using
  584. the named / logical index versions.
  585. @param physicalIndex The physical buffer index at which to place the parameter
  586. @param m The value to set
  587. @param elementCount actual element count used with shader
  588. */
  589. void _writeRawConstant(size_t physicalIndex, const Matrix4& m, size_t elementCount);
  590. /** Write a list of Matrix4 parameters to the program.
  591. @note You can use these methods if you have already derived the physical
  592. constant buffer location, for a slight speed improvement over using
  593. the named / logical index versions.
  594. @param physicalIndex The physical buffer index at which to place the parameter
  595. @param numEntries Number of Matrix4 entries
  596. */
  597. void _writeRawConstant(size_t physicalIndex, const Matrix4* m, size_t numEntries);
  598. /** Write a Matrix3 parameter to the program.
  599. @note You can use these methods if you have already derived the physical
  600. constant buffer location, for a slight speed improvement over using
  601. the named / logical index versions.
  602. @param physicalIndex The physical buffer index at which to place the parameter
  603. @param m The value to set
  604. @param elementCount actual element count used with shader
  605. */
  606. void _writeRawConstant(size_t physicalIndex, const Matrix3& m, size_t elementCount);
  607. /** Write a ColourValue parameter to the program.
  608. @note You can use these methods if you have already derived the physical
  609. constant buffer location, for a slight speed improvement over using
  610. the named / logical index versions.
  611. @param physicalIndex The physical buffer index at which to place the parameter
  612. @param colour The value to set
  613. @param count The number of floats to write; if for example
  614. the uniform constant 'slot' is smaller than a Vector4
  615. */
  616. void _writeRawConstant(size_t physicalIndex, const Color& colour,
  617. size_t count = 4);
  618. /** Gets an iterator over the named GpuConstantDefinition instances as defined
  619. by the program for which these parameters exist.
  620. @note
  621. Only available if this parameters object has named parameters.
  622. */
  623. GpuConstantDefinitionIterator getConstantDefinitionIterator(void) const;
  624. /** Get a specific GpuConstantDefinition for a named parameter.
  625. @note
  626. Only available if this parameters object has named parameters.
  627. */
  628. const GpuConstantDefinition& getConstantDefinition(const String& name) const;
  629. /** Get the full list of GpuConstantDefinition instances.
  630. @note
  631. Only available if this parameters object has named parameters.
  632. */
  633. const GpuNamedConstants& getConstantDefinitions() const;
  634. /** Get the current list of mappings from low-level logical param indexes
  635. to physical buffer locations in the float buffer.
  636. @note
  637. Only applicable to low-level programs.
  638. */
  639. const GpuLogicalBufferStructPtr& getFloatLogicalBufferStruct() const { return mFloatLogicalToPhysical; }
  640. /** Retrieves the logical index relating to a physical index in the float
  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 getFloatLogicalIndexForPhysicalIndex(size_t physicalIndex);
  646. /** Retrieves the logical index relating to a physical index in the int
  647. buffer, for programs which support that (low-level programs and
  648. high-level programs which use logical parameter indexes).
  649. @returns std::numeric_limits<size_t>::max() if not found
  650. */
  651. size_t getIntLogicalIndexForPhysicalIndex(size_t physicalIndex);
  652. /** Get the current list of mappings from low-level logical param indexes
  653. to physical buffer locations in the integer buffer.
  654. @note
  655. Only applicable to low-level programs.
  656. */
  657. const GpuLogicalBufferStructPtr& getIntLogicalBufferStruct() const { return mIntLogicalToPhysical; }
  658. /// Get a reference to the list of float constants
  659. const FloatConstantList& getFloatConstantList() const { return mFloatConstants; }
  660. /// Get a pointer to the 'nth' item in the float buffer
  661. float* getFloatPointer(size_t pos) { return &mFloatConstants[pos]; }
  662. /// Get a pointer to the 'nth' item in the float buffer
  663. const float* getFloatPointer(size_t pos) const { return &mFloatConstants[pos]; }
  664. /// Get a reference to the list of int constants
  665. const IntConstantList& getIntConstantList() const { return mIntConstants; }
  666. /// Get a pointer to the 'nth' item in the int buffer
  667. int* getIntPointer(size_t pos) { return &mIntConstants[pos]; }
  668. /// Get a pointer to the 'nth' item in the int buffer
  669. const int* getIntPointer(size_t pos) const { return &mIntConstants[pos]; }
  670. const GpuLogicalBufferStructPtr& getSamplerLogicalBufferStruct() const { return mSamplerLogicalToPhysical; }
  671. TextureRef getTexture(size_t pos) const { return mTextures[pos];}
  672. /// Get a reference to the list of textures
  673. const TextureList& getTextureList() const { return mTextures; }
  674. /** Tells the program whether to ignore missing parameters or not.
  675. */
  676. void setIgnoreMissingParams(bool state) { mIgnoreMissingParams = state; }
  677. /** Sets a texture parameter to the program.
  678. @remarks
  679. Different types of GPU programs support different types of constant parameters.
  680. For example, it's relatively common to find that vertex programs only support
  681. floating point constants, and that fragment programs only support integer (fixed point)
  682. parameters. This can vary depending on the program version supported by the
  683. graphics card being used. You should consult the documentation for the type of
  684. low level program you are using, or alternatively use the methods
  685. provided on RenderSystemCapabilities to determine the options.
  686. @par
  687. Another possible limitation is that some systems only allow constants to be set
  688. on certain boundaries, e.g. in sets of 4 values for example. Again, see
  689. RenderSystemCapabilities for full details.
  690. @note
  691. This named option will only work if you are using a parameters object created
  692. from a high-level program (HighLevelGpuProgram).
  693. @param name The name of the parameter
  694. @param val The value to set
  695. */
  696. void setNamedConstant(const String& name, TextureRef val);
  697. /** Sets a single value constant floating-point parameter to the program.
  698. @remarks
  699. Different types of GPU programs support different types of constant parameters.
  700. For example, it's relatively common to find that vertex programs only support
  701. floating point constants, and that fragment programs only support integer (fixed point)
  702. parameters. This can vary depending on the program version supported by the
  703. graphics card being used. You should consult the documentation for the type of
  704. low level program you are using, or alternatively use the methods
  705. provided on RenderSystemCapabilities to determine the options.
  706. @par
  707. Another possible limitation is that some systems only allow constants to be set
  708. on certain boundaries, e.g. in sets of 4 values for example. Again, see
  709. RenderSystemCapabilities for full details.
  710. @note
  711. This named option will only work if you are using a parameters object created
  712. from a high-level program (HighLevelGpuProgram).
  713. @param name The name of the parameter
  714. @param val The value to set
  715. */
  716. void setNamedConstant(const String& name, float val);
  717. /** Sets a single value constant integer parameter to the program.
  718. @remarks
  719. Different types of GPU programs support different types of constant parameters.
  720. For example, it's relatively common to find that vertex programs only support
  721. floating point constants, and that fragment programs only support integer (fixed point)
  722. parameters. This can vary depending on the program version supported by the
  723. graphics card being used. You should consult the documentation for the type of
  724. low level program you are using, or alternatively use the methods
  725. provided on RenderSystemCapabilities to determine the options.
  726. @par
  727. Another possible limitation is that some systems only allow constants to be set
  728. on certain boundaries, e.g. in sets of 4 values for example. Again, see
  729. RenderSystemCapabilities for full details.
  730. @note
  731. This named option will only work if you are using a parameters object created
  732. from a high-level program (HighLevelGpuProgram).
  733. @param name The name of the parameter
  734. @param val The value to set
  735. */
  736. void setNamedConstant(const String& name, int val);
  737. /** Sets a Vector4 parameter to the program.
  738. @param name The name of the parameter
  739. @param vec The value to set
  740. */
  741. void setNamedConstant(const String& name, const Vector4& vec);
  742. /** Sets a Vector3 parameter to the program.
  743. @note
  744. This named option will only work if you are using a parameters object created
  745. from a high-level program (HighLevelGpuProgram).
  746. @param index The index at which to place the parameter
  747. NB this index refers to the number of floats, so a Vector3 is 3. Note that many
  748. rendersystems & programs assume that every floating point parameter is passed in
  749. as a vector of 4 items, so you are strongly advised to check with
  750. RenderSystemCapabilities before using this version - if in doubt use Vector4
  751. or ColourValue instead (both are 4D).
  752. @param vec The value to set
  753. */
  754. void setNamedConstant(const String& name, const Vector3& vec);
  755. /** Sets a Vector2 parameter to the program.
  756. @note
  757. This named option will only work if you are using a parameters object created
  758. from a high-level program (HighLevelGpuProgram).
  759. @param vec The value to set
  760. */
  761. void setNamedConstant(const String& name, const Vector2& vec);
  762. /** Sets a Matrix4 parameter to the program.
  763. @param name The name of the parameter
  764. @param m The value to set
  765. */
  766. void setNamedConstant(const String& name, const Matrix4& m);
  767. /** Sets a list of Matrix4 parameters to the program.
  768. @param name The name of the parameter; this must be the first index of an array,
  769. for examples 'matrices[0]'
  770. NB since a Matrix4 is 16 floats long, so each entry will take up 4 indexes.
  771. @param m Pointer to an array of matrices to set
  772. @param numEntries Number of Matrix4 entries
  773. */
  774. void setNamedConstant(const String& name, const Matrix4* m, size_t numEntries);
  775. /** Sets a Matrix3 parameter to the program.
  776. @param name The name of the parameter
  777. @param m The value to set
  778. */
  779. void setNamedConstant(const String& name, const Matrix3& m);
  780. /** Sets a multiple value constant floating-point parameter to the program.
  781. @par
  782. Some systems only allow constants to be set on certain boundaries,
  783. e.g. in sets of 4 values for example. The 'multiple' parameter allows
  784. you to control that although you should only change it if you know
  785. your chosen language supports that (at the time of writing, only
  786. GLSL allows constants which are not a multiple of 4).
  787. @note
  788. This named option will only work if you are using a parameters object created
  789. from a high-level program (HighLevelGpuProgram).
  790. @param name The name of the parameter
  791. @param val Pointer to the values to write
  792. @param count The number of 'multiples' of floats to write
  793. @param multiple The number of raw entries in each element to write,
  794. the default is 4 so count = 1 would write 4 floats.
  795. */
  796. void setNamedConstant(const String& name, const float *val, size_t count,
  797. size_t multiple = 4);
  798. /** Sets a multiple value constant floating-point parameter to the program.
  799. @par
  800. Some systems only allow constants to be set on certain boundaries,
  801. e.g. in sets of 4 values for example. The 'multiple' parameter allows
  802. you to control that although you should only change it if you know
  803. your chosen language supports that (at the time of writing, only
  804. GLSL allows constants which are not a multiple of 4).
  805. @note
  806. This named option will only work if you are using a parameters object created
  807. from a high-level program (HighLevelGpuProgram).
  808. @param name The name of the parameter
  809. @param val Pointer to the values to write
  810. @param count The number of 'multiples' of floats to write
  811. @param multiple The number of raw entries in each element to write,
  812. the default is 4 so count = 1 would write 4 floats.
  813. */
  814. void setNamedConstant(const String& name, const double *val, size_t count,
  815. size_t multiple = 4);
  816. /** Sets a ColourValue parameter to the program.
  817. @param name The name of the parameter
  818. @param colour The value to set
  819. */
  820. void setNamedConstant(const String& name, const Color& colour);
  821. /** Sets a multiple value constant floating-point parameter to the program.
  822. @par
  823. Some systems only allow constants to be set on certain boundaries,
  824. e.g. in sets of 4 values for example. The 'multiple' parameter allows
  825. you to control that although you should only change it if you know
  826. your chosen language supports that (at the time of writing, only
  827. GLSL allows constants which are not a multiple of 4).
  828. @note
  829. This named option will only work if you are using a parameters object created
  830. from a high-level program (HighLevelGpuProgram).
  831. @param name The name of the parameter
  832. @param val Pointer to the values to write
  833. @param count The number of 'multiples' of floats to write
  834. @param multiple The number of raw entries in each element to write,
  835. the default is 4 so count = 1 would write 4 floats.
  836. */
  837. void setNamedConstant(const String& name, const int *val, size_t count,
  838. size_t multiple = 4);
  839. /**
  840. * @brief Returns true if a named constant with the specified name exists.
  841. */
  842. bool hasNamedConstant(const String& name) const;
  843. /** Find a constant definition for a named parameter.
  844. @remarks
  845. This method returns null if the named parameter did not exist, unlike
  846. getConstantDefinition which is more strict; unless you set the
  847. last parameter to true.
  848. @param name The name to look up
  849. @param throwExceptionIfMissing If set to true, failure to find an entry
  850. will throw an exception.
  851. */
  852. const GpuConstantDefinition* _findNamedConstantDefinition(
  853. const String& name, bool throwExceptionIfMissing = false) const;
  854. /** Gets the physical buffer index associated with a logical float constant index.
  855. @note Only applicable to low-level programs.
  856. @param logicalIndex The logical parameter index
  857. @param requestedSize The requested size - pass 0 to ignore missing entries
  858. and return std::numeric_limits<size_t>::max()
  859. */
  860. size_t _getFloatConstantPhysicalIndex(size_t logicalIndex, size_t requestedSize, UINT16 variability);
  861. /** Gets the physical buffer index associated with a logical int constant index.
  862. @note Only applicable to low-level programs.
  863. @param logicalIndex The logical parameter index
  864. @param requestedSize The requested size - pass 0 to ignore missing entries
  865. and return std::numeric_limits<size_t>::max()
  866. */
  867. size_t _getIntConstantPhysicalIndex(size_t logicalIndex, size_t requestedSize, UINT16 variability);
  868. /** Sets whether or not we need to transpose the matrices passed in from the rest of OGRE.
  869. @remarks
  870. D3D uses transposed matrices compared to GL and OGRE; this is not important when you
  871. use programs which are written to process row-major matrices, such as those generated
  872. by Cg, but if you use a program written to D3D's matrix layout you will need to enable
  873. this flag.
  874. */
  875. void setTransposeMatrices(bool val) { mTransposeMatrices = val; }
  876. /// Gets whether or not matrices are to be transposed when set
  877. bool getTransposeMatrices(void) const { return mTransposeMatrices; }
  878. /** Copies the values of all constants (including auto constants) from another
  879. GpuProgramParameters object.
  880. @note This copes the internal storage of the paarameters object and therefore
  881. can only be used for parameters objects created from the same GpuProgram.
  882. To merge parameters that match from different programs, use copyMatchingNamedConstantsFrom.
  883. */
  884. void copyConstantsFrom(const GpuProgramParameters& source);
  885. /** increments the multipass number entry by 1 if it exists
  886. */
  887. void incPassIterationNumber(void);
  888. /** Does this parameters object have a pass iteration number constant? */
  889. bool hasPassIterationNumber() const
  890. { return mActivePassIterationIndex != (std::numeric_limits<size_t>::max)(); }
  891. /** Get the physical buffer index of the pass iteration number constant */
  892. size_t getPassIterationNumberIndex() const
  893. { return mActivePassIterationIndex; }
  894. };
  895. /// Shared pointer used to hold references to GpuProgramParameters instances
  896. typedef std::shared_ptr<GpuProgramParameters> GpuProgramParametersSharedPtr;
  897. /** @} */
  898. /** @} */
  899. }
  900. #endif