CmD3D11HLSLProgram.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "CmException.h"
  10. #include "CmDebug.h"
  11. namespace CamelotEngine
  12. {
  13. D3D11HLSLProgram::D3D11HLSLProgram(const String& source, const String& entryPoint, const String& language,
  14. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  15. : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, isAdjacencyInfoRequired),
  16. mColumnMajorMatrices(true), mEnableBackwardsCompatibility(false)
  17. {
  18. }
  19. D3D11HLSLProgram::~D3D11HLSLProgram()
  20. {
  21. unload_internal();
  22. }
  23. void D3D11HLSLProgram::loadFromSource()
  24. {
  25. String hlslProfile = GpuProgramManager::instance().gpuProgProfileToRSSpecificProfile(mProfile);
  26. ID3DBlob* microcode = compileMicrocode(hlslProfile);
  27. mMicrocode.resize(microcode->GetBufferSize());
  28. memcpy(&mMicrocode[0], microcode->GetBufferPointer(), microcode->GetBufferSize());
  29. populateParametersAndConstants(microcode);
  30. mAssemblerProgram = GpuProgramManager::instance().createProgram("", "", hlslProfile, mType, GPP_NONE); // We load it from microcode, so none of this matters
  31. D3D11RenderSystem* rs = static_cast<D3D11RenderSystem*>(RenderSystem::instancePtr());
  32. switch(mType)
  33. {
  34. case GPT_VERTEX_PROGRAM:
  35. {
  36. D3D11GpuVertexProgramPtr vertProgram = std::static_pointer_cast<D3D11GpuVertexProgram>(mAssemblerProgram);
  37. vertProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  38. }
  39. break;
  40. case GPT_FRAGMENT_PROGRAM:
  41. {
  42. D3D11GpuFragmentProgramPtr fragProgram = std::static_pointer_cast<D3D11GpuFragmentProgram>(mAssemblerProgram);
  43. fragProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  44. }
  45. break;
  46. case GPT_GEOMETRY_PROGRAM:
  47. {
  48. D3D11GpuGeometryProgramPtr geomProgram = std::static_pointer_cast<D3D11GpuGeometryProgram>(mAssemblerProgram);
  49. geomProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  50. }
  51. break;
  52. case GPT_HULL_PROGRAM:
  53. {
  54. D3D11GpuHullProgramPtr hullProgram = std::static_pointer_cast<D3D11GpuHullProgram>(mAssemblerProgram);
  55. hullProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  56. }
  57. break;
  58. case GPT_DOMAIN_PROGRAM:
  59. {
  60. D3D11GpuDomainProgramPtr domainProgram = std::static_pointer_cast<D3D11GpuDomainProgram>(mAssemblerProgram);
  61. domainProgram->loadFromMicrocode(rs->getPrimaryDevice(), microcode);
  62. }
  63. break;
  64. }
  65. SAFE_RELEASE(microcode);
  66. }
  67. void D3D11HLSLProgram::unload_internal()
  68. {
  69. mAssemblerProgram = nullptr;
  70. mInputParameters.clear();
  71. mOutputParameters.clear();
  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. parser.parse(microcode, mParametersDesc, mInputParameters, mOutputParameters);
  128. }
  129. /************************************************************************/
  130. /* SERIALIZATION */
  131. /************************************************************************/
  132. RTTITypeBase* D3D11HLSLProgram::getRTTIStatic()
  133. {
  134. return D3D11HLSLProgramRTTI::instance();
  135. }
  136. RTTITypeBase* D3D11HLSLProgram::getRTTI() const
  137. {
  138. return D3D11HLSLProgram::getRTTIStatic();
  139. }
  140. }