CmD3D9HLSLProgram.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #include "CmD3D9HLSLProgram.h"
  25. #include "CmGpuProgramManager.h"
  26. #include "CmD3D9GpuProgram.h"
  27. #include "CmException.h"
  28. #include "CmRenderSystem.h"
  29. #include "CmAsyncOp.h"
  30. #include "CmGpuParams.h"
  31. #include "CmD3D9HLSLParamParser.h"
  32. #include "CmD3D9HLSLProgramRTTI.h"
  33. namespace CamelotFramework {
  34. //-----------------------------------------------------------------------
  35. D3D9HLSLProgram::D3D9HLSLProgram(const String& source, const String& entryPoint, const String& language,
  36. GpuProgramType gptype, GpuProgramProfile profile, const Vector<HGpuProgInclude>::type* includes, bool isAdjacencyInfoRequired)
  37. : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, includes, isAdjacencyInfoRequired)
  38. , mPreprocessorDefines()
  39. , mColumnMajorMatrices(true)
  40. , mpMicroCode(NULL)
  41. , mOptimisationLevel(OPT_DEFAULT)
  42. {
  43. }
  44. //-----------------------------------------------------------------------
  45. D3D9HLSLProgram::~D3D9HLSLProgram()
  46. {
  47. }
  48. //-----------------------------------------------------------------------
  49. void D3D9HLSLProgram::initialize_internal()
  50. {
  51. if (isSupported())
  52. {
  53. // Populate preprocessor defines
  54. String stringBuffer;
  55. Vector<D3DXMACRO>::type defines;
  56. const D3DXMACRO* pDefines = 0;
  57. if (!mPreprocessorDefines.empty())
  58. {
  59. stringBuffer = mPreprocessorDefines;
  60. // Split preprocessor defines and build up macro array
  61. D3DXMACRO macro;
  62. String::size_type pos = 0;
  63. while (pos != String::npos)
  64. {
  65. macro.Name = &stringBuffer[pos];
  66. macro.Definition = 0;
  67. String::size_type start_pos=pos;
  68. // Find delims
  69. pos = stringBuffer.find_first_of(";,=", pos);
  70. if(start_pos==pos)
  71. {
  72. if(pos==stringBuffer.length())
  73. {
  74. break;
  75. }
  76. pos++;
  77. continue;
  78. }
  79. if (pos != String::npos)
  80. {
  81. // Check definition part
  82. if (stringBuffer[pos] == '=')
  83. {
  84. // Setup null character for macro name
  85. stringBuffer[pos++] = '\0';
  86. macro.Definition = &stringBuffer[pos];
  87. pos = stringBuffer.find_first_of(";,", pos);
  88. }
  89. else
  90. {
  91. // No definition part, define as "1"
  92. macro.Definition = "1";
  93. }
  94. if (pos != String::npos)
  95. {
  96. // Setup null character for macro name or definition
  97. stringBuffer[pos++] = '\0';
  98. }
  99. }
  100. else
  101. {
  102. macro.Definition = "1";
  103. }
  104. if(strlen(macro.Name)>0)
  105. {
  106. defines.push_back(macro);
  107. }
  108. else
  109. {
  110. break;
  111. }
  112. }
  113. // Add NULL terminator
  114. macro.Name = 0;
  115. macro.Definition = 0;
  116. defines.push_back(macro);
  117. pDefines = &defines[0];
  118. }
  119. // Populate compile flags
  120. DWORD compileFlags = 0;
  121. if (mColumnMajorMatrices)
  122. compileFlags |= D3DXSHADER_PACKMATRIX_COLUMNMAJOR;
  123. else
  124. compileFlags |= D3DXSHADER_PACKMATRIX_ROWMAJOR;
  125. #if CM_DEBUG_MODE
  126. compileFlags |= D3DXSHADER_DEBUG;
  127. #endif
  128. switch (mOptimisationLevel)
  129. {
  130. case OPT_DEFAULT:
  131. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  132. break;
  133. case OPT_NONE:
  134. compileFlags |= D3DXSHADER_SKIPOPTIMIZATION;
  135. break;
  136. case OPT_0:
  137. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL0;
  138. break;
  139. case OPT_1:
  140. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL1;
  141. break;
  142. case OPT_2:
  143. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL2;
  144. break;
  145. case OPT_3:
  146. compileFlags |= D3DXSHADER_OPTIMIZATION_LEVEL3;
  147. break;
  148. }
  149. LPD3DXBUFFER errors = 0;
  150. // include handler
  151. LPD3DXCONSTANTTABLE constTable;
  152. String hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  153. // Compile & assemble into microcode
  154. HRESULT hr = D3DXCompileShader(
  155. mSource.c_str(),
  156. static_cast<UINT>(mSource.length()),
  157. pDefines,
  158. nullptr,
  159. mEntryPoint.c_str(),
  160. hlslProfile.c_str(),
  161. compileFlags,
  162. &mpMicroCode,
  163. &errors,
  164. &constTable);
  165. if (FAILED(hr))
  166. {
  167. String message = "Cannot assemble D3D9 high-level shader ";
  168. if( errors )
  169. {
  170. message += String(" Errors:\n") + static_cast<const char*>(errors->GetBufferPointer());
  171. errors->Release();
  172. }
  173. CM_EXCEPT(RenderingAPIException, message);
  174. }
  175. hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  176. // Create a low-level program, give it the same name as us
  177. mAssemblerProgram =
  178. GpuProgramManager::instance().createProgram(
  179. "",// dummy source, since we'll be using microcode
  180. "",
  181. hlslProfile,
  182. mType,
  183. GPP_NONE);
  184. static_cast<D3D9GpuProgram*>(mAssemblerProgram.get())->setExternalMicrocode(mpMicroCode);
  185. D3D9HLSLParamParser paramParser(constTable);
  186. mParametersDesc = paramParser.buildParameterDescriptions();
  187. SAFE_RELEASE(constTable);
  188. }
  189. HighLevelGpuProgram::initialize_internal();
  190. }
  191. //-----------------------------------------------------------------------
  192. void D3D9HLSLProgram::destroy_internal()
  193. {
  194. SAFE_RELEASE(mpMicroCode);
  195. HighLevelGpuProgram::destroy_internal();
  196. }
  197. //-----------------------------------------------------------------------
  198. LPD3DXBUFFER D3D9HLSLProgram::getMicroCode()
  199. {
  200. return mpMicroCode;
  201. }
  202. //-----------------------------------------------------------------------
  203. bool D3D9HLSLProgram::isSupported(void) const
  204. {
  205. if (!isRequiredCapabilitiesSupported())
  206. return false;
  207. String hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  208. RenderSystem* rs = CamelotFramework::RenderSystem::instancePtr();
  209. return rs->getCapabilities()->isShaderProfileSupported(hlslProfile);
  210. }
  211. //-----------------------------------------------------------------------
  212. GpuParamsPtr D3D9HLSLProgram::createParameters()
  213. {
  214. GpuParamsPtr params = cm_shared_ptr<GpuParams, PoolAlloc>(std::ref(mParametersDesc), mColumnMajorMatrices);
  215. return params;
  216. }
  217. //-----------------------------------------------------------------------
  218. const String& D3D9HLSLProgram::getLanguage(void) const
  219. {
  220. static const String language = "hlsl";
  221. return language;
  222. }
  223. /************************************************************************/
  224. /* SERIALIZATION */
  225. /************************************************************************/
  226. RTTITypeBase* D3D9HLSLProgram::getRTTIStatic()
  227. {
  228. return D3D9HLSLProgramRTTI::instance();
  229. }
  230. RTTITypeBase* D3D9HLSLProgram::getRTTI() const
  231. {
  232. return D3D9HLSLProgram::getRTTIStatic();
  233. }
  234. }