CmShader.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include "CmShader.h"
  2. #include "CmTechnique.h"
  3. #include "CmException.h"
  4. #include "CmDebug.h"
  5. #include "CmShaderRTTI.h"
  6. namespace CamelotEngine
  7. {
  8. Shader::Shader(const String& name)
  9. :mName(name)
  10. {
  11. }
  12. void Shader::initialize_internal()
  13. { }
  14. TechniquePtr Shader::addTechnique(const String& renderSystem, const String& renderer)
  15. {
  16. TechniquePtr technique = TechniquePtr(new Technique(renderSystem, renderer));
  17. mTechniques.push_back(technique);
  18. return technique;
  19. }
  20. void Shader::removeTechnique(UINT32 idx)
  21. {
  22. if(idx < 0 || idx >= mTechniques.size())
  23. CM_EXCEPT(InvalidParametersException, "Index out of range: " + toString(idx));
  24. int count = 0;
  25. auto iter = mTechniques.begin();
  26. while(count != idx)
  27. {
  28. ++count;
  29. ++iter;
  30. }
  31. mTechniques.erase(iter);
  32. }
  33. void Shader::removeTechnique(TechniquePtr technique)
  34. {
  35. auto iterFind = std::find(mTechniques.begin(), mTechniques.end(), technique);
  36. if(iterFind == mTechniques.end())
  37. CM_EXCEPT(InvalidParametersException, "Cannot remove specified technique because it wasn't found in this shader.");
  38. mTechniques.erase(iterFind);
  39. }
  40. TechniquePtr Shader::getBestTechnique() const
  41. {
  42. for(auto iter = mTechniques.begin(); iter != mTechniques.end(); ++iter)
  43. {
  44. if((*iter)->isSupported())
  45. {
  46. return *iter;
  47. }
  48. }
  49. CM_EXCEPT(InternalErrorException, "No techniques are supported!");
  50. // TODO - Low priority. Instead of throwing an exception use an extremely simple technique that will be supported almost everywhere as a fallback.
  51. }
  52. void Shader::addParameter(const String& name, const String& gpuVariableName, GpuParamDataType type, UINT32 arraySize, bool hidden)
  53. {
  54. SHADER_DATA_PARAM_DESC desc;
  55. desc.name = name;
  56. desc.gpuVariableName = gpuVariableName;
  57. desc.type = type;
  58. desc.arraySize = arraySize;
  59. desc.hidden = hidden;
  60. mDataParams[name] = desc;
  61. mObjectParams.erase(name);
  62. }
  63. void Shader::addParameter(const String& name, const String& gpuVariableName, GpuParamObjectType type, bool hidden)
  64. {
  65. SHADER_OBJECT_PARAM_DESC desc;
  66. desc.name = name;
  67. desc.gpuVariableName = gpuVariableName;
  68. desc.type = type;
  69. desc.hidden = hidden;
  70. mObjectParams[name] = desc;
  71. mDataParams.erase(name);
  72. }
  73. GpuParamType Shader::getParamType(const String& name) const
  74. {
  75. auto findIterData = mDataParams.find(name);
  76. if(findIterData != mDataParams.end())
  77. return GPT_DATA;
  78. auto findIterObject = mObjectParams.find(name);
  79. if(findIterObject != mObjectParams.end())
  80. return GPT_OBJECT;
  81. CM_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  82. }
  83. const SHADER_DATA_PARAM_DESC& Shader::getDataParamDesc(const String& name) const
  84. {
  85. auto findIterData = mDataParams.find(name);
  86. if(findIterData != mDataParams.end())
  87. return findIterData->second;
  88. CM_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  89. }
  90. const SHADER_OBJECT_PARAM_DESC& Shader::getObjectParamDesc(const String& name) const
  91. {
  92. auto findIterObject = mObjectParams.find(name);
  93. if(findIterObject != mObjectParams.end())
  94. return findIterObject->second;
  95. CM_EXCEPT(InternalErrorException, "Cannot find the parameter with the name: " + name);
  96. }
  97. bool Shader::hasDataParam(const String& name) const
  98. {
  99. auto findIterData = mDataParams.find(name);
  100. if(findIterData != mDataParams.end())
  101. return true;
  102. return false;
  103. }
  104. bool Shader::hasObjectParam(const String& name) const
  105. {
  106. auto findIterObject = mObjectParams.find(name);
  107. if(findIterObject != mObjectParams.end())
  108. return true;
  109. return false;
  110. }
  111. void Shader::removeParameter(const String& name)
  112. {
  113. mDataParams.erase(name);
  114. mObjectParams.erase(name);
  115. }
  116. void Shader::setParamBlockAttribs(const String& name, bool shared, GpuParamBlockUsage usage)
  117. {
  118. SHADER_PARAM_BLOCK_DESC desc;
  119. desc.name = name;
  120. desc.shared = shared;
  121. desc.usage = usage;
  122. mParamBlocks[name] = desc;
  123. }
  124. bool Shader::isSampler(GpuParamObjectType type)
  125. {
  126. switch(type)
  127. {
  128. case GPOT_SAMPLER1D:
  129. case GPOT_SAMPLER2D:
  130. case GPOT_SAMPLER3D:
  131. case GPOT_SAMPLERCUBE:
  132. return true;
  133. }
  134. return false;
  135. }
  136. bool Shader::isTexture(GpuParamObjectType type)
  137. {
  138. switch(type)
  139. {
  140. case GPOT_TEXTURE1D:
  141. case GPOT_TEXTURE2D:
  142. case GPOT_TEXTURE3D:
  143. case GPOT_TEXTURECUBE:
  144. case GPOT_RWTEXTURE1D:
  145. case GPOT_RWTEXTURE2D:
  146. case GPOT_RWTEXTURE3D:
  147. return true;
  148. }
  149. return false;
  150. }
  151. bool Shader::isBuffer(GpuParamObjectType type)
  152. {
  153. switch(type)
  154. {
  155. case GPOT_BYTE_BUFFER:
  156. case GPOT_STRUCTURED_BUFFER:
  157. case GPOT_RWBYTE_BUFFER:
  158. case GPOT_RWAPPEND_BUFFER:
  159. case GPOT_RWCONSUME_BUFFER:
  160. case GPOT_RWSTRUCTURED_BUFFER:
  161. case GPOT_RWSTRUCTURED_BUFFER_WITH_COUNTER:
  162. case GPOT_RWTYPED_BUFFER:
  163. return true;
  164. }
  165. return false;
  166. }
  167. RTTITypeBase* Shader::getRTTIStatic()
  168. {
  169. return ShaderRTTI::instance();
  170. }
  171. RTTITypeBase* Shader::getRTTI() const
  172. {
  173. return Shader::getRTTIStatic();
  174. }
  175. }