2
0

CmShader.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmResource.h"
  4. #include "CmCommonEnums.h"
  5. namespace CamelotFramework
  6. {
  7. struct CM_EXPORT SHADER_DATA_PARAM_DESC
  8. {
  9. String name;
  10. String gpuVariableName;
  11. GpuParamDataType type;
  12. UINT32 arraySize;
  13. bool hidden;
  14. UINT32 elementSize;
  15. };
  16. struct CM_EXPORT SHADER_OBJECT_PARAM_DESC
  17. {
  18. String name;
  19. String gpuVariableName;
  20. GpuParamObjectType type;
  21. bool hidden;
  22. };
  23. struct CM_EXPORT SHADER_PARAM_BLOCK_DESC
  24. {
  25. String name;
  26. bool shared;
  27. GpuParamBlockUsage usage;
  28. };
  29. /**
  30. * @brief Shader represents a collection of techniques. They are used in Materials,
  31. * which can be considered as instances of a Shader. Multiple materials
  32. * may share the same shader but provide different parameters to it.
  33. *
  34. * Shader will always choose the first supported technique based on the current render
  35. * system, render manager and other properties. So make sure to add most important techniques
  36. * first so you make sure they are used if they are supported.
  37. */
  38. class CM_EXPORT Shader : public Resource
  39. {
  40. public:
  41. TechniquePtr addTechnique(const String& renderSystem, const String& renderer);
  42. void removeTechnique(UINT32 idx);
  43. void removeTechnique(TechniquePtr technique);
  44. UINT32 getNumTechniques() const { return (UINT32)mTechniques.size(); }
  45. /**
  46. * @brief Gets the best supported technique based on current render and other systems.
  47. * Throws an exception if not a single technique is supported.
  48. */
  49. TechniquePtr getBestTechnique() const;
  50. /**
  51. * @brief Registers a new parameter you can use for easily setting GpuProgram constants via Material.
  52. * Only data types may be set using this method. Use the other overload of the method if you want
  53. * to add object parameters.
  54. *
  55. * @param name The name of the parameter.
  56. * @param gpuVariableName Name of the GPU variable in the GpuProgram that the parameter corresponds with.
  57. * @param type The type of the parameter, must be the same as the type in GpuProgram.
  58. * @param arraySize (optional) If the parameter is an array, the number of elements in the array. Size of 1 means its not an array.
  59. * @param elementSize (optional) Size of an individual element in the array, in bytes. You only need to set this if you are setting variable
  60. * length parameters, like structs.
  61. * @param hidden (optional) Property that is not directly used by the material system, but can be useful if you need to mark certain parameters
  62. * as hidden to some system. (e.g. hiding internal engine-managed parameters from the user in the Editor)
  63. */
  64. void addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type, UINT32 arraySize = 1, UINT32 elementSize = 0, bool hidden = false);
  65. void addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, bool hidden = false);
  66. void removeParameter(const String& name);
  67. void setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage);
  68. GpuParamType getParamType(const String& name) const;
  69. const SHADER_DATA_PARAM_DESC& getDataParamDesc(const String& name) const;
  70. const SHADER_OBJECT_PARAM_DESC& getObjectParamDesc(const String& name) const;
  71. bool hasDataParam(const String& name) const;
  72. bool hasObjectParam(const String& name) const;
  73. const Map<String, SHADER_DATA_PARAM_DESC>::type& getDataParams() const { return mDataParams; }
  74. const Map<String, SHADER_OBJECT_PARAM_DESC>::type& getObjectParams() const { return mObjectParams; }
  75. const Map<String, SHADER_PARAM_BLOCK_DESC>::type& getParamBlocks() const { return mParamBlocks; }
  76. static bool isSampler(GpuParamObjectType type);
  77. static bool isTexture(GpuParamObjectType type);
  78. static bool isBuffer(GpuParamObjectType type);
  79. static ShaderPtr create(const String& name);
  80. private:
  81. String mName;
  82. Vector<TechniquePtr>::type mTechniques;
  83. Map<String, SHADER_DATA_PARAM_DESC>::type mDataParams;
  84. Map<String, SHADER_OBJECT_PARAM_DESC>::type mObjectParams;
  85. Map<String, SHADER_PARAM_BLOCK_DESC>::type mParamBlocks;
  86. Shader(const String& name);
  87. /************************************************************************/
  88. /* RTTI */
  89. /************************************************************************/
  90. public:
  91. friend class ShaderRTTI;
  92. static RTTITypeBase* getRTTIStatic();
  93. virtual RTTITypeBase* getRTTI() const;
  94. };
  95. }