CmGpuProgramParams.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 "CmDrawOps.h"
  29. #include "CmSamplerState.h"
  30. namespace CamelotFramework {
  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_SAMPLER1D:
  154. case GCT_SAMPLER2D:
  155. case GCT_SAMPLER3D:
  156. case GCT_SAMPLERCUBE:
  157. case GCT_SAMPLER1DSHADOW:
  158. case GCT_SAMPLER2DSHADOW:
  159. return 1; // Samplers aren't like other variables so they won't be padded
  160. case GCT_FLOAT1:
  161. case GCT_INT1:
  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. case GCT_MATRIX_2X2:
  169. return 4;
  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<UINT32>::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. UINT32 floatBufferSize;
  241. /// Total size of the int buffer required
  242. UINT32 intBufferSize;
  243. /// Total number of samplers referenced
  244. UINT32 samplerCount;
  245. /// Total number of textures references
  246. UINT32 textureCount;
  247. /// Map of parameter names to GpuConstantDefinition
  248. GpuConstantDefinitionMap map;
  249. GpuNamedConstants() : floatBufferSize(0), intBufferSize(0),
  250. samplerCount(0), textureCount(0) {}
  251. /** Generate additional constant entries for arrays based on a base definition.
  252. @remarks
  253. Array uniforms will be added just with their base name with no array
  254. suffix. This method will add named entries for array suffixes too
  255. so individual array entries can be addressed. Note that we only
  256. individually index array elements if the array size is up to 16
  257. entries in size. Anything larger than that only gets a [0] entry
  258. as well as the main entry, to save cluttering up the name map. After
  259. all, you can address the larger arrays in a bulk fashion much more
  260. easily anyway.
  261. */
  262. void generateConstantDefinitionArrayEntries(const String& paramName,
  263. const GpuConstantDefinition& baseDef);
  264. /// Indicates whether all array entries will be generated and added to the definitions map
  265. static bool getGenerateAllConstantDefinitionArrayEntries();
  266. /** Sets whether all array entries will be generated and added to the definitions map.
  267. @remarks
  268. Usually, array entries can only be individually indexed if they're up to 16 entries long,
  269. to save memory - arrays larger than that can be set but only via the bulk setting
  270. methods. This option allows you to choose to individually index every array entry.
  271. */
  272. static void setGenerateAllConstantDefinitionArrayEntries(bool generateAll);
  273. protected:
  274. /** Indicates whether all array entries will be generated and added to the definitions map
  275. @remarks
  276. Normally, the number of array entries added to the definitions map is capped at 16
  277. to save memory. Setting this value to <code>true</code> allows all of the entries
  278. to be generated and added to the map.
  279. */
  280. static bool msGenerateAllConstantDefinitionArrayEntries;
  281. };
  282. typedef std::shared_ptr<GpuNamedConstants> GpuNamedConstantsPtr;
  283. /** Structure recording the use of a physical buffer by a logical parameter
  284. index. Only used for low-level programs.
  285. */
  286. struct CM_EXPORT GpuLogicalIndexUse
  287. {
  288. /// Physical buffer index
  289. UINT32 physicalIndex;
  290. /// Current physical size allocation
  291. UINT32 currentSize;
  292. /// How the contents of this slot vary
  293. mutable UINT16 variability;
  294. GpuLogicalIndexUse()
  295. : physicalIndex(99999), currentSize(0), variability(GPV_GLOBAL) {}
  296. GpuLogicalIndexUse(UINT32 bufIdx, UINT32 curSz, UINT32 v)
  297. : physicalIndex(bufIdx), currentSize(curSz), variability(v) {}
  298. };
  299. typedef Map<UINT32, GpuLogicalIndexUse>::type GpuLogicalIndexUseMap;
  300. /// Container struct to allow params to safely & update shared list of logical buffer assignments
  301. struct CM_EXPORT GpuLogicalBufferStruct
  302. {
  303. /// Map from logical index to physical buffer location
  304. GpuLogicalIndexUseMap map;
  305. /// Shortcut to know the buffer size needs
  306. UINT32 bufferSize;
  307. GpuLogicalBufferStruct() : bufferSize(0) {}
  308. };
  309. typedef std::shared_ptr<GpuLogicalBufferStruct> GpuLogicalBufferStructPtr;
  310. /** Definition of container that holds the current float constants.
  311. @note Not necessarily in direct index order to constant indexes, logical
  312. to physical index map is derived from GpuProgram
  313. */
  314. typedef Vector<float>::type FloatConstantList;
  315. /** Definition of container that holds the current float constants.
  316. @note Not necessarily in direct index order to constant indexes, logical
  317. to physical index map is derived from GpuProgram
  318. */
  319. typedef Vector<int>::type IntConstantList;
  320. /** Collects together the program parameters used for a GpuProgram.
  321. @remarks
  322. Gpu program state includes constant parameters used by the program, and
  323. bindings to render system state which is propagated into the constants
  324. by the engine automatically if requested.
  325. @par
  326. GpuProgramParameters objects should be created through the GpuProgram and
  327. may be shared between multiple Pass instances. For this reason they
  328. are managed using a shared pointer, which will ensure they are automatically
  329. deleted when no Pass is using them anymore.
  330. @par
  331. High-level programs use named parameters (uniforms), low-level programs
  332. use indexed constants. This class supports both, but you can tell whether
  333. named constants are supported by calling hasNamedParameters(). There are
  334. references in the documentation below to 'logical' and 'physical' indexes;
  335. logical indexes are the indexes used by low-level programs and represent
  336. indexes into an array of float4's, some of which may be settable, some of
  337. which may be predefined constants in the program. We only store those
  338. constants which have actually been set, therefore our buffer could have
  339. gaps if we used the logical indexes in our own buffers. So instead we map
  340. these logical indexes to physical indexes in our buffer. When using
  341. high-level programs, logical indexes don't necessarily exist, although they
  342. might if the high-level program has a direct, exposed mapping from parameter
  343. names to logical indexes. In addition, high-level languages may or may not pack
  344. arrays of elements that are smaller than float4 (e.g. float2/vec2) contiguously.
  345. This kind of information is held in the ConstantDefinition structure which
  346. is only populated for high-level programs. You don't have to worry about
  347. any of this unless you intend to read parameters back from this structure
  348. rather than just setting them.
  349. */
  350. class CM_EXPORT GpuProgramParameters
  351. {
  352. public:
  353. /** Defines the type of the extra data item used by the auto constant.
  354. */
  355. enum ACDataType {
  356. /// no data is required
  357. ACDT_NONE,
  358. /// the auto constant requires data of type int
  359. ACDT_INT,
  360. /// the auto constant requires data of type real
  361. ACDT_REAL
  362. };
  363. /** Defines the base element type of the auto constant
  364. */
  365. enum ElementType {
  366. ET_INT,
  367. ET_REAL
  368. };
  369. protected:
  370. /// Packed list of floating-point constants (physical indexing)
  371. FloatConstantList mFloatConstants;
  372. /// Packed list of integer constants (physical indexing)
  373. IntConstantList mIntConstants;
  374. /// List of all texture parameters
  375. Vector<HTexture>::type mTextures;
  376. // List of all sampler states
  377. Vector<SamplerStatePtr>::type mSamplerStates;
  378. /** Logical index to physical index map - for low-level programs
  379. or high-level programs which pass params this way. */
  380. GpuLogicalBufferStructPtr mFloatLogicalToPhysical;
  381. /** Logical index to physical index map - for low-level programs
  382. or high-level programs which pass params this way. */
  383. GpuLogicalBufferStructPtr mIntLogicalToPhysical;
  384. /** Logical index to physical index map - for low-level programs
  385. or high-level programs which pass params this way. */
  386. GpuLogicalBufferStructPtr mTextureLogicalToPhysical;
  387. /** Logical index to physical index map - for low-level programs
  388. or high-level programs which pass params this way. */
  389. GpuLogicalBufferStructPtr mSamplerLogicalToPhysical;
  390. /// Mapping from parameter names to def - high-level programs are expected to populate this
  391. GpuNamedConstantsPtr mNamedConstants;
  392. /// The combined variability masks of all parameters
  393. UINT16 mCombinedVariability;
  394. /// Do we need to transpose matrices?
  395. bool mTransposeMatrices;
  396. /// flag to indicate if names not found will be ignored
  397. bool mIgnoreMissingParams;
  398. /** Gets the low-level structure for a logical index.
  399. */
  400. GpuLogicalIndexUse* _getFloatConstantLogicalIndexUse(UINT32 logicalIndex, UINT32 requestedSize, UINT16 variability);
  401. /** Gets the physical buffer index associated with a logical int constant index.
  402. */
  403. GpuLogicalIndexUse* _getIntConstantLogicalIndexUse(UINT32 logicalIndex, UINT32 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,
  416. const GpuLogicalBufferStructPtr& samplerIndexMap,
  417. const GpuLogicalBufferStructPtr& textureIndexMap
  418. );
  419. /// Does this parameter set include named parameters?
  420. bool hasNamedParameters() const { return mNamedConstants != nullptr; }
  421. /** Does this parameter set include logically indexed parameters?
  422. @note Not mutually exclusive with hasNamedParameters since some high-level
  423. programs still use logical indexes to set the parameters on the
  424. rendersystem.
  425. */
  426. bool hasLogicalIndexedParameters() const { return mFloatLogicalToPhysical != nullptr; }
  427. /** Sets a 4-element floating-point parameter to the program.
  428. @param index The logical constant index at which to place the parameter
  429. (each constant is a 4D float)
  430. @param vec The value to set
  431. */
  432. void setConstant(UINT32 index, const Vector4& vec);
  433. /** Sets a single floating-point parameter to the program.
  434. @note This is actually equivalent to calling
  435. setConstant(index Vector4(val, 0, 0, 0)) since all constants are 4D.
  436. @param index The logical constant index at which to place the parameter (each constant is
  437. a 4D float)
  438. @param val The value to set
  439. */
  440. void setConstant(UINT32 index, float val);
  441. /** Sets a 4-element floating-point parameter to the program via Vector3.
  442. @param index The logical constant index at which to place the parameter (each constant is
  443. a 4D float).
  444. Note that since you're passing a Vector3, the last element of the 4-element
  445. value will be set to 1 (a homogeneous vector)
  446. @param vec The value to set
  447. */
  448. void setConstant(UINT32 index, const Vector3& vec);
  449. /** Sets a Matrix4 parameter to the program.
  450. @param index The logical constant index at which to place the parameter (each constant is
  451. a 4D float).
  452. NB since a Matrix4 is 16 floats long, this parameter will take up 4 indexes.
  453. @param m The value to set
  454. */
  455. void setConstant(UINT32 index, const Matrix4& m);
  456. /** Sets a list of Matrix4 parameters to the program.
  457. @param index The logical constant index at which to start placing the parameter (each constant is
  458. a 4D float).
  459. NB since a Matrix4 is 16 floats long, so each entry will take up 4 indexes.
  460. @param m Pointer to an array of matrices to set
  461. @param numEntries Number of Matrix4 entries
  462. */
  463. void setConstant(UINT32 index, const Matrix4* m, UINT32 numEntries);
  464. /** Sets a multiple value constant floating-point parameter to the program.
  465. @param index The logical constant index at which to start placing parameters (each constant is
  466. a 4D float)
  467. @param val Pointer to the values to write, must contain 4*count floats
  468. @param count The number of groups of 4 floats to write
  469. */
  470. void setConstant(UINT32 index, const float *val, UINT32 count);
  471. /** Sets a multiple value constant floating-point parameter to the program.
  472. @param index The logical constant index at which to start placing parameters (each constant is
  473. a 4D float)
  474. @param val Pointer to the values to write, must contain 4*count floats
  475. @param count The number of groups of 4 floats to write
  476. */
  477. void setConstant(UINT32 index, const double *val, UINT32 count);
  478. /** Sets a ColourValue parameter to the program.
  479. @param index The logical constant index at which to place the parameter (each constant is
  480. a 4D float)
  481. @param colour The value to set
  482. */
  483. void setConstant(UINT32 index, const Color& colour);
  484. /** Sets a multiple value constant integer parameter to the program.
  485. @remarks
  486. Different types of GPU programs support different types of constant parameters.
  487. For example, it's relatively common to find that vertex programs only support
  488. floating point constants, and that fragment programs only support integer (fixed point)
  489. parameters. This can vary depending on the program version supported by the
  490. graphics card being used. You should consult the documentation for the type of
  491. low level program you are using, or alternatively use the methods
  492. provided on RenderSystemCapabilities to determine the options.
  493. @param index The logical constant index at which to place the parameter (each constant is
  494. a 4D integer)
  495. @param val Pointer to the values to write, must contain 4*count ints
  496. @param count The number of groups of 4 ints to write
  497. */
  498. void setConstant(UINT32 index, const int *val, UINT32 count);
  499. /** Write a series of floating point values into the underlying float
  500. constant buffer at the given physical index.
  501. @param physicalIndex The buffer position to start writing
  502. @param val Pointer to a list of values to write
  503. @param count The number of floats to write
  504. */
  505. void _writeRawConstants(UINT32 physicalIndex, const float* val, UINT32 count);
  506. /** Write a series of floating point values into the underlying float
  507. constant buffer at the given physical index.
  508. @param physicalIndex The buffer position to start writing
  509. @param val Pointer to a list of values to write
  510. @param count The number of floats to write
  511. */
  512. void _writeRawConstants(UINT32 physicalIndex, const double* val, UINT32 count);
  513. /** Write a series of integer values into the underlying integer
  514. constant buffer at the given physical index.
  515. @param physicalIndex The buffer position to start writing
  516. @param val Pointer to a list of values to write
  517. @param count The number of ints to write
  518. */
  519. void _writeRawConstants(UINT32 physicalIndex, const int* val, UINT32 count);
  520. /** Read a series of floating point values from the underlying float
  521. constant buffer at the given physical index.
  522. @param physicalIndex The buffer position to start reading
  523. @param count The number of floats to read
  524. @param dest Pointer to a buffer to receive the values
  525. */
  526. void _readRawConstants(UINT32 physicalIndex, UINT32 count, float* dest);
  527. /** Read a series of integer values from the underlying integer
  528. constant buffer at the given physical index.
  529. @param physicalIndex The buffer position to start reading
  530. @param count The number of ints to read
  531. @param dest Pointer to a buffer to receive the values
  532. */
  533. void _readRawConstants(UINT32 physicalIndex, UINT32 count, int* dest);
  534. /** Read a texture from the underlying texture
  535. array at the given physical index.
  536. @param physicalIndex The array position of the texture
  537. @param dest Reference of the texture to store
  538. */
  539. void _readTexture(UINT32 physicalIndex, HTexture& dest);
  540. /** Write a 4-element floating-point parameter to the program directly to
  541. the underlying constants buffer.
  542. @note You can use these methods if you have already derived the physical
  543. constant buffer location, for a slight speed improvement over using
  544. the named / logical index versions.
  545. @param physicalIndex The physical buffer index at which to place the parameter
  546. @param vec The value to set
  547. @param count The number of floats to write; if for example
  548. the uniform constant 'slot' is smaller than a Vector4
  549. */
  550. void _writeRawConstant(UINT32 physicalIndex, const Vector4& vec,
  551. UINT32 count = 4);
  552. /** Write a single floating-point parameter to the program.
  553. @note You can use these methods if you have already derived the physical
  554. constant buffer location, for a slight speed improvement over using
  555. the named / logical index versions.
  556. @param physicalIndex The physical buffer index at which to place the parameter
  557. @param val The value to set
  558. */
  559. void _writeRawConstant(UINT32 physicalIndex, float val);
  560. /** Write a single integer parameter to the program.
  561. @note You can use these methods if you have already derived the physical
  562. constant buffer location, for a slight speed improvement over using
  563. the named / logical index versions.
  564. @param physicalIndex The physical buffer index at which to place the parameter
  565. @param val The value to set
  566. */
  567. void _writeRawConstant(UINT32 physicalIndex, int val);
  568. /** Write a 3-element floating-point parameter to the program via Vector3.
  569. @note You can use these methods if you have already derived the physical
  570. constant buffer location, for a slight speed improvement over using
  571. the named / logical index versions.
  572. @param physicalIndex The physical buffer index at which to place the parameter
  573. @param vec The value to set
  574. */
  575. void _writeRawConstant(UINT32 physicalIndex, const Vector3& vec);
  576. /** Write a 2-element floating-point parameter to the program via Vector2.
  577. @note You can use these methods if you have already derived the physical
  578. constant buffer location, for a slight speed improvement over using
  579. the named / logical index versions.
  580. @param physicalIndex The physical buffer index at which to place the parameter
  581. @param vec The value to set
  582. */
  583. void _writeRawConstant(UINT32 physicalIndex, const Vector2& vec);
  584. /** Write a Matrix4 parameter to the program.
  585. @note You can use these methods if you have already derived the physical
  586. constant buffer location, for a slight speed improvement over using
  587. the named / logical index versions.
  588. @param physicalIndex The physical buffer index at which to place the parameter
  589. @param m The value to set
  590. @param elementCount actual element count used with shader
  591. */
  592. void _writeRawConstant(UINT32 physicalIndex, const Matrix4& m, UINT32 elementCount);
  593. /** Write a list of Matrix4 parameters to the program.
  594. @note You can use these methods if you have already derived the physical
  595. constant buffer location, for a slight speed improvement over using
  596. the named / logical index versions.
  597. @param physicalIndex The physical buffer index at which to place the parameter
  598. @param numEntries Number of Matrix4 entries
  599. */
  600. void _writeRawConstant(UINT32 physicalIndex, const Matrix4* m, UINT32 numEntries);
  601. /** Write a Matrix3 parameter to the program.
  602. @note You can use these methods if you have already derived the physical
  603. constant buffer location, for a slight speed improvement over using
  604. the named / logical index versions.
  605. @param physicalIndex The physical buffer index at which to place the parameter
  606. @param m The value to set
  607. @param elementCount actual element count used with shader
  608. */
  609. void _writeRawConstant(UINT32 physicalIndex, const Matrix3& m, UINT32 elementCount);
  610. /** Write a ColourValue parameter to the program.
  611. @note You can use these methods if you have already derived the physical
  612. constant buffer location, for a slight speed improvement over using
  613. the named / logical index versions.
  614. @param physicalIndex The physical buffer index at which to place the parameter
  615. @param colour The value to set
  616. @param count The number of floats to write; if for example
  617. the uniform constant 'slot' is smaller than a Vector4
  618. */
  619. void _writeRawConstant(UINT32 physicalIndex, const Color& colour,
  620. UINT32 count = 4);
  621. /** Gets an iterator over the named GpuConstantDefinition instances as defined
  622. by the program for which these parameters exist.
  623. @note
  624. Only available if this parameters object has named parameters.
  625. */
  626. GpuConstantDefinitionIterator getConstantDefinitionIterator(void) const;
  627. /** Get a specific GpuConstantDefinition for a named parameter.
  628. @note
  629. Only available if this parameters object has named parameters.
  630. */
  631. const GpuConstantDefinition& getConstantDefinition(const String& name) const;
  632. /** Get the full list of GpuConstantDefinition instances.
  633. @note
  634. Only available if this parameters object has named parameters.
  635. */
  636. const GpuNamedConstants& getConstantDefinitions() const;
  637. /** Get the current list of mappings from low-level logical param indexes
  638. to physical buffer locations in the float buffer.
  639. @note
  640. Only applicable to low-level programs.
  641. */
  642. const GpuLogicalBufferStructPtr& getFloatLogicalBufferStruct() const { return mFloatLogicalToPhysical; }
  643. /** Retrieves the logical index relating to a physical index in the float
  644. buffer, for programs which support that (low-level programs and
  645. high-level programs which use logical parameter indexes).
  646. @returns std::numeric_limits<UINT32>::max() if not found
  647. */
  648. UINT32 getFloatLogicalIndexForPhysicalIndex(UINT32 physicalIndex);
  649. /** Retrieves the logical index relating to a physical index in the int
  650. buffer, for programs which support that (low-level programs and
  651. high-level programs which use logical parameter indexes).
  652. @returns std::numeric_limits<UINT32>::max() if not found
  653. */
  654. UINT32 getIntLogicalIndexForPhysicalIndex(UINT32 physicalIndex);
  655. /** Get the current list of mappings from low-level logical param indexes
  656. to physical buffer locations in the integer buffer.
  657. @note
  658. Only applicable to low-level programs.
  659. */
  660. const GpuLogicalBufferStructPtr& getIntLogicalBufferStruct() const { return mIntLogicalToPhysical; }
  661. /// Get a reference to the list of float constants
  662. const FloatConstantList& getFloatConstantList() const { return mFloatConstants; }
  663. /// Get a pointer to the 'nth' item in the float buffer
  664. float* getFloatPointer(UINT32 pos) { return &mFloatConstants[pos]; }
  665. /// Get a pointer to the 'nth' item in the float buffer
  666. const float* getFloatPointer(UINT32 pos) const { return &mFloatConstants[pos]; }
  667. /// Get a reference to the list of int constants
  668. const IntConstantList& getIntConstantList() const { return mIntConstants; }
  669. /// Get a pointer to the 'nth' item in the int buffer
  670. int* getIntPointer(UINT32 pos) { return &mIntConstants[pos]; }
  671. /// Get a pointer to the 'nth' item in the int buffer
  672. const int* getIntPointer(UINT32 pos) const { return &mIntConstants[pos]; }
  673. const GpuLogicalBufferStructPtr& getTextureLogicalBufferStruct() const { return mTextureLogicalToPhysical; }
  674. HTexture getTexture(UINT32 pos) const;
  675. const GpuLogicalBufferStructPtr& getSamplerLogicalBufferStruct() const { return mSamplerLogicalToPhysical; }
  676. SamplerStatePtr getSamplerState(UINT32 pos) const;
  677. /// Get a reference to the list of textures
  678. const Vector<HTexture>::type& getTextureList() const { return mTextures; }
  679. UINT32 getNumTextures() const { return (UINT32)mTextures.size(); }
  680. const Vector<SamplerStatePtr>::type& getSamplerStateList() const { return mSamplerStates; }
  681. UINT32 getNumSamplerStates() const { return (UINT32)mSamplerStates.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, HTexture 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, SamplerStatePtr 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