CmD3D11HLSLProgram.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "CmD3D11HLSLProgram.h"
  2. #include "CmD3D11HLSLProgramRTTI.h"
  3. #include "CmRenderSystem.h"
  4. #include "CmGpuProgramManager.h"
  5. #include "CmD3D11GpuProgram.h"
  6. #include "CmHardwareBufferManager.h"
  7. #include "CmD3D11RenderSystem.h"
  8. #include "CmD3D11HLSLParamParser.h"
  9. #include "CmD3D11Mappings.h"
  10. #include "CmGpuParams.h"
  11. #include "CmException.h"
  12. #include "CmDebug.h"
  13. namespace CamelotEngine
  14. {
  15. D3D11HLSLProgram::D3D11HLSLProgram(const String& source, const String& entryPoint, const String& language,
  16. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  17. : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, isAdjacencyInfoRequired),
  18. mColumnMajorMatrices(true), mEnableBackwardsCompatibility(false)
  19. {
  20. }
  21. D3D11HLSLProgram::~D3D11HLSLProgram()
  22. {
  23. unload_internal();
  24. }
  25. void D3D11HLSLProgram::loadFromSource()
  26. {
  27. String hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  28. ID3DBlob* microcode = compileMicrocode(hlslProfile);
  29. mMicrocode.resize(microcode->GetBufferSize());
  30. memcpy(&mMicrocode[0], microcode->GetBufferPointer(), microcode->GetBufferSize());
  31. populateParametersAndConstants(microcode);
  32. mAssemblerProgram = GpuProgramManager::instance().createProgram("", "", hlslProfile, mType, GPP_NONE); // We load it from microcode, so none of this matters
  33. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  34. switch(mType)
  35. {
  36. case GPT_VERTEX_PROGRAM:
  37. {
  38. D3D11GpuVertexProgramPtr vertProgram = std::static_pointer_cast<D3D11GpuVertexProgram>(mAssemblerProgram);
  39. vertProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  40. }
  41. break;
  42. case GPT_FRAGMENT_PROGRAM:
  43. {
  44. D3D11GpuFragmentProgramPtr fragProgram = std::static_pointer_cast<D3D11GpuFragmentProgram>(mAssemblerProgram);
  45. fragProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  46. }
  47. break;
  48. case GPT_GEOMETRY_PROGRAM:
  49. {
  50. D3D11GpuGeometryProgramPtr geomProgram = std::static_pointer_cast<D3D11GpuGeometryProgram>(mAssemblerProgram);
  51. geomProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  52. }
  53. break;
  54. case GPT_HULL_PROGRAM:
  55. {
  56. D3D11GpuHullProgramPtr hullProgram = std::static_pointer_cast<D3D11GpuHullProgram>(mAssemblerProgram);
  57. hullProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  58. }
  59. break;
  60. case GPT_DOMAIN_PROGRAM:
  61. {
  62. D3D11GpuDomainProgramPtr domainProgram = std::static_pointer_cast<D3D11GpuDomainProgram>(mAssemblerProgram);
  63. domainProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  64. }
  65. break;
  66. }
  67. SAFE_RELEASE(microcode);
  68. }
  69. void D3D11HLSLProgram::unload_internal()
  70. {
  71. mAssemblerProgram = nullptr;
  72. mMicrocode.clear();
  73. }
  74. const String& D3D11HLSLProgram::getLanguage() const
  75. {
  76. static String name = "hlsl";
  77. return name;
  78. }
  79. bool D3D11HLSLProgram::isSupported() const
  80. {
  81. RenderSystem* rs = RenderSystem::instancePtr();
  82. return rs->getCapabilities()->isShaderProfileSupported(getSyntaxCode()) && HighLevelGpuProgram::isSupported();
  83. }
  84. ID3DBlob* D3D11HLSLProgram::compileMicrocode(const String& profile)
  85. {
  86. // TODO - Preprocessor defines aren't supported
  87. UINT compileFlags = 0;
  88. #if defined(CM_DEBUG_MODE)
  89. compileFlags |= D3DCOMPILE_DEBUG;
  90. compileFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  91. #endif
  92. if (mColumnMajorMatrices)
  93. compileFlags |= D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
  94. else
  95. compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
  96. if (mEnableBackwardsCompatibility)
  97. compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  98. ID3DBlob* microCode = nullptr;
  99. ID3DBlob* errors = nullptr;
  100. HRESULT hr = D3DCompile(
  101. mSource.c_str(), // [in] Pointer to the shader in memory.
  102. mSource.size(), // [in] Size of the shader in memory.
  103. nullptr, // [in] The name of the file that contains the shader code.
  104. nullptr, // [in] Optional. Pointer to a NULL-terminated array of macro definitions. See D3D_SHADER_MACRO. If not used, set this to NULL.
  105. nullptr, // [in] Optional. Pointer to an ID3DInclude Interface interface for handling include files. Setting this to NULL will cause a compile error if a shader contains a #include.
  106. mEntryPoint.c_str(),// [in] Name of the shader-entrypoint function where shader execution begins.
  107. profile.c_str(),// [in] A string that specifies the shader model; can be any profile in shader model 4 or higher.
  108. compileFlags, // [in] Effect compile flags - no D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY at the first try...
  109. 0, // [in] Effect compile flags
  110. &microCode, // [out] A pointer to an ID3DBlob Interface which contains the compiled shader, as well as any embedded debug and symbol-table information.
  111. &errors // [out] A pointer to an ID3DBlob Interface which contains a listing of errors and warnings that occurred during compilation. These errors and warnings are identical to the the debug output from a debugger.
  112. );
  113. if (FAILED(hr))
  114. {
  115. String message = "Cannot assemble D3D11 high-level shader. Errors:\n" +
  116. String(static_cast<const char*>(errors->GetBufferPointer()));
  117. SAFE_RELEASE(errors);
  118. CM_EXCEPT(RenderingAPIException, message);
  119. }
  120. SAFE_RELEASE(errors);
  121. return microCode;
  122. }
  123. void D3D11HLSLProgram::populateParametersAndConstants(ID3DBlob* microcode)
  124. {
  125. assert(microcode != nullptr);
  126. D3D11HLSLParamParser parser;
  127. if(mType == GPT_VERTEX_PROGRAM)
  128. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration();
  129. parser.parse(microcode, mParametersDesc, mInputDeclaration);
  130. }
  131. GpuParamsPtr D3D11HLSLProgram::createParameters()
  132. {
  133. GpuParamsPtr params(new GpuParams(mParametersDesc));
  134. params->setTransposeMatrices(mColumnMajorMatrices);
  135. return params;
  136. }
  137. /************************************************************************/
  138. /* SERIALIZATION */
  139. /************************************************************************/
  140. RTTITypeBase* D3D11HLSLProgram::getRTTIStatic()
  141. {
  142. return D3D11HLSLProgramRTTI::instance();
  143. }
  144. RTTITypeBase* D3D11HLSLProgram::getRTTI() const
  145. {
  146. return D3D11HLSLProgram::getRTTIStatic();
  147. }
  148. }