CmD3D11HLSLProgram.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "CmD3D11HLSLProgram.h"
  2. #include "CmRenderSystemManager.h"
  3. #include "CmRenderSystem.h"
  4. #include "CmException.h"
  5. namespace CamelotEngine
  6. {
  7. D3D11HLSLProgram::D3D11HLSLProgram(const String& source, const String& entryPoint, const String& language,
  8. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  9. : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, isAdjacencyInfoRequired),
  10. mColumnMajorMatrices(true), mEnableBackwardsCompatibility(false)
  11. {
  12. }
  13. D3D11HLSLProgram::~D3D11HLSLProgram()
  14. {
  15. unloadHighLevel();
  16. }
  17. const String& D3D11HLSLProgram::getLanguage() const
  18. {
  19. static String name = "hlsl";
  20. return name;
  21. }
  22. bool D3D11HLSLProgram::isSupported() const
  23. {
  24. RenderSystem* rs = RenderSystemManager::getActive();
  25. return rs->getCapabilities_internal()->isShaderProfileSupported(getSyntaxCode()) && HighLevelGpuProgram::isSupported();
  26. }
  27. ID3DBlob* D3D11HLSLProgram::compileMicrocode()
  28. {
  29. // TODO - Preprocessor defines aren't supported
  30. UINT compileFlags = 0;
  31. #if defined(CM_DEBUG_MODE)
  32. compileFlags |= D3DCOMPILE_DEBUG;
  33. compileFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  34. #endif
  35. if (mColumnMajorMatrices)
  36. compileFlags |= D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
  37. else
  38. compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
  39. if (mEnableBackwardsCompatibility)
  40. compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  41. ID3DBlob* microCode = nullptr;
  42. ID3DBlob* errors = nullptr;
  43. HRESULT hr = D3DCompile(
  44. mSource.c_str(), // [in] Pointer to the shader in memory.
  45. mSource.size(), // [in] Size of the shader in memory.
  46. nullptr, // [in] The name of the file that contains the shader code.
  47. nullptr, // [in] Optional. Pointer to a NULL-terminated array of macro definitions. See D3D_SHADER_MACRO. If not used, set this to NULL.
  48. 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.
  49. mEntryPoint.c_str(),// [in] Name of the shader-entrypoint function where shader execution begins.
  50. mSyntaxCode.c_str(),// [in] A string that specifies the shader model; can be any profile in shader model 4 or higher.
  51. compileFlags, // [in] Effect compile flags - no D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY at the first try...
  52. 0, // [in] Effect compile flags
  53. &microCode, // [out] A pointer to an ID3DBlob Interface which contains the compiled shader, as well as any embedded debug and symbol-table information.
  54. &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.
  55. );
  56. if (FAILED(hr))
  57. {
  58. String message = "Cannot assemble D3D11 high-level shader. Errors:\n" +
  59. String(static_cast<const char*>(errors->GetBufferPointer()));
  60. SAFE_RELEASE(errors);
  61. CM_EXCEPT(RenderingAPIException, message);
  62. }
  63. SAFE_RELEASE(errors);
  64. }
  65. }