CmGpuProgramParams.h 41 KB

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