CmD3D11HLSLProgram.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 CamelotFramework
  14. {
  15. UINT32 D3D11HLSLProgram::globalProgramId = 0;
  16. D3D11HLSLProgram::D3D11HLSLProgram(const String& source, const String& entryPoint, const String& language,
  17. GpuProgramType gptype, GpuProgramProfile profile, const Vector<HGpuProgInclude>::type* includes, bool isAdjacencyInfoRequired)
  18. : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, includes, isAdjacencyInfoRequired),
  19. mColumnMajorMatrices(true), mEnableBackwardsCompatibility(false), mProgramId(0)
  20. {
  21. }
  22. D3D11HLSLProgram::~D3D11HLSLProgram()
  23. {
  24. }
  25. void D3D11HLSLProgram::initialize_internal()
  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. mProgramId = globalProgramId++;
  68. SAFE_RELEASE(microcode);
  69. HighLevelGpuProgram::initialize_internal();
  70. }
  71. void D3D11HLSLProgram::destroy_internal()
  72. {
  73. mAssemblerProgram = nullptr;
  74. mMicrocode.clear();
  75. mInputDeclaration = nullptr;
  76. HighLevelGpuProgram::destroy_internal();
  77. }
  78. const String& D3D11HLSLProgram::getLanguage() const
  79. {
  80. static String name = "hlsl";
  81. return name;
  82. }
  83. bool D3D11HLSLProgram::isSupported() const
  84. {
  85. RenderSystem* rs = RenderSystem::instancePtr();
  86. return rs->getCapabilities()->isShaderProfileSupported(getSyntaxCode()) && HighLevelGpuProgram::isSupported();
  87. }
  88. ID3DBlob* D3D11HLSLProgram::compileMicrocode(const String& profile)
  89. {
  90. // TODO - Preprocessor defines aren't supported
  91. UINT compileFlags = 0;
  92. #if defined(CM_DEBUG_MODE)
  93. compileFlags |= D3DCOMPILE_DEBUG;
  94. compileFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  95. #endif
  96. if (mColumnMajorMatrices)
  97. compileFlags |= D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
  98. else
  99. compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
  100. if (mEnableBackwardsCompatibility)
  101. compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  102. ID3DBlob* microCode = nullptr;
  103. ID3DBlob* errors = nullptr;
  104. HRESULT hr = D3DCompile(
  105. mSource.c_str(), // [in] Pointer to the shader in memory.
  106. mSource.size(), // [in] Size of the shader in memory.
  107. nullptr, // [in] The name of the file that contains the shader code.
  108. nullptr, // [in] Optional. Pointer to a NULL-terminated array of macro definitions. See D3D_SHADER_MACRO. If not used, set this to NULL.
  109. 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.
  110. mEntryPoint.c_str(),// [in] Name of the shader-entrypoint function where shader execution begins.
  111. profile.c_str(),// [in] A string that specifies the shader model; can be any profile in shader model 4 or higher.
  112. compileFlags, // [in] Effect compile flags - no D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY at the first try...
  113. 0, // [in] Effect compile flags
  114. &microCode, // [out] A pointer to an ID3DBlob Interface which contains the compiled shader, as well as any embedded debug and symbol-table information.
  115. &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.
  116. );
  117. if (FAILED(hr))
  118. {
  119. String message = "Cannot assemble D3D11 high-level shader. Errors:\n" +
  120. String(static_cast<const char*>(errors->GetBufferPointer()));
  121. SAFE_RELEASE(errors);
  122. CM_EXCEPT(RenderingAPIException, message);
  123. }
  124. SAFE_RELEASE(errors);
  125. return microCode;
  126. }
  127. void D3D11HLSLProgram::populateParametersAndConstants(ID3DBlob* microcode)
  128. {
  129. assert(microcode != nullptr);
  130. D3D11HLSLParamParser parser;
  131. if(mType == GPT_VERTEX_PROGRAM)
  132. mInputDeclaration = HardwareBufferManager::instance().createVertexDeclaration();
  133. parser.parse(microcode, mParametersDesc, mInputDeclaration);
  134. }
  135. GpuParamsPtr D3D11HLSLProgram::createParameters()
  136. {
  137. GpuParamsPtr params = cm_shared_ptr<GpuParams, PoolAlloc>(std::ref(mParametersDesc), mColumnMajorMatrices);
  138. return params;
  139. }
  140. /************************************************************************/
  141. /* SERIALIZATION */
  142. /************************************************************************/
  143. RTTITypeBase* D3D11HLSLProgram::getRTTIStatic()
  144. {
  145. return D3D11HLSLProgramRTTI::instance();
  146. }
  147. RTTITypeBase* D3D11HLSLProgram::getRTTI() const
  148. {
  149. return D3D11HLSLProgram::getRTTIStatic();
  150. }
  151. }