CmD3D11HLSLProgram.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. #include "CmD3D11HLSLProgram.h"
  2. #include "CmRenderSystem.h"
  3. #include "CmGpuProgramManager.h"
  4. #include "CmD3D11GpuProgram.h"
  5. #include "CmHardwareBufferManager.h"
  6. #include "CmHardwareConstantBuffer.h"
  7. #include "CmD3D11RenderSystem.h"
  8. #include "CmException.h"
  9. #include "CmDebug.h"
  10. namespace CamelotEngine
  11. {
  12. D3D11HLSLProgram::D3D11HLSLProgram(const String& source, const String& entryPoint, const String& language,
  13. GpuProgramType gptype, GpuProgramProfile profile, bool isAdjacencyInfoRequired)
  14. : HighLevelGpuProgram(source, entryPoint, language, gptype, profile, isAdjacencyInfoRequired),
  15. mColumnMajorMatrices(true), mEnableBackwardsCompatibility(false)
  16. {
  17. }
  18. D3D11HLSLProgram::~D3D11HLSLProgram()
  19. {
  20. unload_internal();
  21. }
  22. void D3D11HLSLProgram::loadFromSource()
  23. {
  24. ID3DBlob* microcode = compileMicrocode();
  25. mMicrocode.resize(microcode->GetBufferSize());
  26. memcpy(&mMicrocode[0], microcode->GetBufferPointer(), microcode->GetBufferSize());
  27. populateParametersAndConstants(microcode);
  28. createConstantBuffers();
  29. mAssemblerProgram = GpuProgramManager::instance().createProgram("", "", "", mType, GPP_NONE); // We load it from microcode, so none of this matters
  30. switch(mType)
  31. {
  32. case GPT_VERTEX_PROGRAM:
  33. {
  34. D3D11GpuVertexProgramPtr vertProgram = std::static_pointer_cast<D3D11GpuVertexProgram>(mAssemblerProgram);
  35. vertProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  36. }
  37. break;
  38. case GPT_FRAGMENT_PROGRAM:
  39. {
  40. D3D11GpuFragmentProgramPtr fragProgram = std::static_pointer_cast<D3D11GpuFragmentProgram>(mAssemblerProgram);
  41. fragProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  42. }
  43. break;
  44. case GPT_GEOMETRY_PROGRAM:
  45. {
  46. D3D11GpuGeometryProgramPtr geomProgram = std::static_pointer_cast<D3D11GpuGeometryProgram>(mAssemblerProgram);
  47. geomProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  48. }
  49. break;
  50. case GPT_HULL_PROGRAM:
  51. {
  52. D3D11GpuHullProgramPtr hullProgram = std::static_pointer_cast<D3D11GpuHullProgram>(mAssemblerProgram);
  53. hullProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  54. }
  55. break;
  56. case GPT_DOMAIN_PROGRAM:
  57. {
  58. D3D11GpuDomainProgramPtr domainProgram = std::static_pointer_cast<D3D11GpuDomainProgram>(mAssemblerProgram);
  59. domainProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  60. }
  61. break;
  62. }
  63. SAFE_RELEASE(microcode);
  64. }
  65. void D3D11HLSLProgram::unload_internal()
  66. {
  67. mAssemblerProgram = nullptr;
  68. mShaderBuffers.clear();
  69. mInputParameters.clear();
  70. mOutputParameters.clear();
  71. mConstantBuffers.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()
  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. mSyntaxCode.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. mShaderBuffers.clear();
  127. mInputParameters.clear();
  128. mOutputParameters.clear();
  129. const char* commentString = nullptr;
  130. ID3DBlob* pIDisassembly = nullptr;
  131. char* pDisassembly = nullptr;
  132. HRESULT hr = D3DDisassemble((UINT*)microcode->GetBufferPointer(),
  133. microcode->GetBufferSize(), D3D_DISASM_ENABLE_COLOR_CODE, commentString, &pIDisassembly);
  134. const char* assemblyCode = static_cast<const char*>(pIDisassembly->GetBufferPointer());
  135. if (FAILED(hr))
  136. CM_EXCEPT(RenderingAPIException, "Unable to disassemble shader.");
  137. ID3D11ShaderReflection* shaderReflection;
  138. hr = D3DReflect((void*)microcode->GetBufferPointer(), microcode->GetBufferSize(),
  139. IID_ID3D11ShaderReflection, (void**)&shaderReflection);
  140. if (FAILED(hr))
  141. CM_EXCEPT(RenderingAPIException, "Cannot reflect D3D11 high-level shader.");
  142. D3D11_SHADER_DESC shaderDesc;
  143. hr = shaderReflection->GetDesc(&shaderDesc);
  144. if (FAILED(hr))
  145. CM_EXCEPT(RenderingAPIException, "Cannot reflect D3D11 high-level shader.");
  146. mInputParameters.resize(shaderDesc.InputParameters);
  147. for (UINT32 i = 0; i < shaderDesc.InputParameters; i++)
  148. shaderReflection->GetInputParameterDesc(i, &(mInputParameters[i]));
  149. mOutputParameters.resize(shaderDesc.OutputParameters);
  150. for (UINT32 i = 0; i < shaderDesc.OutputParameters; i++)
  151. shaderReflection->GetOutputParameterDesc(i, &(mOutputParameters[i]));
  152. mShaderBuffers.resize(shaderDesc.ConstantBuffers);
  153. for(UINT32 i = 0; i < shaderDesc.ConstantBuffers; i++)
  154. {
  155. ID3D11ShaderReflectionConstantBuffer* shaderReflectionConstantBuffer;
  156. shaderReflectionConstantBuffer = shaderReflection->GetConstantBufferByIndex(i);
  157. populateConstantBufferParameters(shaderReflectionConstantBuffer);
  158. }
  159. shaderReflection->Release();
  160. }
  161. void D3D11HLSLProgram::buildConstantDefinitions() const
  162. {
  163. createParameterMappingStructures(true);
  164. for(auto shaderBufferIter = mShaderBuffers.begin(); shaderBufferIter != mShaderBuffers.end(); ++shaderBufferIter)
  165. {
  166. for(size_t i = 0; i < shaderBufferIter->variables.size(); i++)
  167. {
  168. const D3D11_SHADER_VARIABLE_DESC& variableDesc = shaderBufferIter->variables[i];
  169. const D3D11_SHADER_TYPE_DESC& variableType = shaderBufferIter->variableTypes[i];
  170. String name = variableDesc.Name;
  171. if (name.at(0) == '$')
  172. name.erase(name.begin());
  173. // Also trim the '[0]' suffix if it exists, we will add our own indexing later
  174. if (StringUtil::endsWith(name, "[0]", false))
  175. name.erase(name.size() - 3);
  176. UINT32 paramIndex = (UINT32)i;
  177. // TODO - Need to add support for more types. ESPECIALLY TEXTURES & STRUCTS!
  178. if(variableType.Type == D3D_SVT_FLOAT || variableType.Type == D3D_SVT_INT || variableType.Type == D3D_SVT_BOOL || variableType.Type == D3D_SVT_SAMPLER1D ||
  179. variableType.Type == D3D_SVT_SAMPLER2D || variableType.Type == D3D_SVT_SAMPLER3D || variableType.Type == D3D_SVT_SAMPLERCUBE)
  180. {
  181. GpuConstantDefinition def;
  182. def.logicalIndex = paramIndex;
  183. // populate type, array size & element size
  184. populateParameterDefinition(variableDesc, variableType, def);
  185. if(def.isSampler())
  186. {
  187. def.physicalIndex = variableDesc.StartSampler;
  188. mSamplerLogicalToPhysical->map.insert(
  189. GpuLogicalIndexUseMap::value_type(paramIndex,
  190. GpuLogicalIndexUse(def.physicalIndex, def.arraySize, GPV_GLOBAL)));
  191. mSamplerLogicalToPhysical->bufferSize = std::max(mSamplerLogicalToPhysical->bufferSize, def.physicalIndex + def.arraySize);
  192. mConstantDefs->samplerCount = mSamplerLogicalToPhysical->bufferSize;
  193. // TODO - Add textures!
  194. CM_EXCEPT(NotImplementedException, "Add support for texture parameters!");
  195. }
  196. else
  197. {
  198. if (def.isFloat())
  199. {
  200. def.physicalIndex = variableDesc.StartOffset;
  201. mFloatLogicalToPhysical->map.insert(
  202. GpuLogicalIndexUseMap::value_type(paramIndex,
  203. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  204. mFloatLogicalToPhysical->bufferSize = std::max(mFloatLogicalToPhysical->bufferSize, def.physicalIndex + def.arraySize);
  205. mConstantDefs->floatBufferSize = mFloatLogicalToPhysical->bufferSize;
  206. }
  207. else
  208. {
  209. def.physicalIndex = variableDesc.StartOffset;
  210. mIntLogicalToPhysical->map.insert(
  211. GpuLogicalIndexUseMap::value_type(paramIndex,
  212. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  213. mIntLogicalToPhysical->bufferSize = std::max(mIntLogicalToPhysical->bufferSize, def.physicalIndex + def.arraySize);
  214. mConstantDefs->intBufferSize = mIntLogicalToPhysical->bufferSize;
  215. }
  216. }
  217. mConstantDefs->map.insert(GpuConstantDefinitionMap::value_type(name, def));
  218. // Now deal with arrays
  219. mConstantDefs->generateConstantDefinitionArrayEntries(name, def);
  220. }
  221. }
  222. }
  223. }
  224. void D3D11HLSLProgram::populateConstantBufferParameters(ID3D11ShaderReflectionConstantBuffer* bufferReflection)
  225. {
  226. D3D11_SHADER_BUFFER_DESC constantBufferDesc;
  227. HRESULT hr = bufferReflection->GetDesc(&constantBufferDesc);
  228. if (FAILED(hr))
  229. CM_EXCEPT(RenderingAPIException, "Failed to retrieve HLSL constant buffer description.");
  230. if(constantBufferDesc.Type != D3D_CBUFFER_TYPE::D3D_CT_CBUFFER && constantBufferDesc.Type != D3D_CBUFFER_TYPE::D3D_CT_TBUFFER)
  231. {
  232. LOGDBG("D3D11 HLSL parsing: Unsupported constant buffer type, skipping. Type: " + toString(constantBufferDesc.Type));
  233. return;
  234. }
  235. mShaderBuffers.push_back(D3D11_ShaderBufferDesc());
  236. D3D11_ShaderBufferDesc& newShaderBufferDesc = *mShaderBuffers.end();
  237. for(UINT32 j = 0; j < constantBufferDesc.Variables; j++)
  238. {
  239. ID3D11ShaderReflectionVariable* varRef;
  240. varRef = bufferReflection->GetVariableByIndex(j);
  241. D3D11_SHADER_VARIABLE_DESC varDesc;
  242. HRESULT hr = varRef->GetDesc(&varDesc);
  243. if (FAILED(hr))
  244. CM_EXCEPT(RenderingAPIException, "Failed to retrieve HLSL constant buffer variable description.");
  245. ID3D11ShaderReflectionType* varRefType;
  246. varRefType = varRef->GetType();
  247. D3D11_SHADER_TYPE_DESC varTypeDesc;
  248. varRefType->GetDesc(&varTypeDesc);
  249. switch(varTypeDesc.Type)
  250. {
  251. case D3D_SVT_FLOAT:
  252. case D3D_SVT_INT:
  253. case D3D_SVT_SAMPLER1D:
  254. case D3D_SVT_SAMPLER2D:
  255. case D3D_SVT_SAMPLER3D:
  256. case D3D_SVT_SAMPLERCUBE: // TODO - Need to add support for other types!
  257. newShaderBufferDesc.variables.push_back(varDesc);
  258. newShaderBufferDesc.variableTypes.push_back(varTypeDesc);
  259. default:
  260. CM_EXCEPT(RenderingAPIException, "Unsupported shader variable type!");
  261. }
  262. }
  263. }
  264. void D3D11HLSLProgram::populateParameterDefinition(const D3D11_SHADER_VARIABLE_DESC& paramDesc, const D3D11_SHADER_TYPE_DESC& paramType, GpuConstantDefinition& def) const
  265. {
  266. def.arraySize = paramType.Elements + 1;
  267. switch(paramType.Type)
  268. {
  269. case D3D_SVT_SAMPLER1D:
  270. def.constType = GCT_SAMPLER1D;
  271. def.elementSize = paramDesc.SamplerSize / def.arraySize;
  272. break;
  273. case D3D_SVT_SAMPLER2D:
  274. CM_EXCEPT(NotImplementedException, "Break here because I want to check what is the elementSize of the sampler. It has to be 1.");
  275. def.constType = GCT_SAMPLER2D;
  276. def.elementSize = paramDesc.SamplerSize / def.arraySize;
  277. break;
  278. case D3D_SVT_SAMPLER3D:
  279. def.constType = GCT_SAMPLER3D;
  280. def.elementSize = paramDesc.SamplerSize / def.arraySize;
  281. break;
  282. case D3D_SVT_SAMPLERCUBE:
  283. def.constType = GCT_SAMPLERCUBE;
  284. def.elementSize = paramDesc.SamplerSize / def.arraySize;
  285. break;
  286. case D3D_SVT_INT:
  287. switch(paramType.Columns)
  288. {
  289. case 1:
  290. def.constType = GCT_INT1;
  291. def.elementSize = paramDesc.Size / def.arraySize;
  292. break;
  293. case 2:
  294. def.constType = GCT_INT2;
  295. def.elementSize = paramDesc.Size / def.arraySize;
  296. break;
  297. case 3:
  298. def.constType = GCT_INT3;
  299. def.elementSize = paramDesc.Size / def.arraySize;
  300. break;
  301. case 4:
  302. def.constType = GCT_INT4;
  303. def.elementSize = paramDesc.Size / def.arraySize;
  304. break;
  305. } // columns
  306. break;
  307. case D3D_SVT_FLOAT:
  308. CM_EXCEPT(NotImplementedException, "Break here because I want to check if paramDesc.Size is size per element or total size of the array.");
  309. switch(paramType.Rows)
  310. {
  311. case 1:
  312. switch(paramType.Columns)
  313. {
  314. case 1:
  315. def.constType = GCT_FLOAT1;
  316. def.elementSize = paramDesc.Size / def.arraySize;
  317. break;
  318. case 2:
  319. def.constType = GCT_FLOAT2;
  320. def.elementSize = paramDesc.Size / def.arraySize;
  321. break;
  322. case 3:
  323. def.constType = GCT_FLOAT3;
  324. def.elementSize = paramDesc.Size / def.arraySize;
  325. break;
  326. case 4:
  327. def.constType = GCT_FLOAT4;
  328. def.elementSize = paramDesc.Size / def.arraySize;
  329. break;
  330. } // columns
  331. break;
  332. case 2:
  333. switch(paramType.Columns)
  334. {
  335. case 2:
  336. def.constType = GCT_MATRIX_2X2;
  337. def.elementSize = paramDesc.Size / def.arraySize;
  338. break;
  339. case 3:
  340. def.constType = GCT_MATRIX_2X3;
  341. def.elementSize = paramDesc.Size / def.arraySize;
  342. break;
  343. case 4:
  344. def.constType = GCT_MATRIX_2X4;
  345. def.elementSize = paramDesc.Size / def.arraySize;
  346. break;
  347. } // columns
  348. break;
  349. case 3:
  350. switch(paramType.Columns)
  351. {
  352. case 2:
  353. def.constType = GCT_MATRIX_3X2;
  354. def.elementSize = paramDesc.Size / def.arraySize;
  355. break;
  356. case 3:
  357. def.constType = GCT_MATRIX_3X3;
  358. def.elementSize = paramDesc.Size / def.arraySize;
  359. break;
  360. case 4:
  361. def.constType = GCT_MATRIX_3X4;
  362. def.elementSize = paramDesc.Size / def.arraySize;
  363. break;
  364. } // columns
  365. break;
  366. case 4:
  367. switch(paramType.Columns)
  368. {
  369. case 2:
  370. def.constType = GCT_MATRIX_4X2;
  371. def.elementSize = paramDesc.Size / def.arraySize;
  372. break;
  373. case 3:
  374. def.constType = GCT_MATRIX_4X3;
  375. def.elementSize = paramDesc.Size / def.arraySize;
  376. break;
  377. case 4:
  378. def.constType = GCT_MATRIX_4X4;
  379. def.elementSize = paramDesc.Size / def.arraySize;
  380. break;
  381. } // columns
  382. break;
  383. } // rows
  384. break;
  385. default:
  386. break;
  387. };
  388. }
  389. void D3D11HLSLProgram::createConstantBuffers()
  390. {
  391. mConstantBuffers.clear();
  392. for(auto shaderBufferIter = mShaderBuffers.begin(); shaderBufferIter != mShaderBuffers.end(); ++shaderBufferIter)
  393. {
  394. HardwareConstantBufferPtr constantBuffer = HardwareBufferManager::instance().createConstantBuffer(shaderBufferIter->desc.Size, HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY);
  395. mConstantBuffers.push_back(constantBuffer);
  396. }
  397. }
  398. }