CmD3D11HLSLProgram.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. #include "CmD3D11HLSLProgram.h"
  2. #include "CmRenderSystemManager.h"
  3. #include "CmRenderSystem.h"
  4. #include "CmGpuProgramManager.h"
  5. #include "CmD3D11GpuProgram.h"
  6. #include "CmHardwareBufferManager.h"
  7. #include "CmHardwareConstantBuffer.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. D3D11GpuVertexProgramPtr vertProgram = std::static_pointer_cast<D3D11GpuVertexProgram>(mAssemblerProgram);
  34. vertProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  35. break;
  36. case GPT_FRAGMENT_PROGRAM:
  37. D3D11GpuFragmentProgramPtr fragProgram = std::static_pointer_cast<D3D11GpuFragmentProgram>(mAssemblerProgram);
  38. fragProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  39. break;
  40. case GPT_GEOMETRY_PROGRAM:
  41. D3D11GpuGeometryProgramPtr geomProgram = std::static_pointer_cast<D3D11GpuGeometryProgram>(mAssemblerProgram);
  42. geomProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  43. break;
  44. case GPT_HULL_PROGRAM:
  45. D3D11GpuHullProgramPtr hullProgram = std::static_pointer_cast<D3D11GpuHullProgram>(mAssemblerProgram);
  46. hullProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  47. break;
  48. case GPT_DOMAIN_PROGRAM:
  49. D3D11GpuDomainProgramPtr domainProgram = std::static_pointer_cast<D3D11GpuDomainProgram>(mAssemblerProgram);
  50. domainProgram->loadFromMicrocode(D3D11RenderSystem::getPrimaryDevice(), microcode);
  51. break;
  52. }
  53. SAFE_RELEASE(microcode);
  54. }
  55. void D3D11HLSLProgram::unload_internal()
  56. {
  57. mAssemblerProgram = nullptr;
  58. mShaderBuffers.clear();
  59. mInputParameters.clear();
  60. mOutputParameters.clear();
  61. mConstantBuffers.clear();
  62. mMicrocode.clear();
  63. }
  64. const String& D3D11HLSLProgram::getLanguage() const
  65. {
  66. static String name = "hlsl";
  67. return name;
  68. }
  69. bool D3D11HLSLProgram::isSupported() const
  70. {
  71. RenderSystem* rs = RenderSystemManager::getActive();
  72. return rs->getCapabilities_internal()->isShaderProfileSupported(getSyntaxCode()) && HighLevelGpuProgram::isSupported();
  73. }
  74. ID3DBlob* D3D11HLSLProgram::compileMicrocode()
  75. {
  76. // TODO - Preprocessor defines aren't supported
  77. UINT compileFlags = 0;
  78. #if defined(CM_DEBUG_MODE)
  79. compileFlags |= D3DCOMPILE_DEBUG;
  80. compileFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
  81. #endif
  82. if (mColumnMajorMatrices)
  83. compileFlags |= D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR;
  84. else
  85. compileFlags |= D3DCOMPILE_PACK_MATRIX_ROW_MAJOR;
  86. if (mEnableBackwardsCompatibility)
  87. compileFlags |= D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY;
  88. ID3DBlob* microCode = nullptr;
  89. ID3DBlob* errors = nullptr;
  90. HRESULT hr = D3DCompile(
  91. mSource.c_str(), // [in] Pointer to the shader in memory.
  92. mSource.size(), // [in] Size of the shader in memory.
  93. nullptr, // [in] The name of the file that contains the shader code.
  94. nullptr, // [in] Optional. Pointer to a NULL-terminated array of macro definitions. See D3D_SHADER_MACRO. If not used, set this to NULL.
  95. 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.
  96. mEntryPoint.c_str(),// [in] Name of the shader-entrypoint function where shader execution begins.
  97. mSyntaxCode.c_str(),// [in] A string that specifies the shader model; can be any profile in shader model 4 or higher.
  98. compileFlags, // [in] Effect compile flags - no D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY at the first try...
  99. 0, // [in] Effect compile flags
  100. &microCode, // [out] A pointer to an ID3DBlob Interface which contains the compiled shader, as well as any embedded debug and symbol-table information.
  101. &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.
  102. );
  103. if (FAILED(hr))
  104. {
  105. String message = "Cannot assemble D3D11 high-level shader. Errors:\n" +
  106. String(static_cast<const char*>(errors->GetBufferPointer()));
  107. SAFE_RELEASE(errors);
  108. CM_EXCEPT(RenderingAPIException, message);
  109. }
  110. SAFE_RELEASE(errors);
  111. return microCode;
  112. }
  113. void D3D11HLSLProgram::populateParametersAndConstants(ID3DBlob* microcode)
  114. {
  115. assert(microcode != nullptr);
  116. mShaderBuffers.clear();
  117. mInputParameters.clear();
  118. mOutputParameters.clear();
  119. const char* commentString = nullptr;
  120. ID3DBlob* pIDisassembly = nullptr;
  121. char* pDisassembly = nullptr;
  122. HRESULT hr = D3DDisassemble((UINT*)microcode->GetBufferPointer(),
  123. microcode->GetBufferSize(), D3D_DISASM_ENABLE_COLOR_CODE, commentString, &pIDisassembly);
  124. const char* assemblyCode = static_cast<const char*>(pIDisassembly->GetBufferPointer());
  125. if (FAILED(hr))
  126. CM_EXCEPT(RenderingAPIException, "Unable to disassemble shader.");
  127. ID3D11ShaderReflection* shaderReflection;
  128. HRESULT hr = D3DReflect((void*)microcode->GetBufferPointer(), microcode->GetBufferSize(),
  129. IID_ID3D11ShaderReflection, (void**)&shaderReflection);
  130. if (FAILED(hr))
  131. CM_EXCEPT(RenderingAPIException, "Cannot reflect D3D11 high-level shader.");
  132. D3D11_SHADER_DESC shaderDesc;
  133. hr = shaderReflection->GetDesc(&shaderDesc);
  134. if (FAILED(hr))
  135. CM_EXCEPT(RenderingAPIException, "Cannot reflect D3D11 high-level shader.");
  136. mInputParameters.resize(shaderDesc.InputParameters);
  137. for (UINT32 i = 0; i < shaderDesc.InputParameters; i++)
  138. shaderReflection->GetInputParameterDesc(i, &(mInputParameters[i]));
  139. mOutputParameters.resize(shaderDesc.OutputParameters);
  140. for (UINT32 i = 0; i < shaderDesc.OutputParameters; i++)
  141. shaderReflection->GetOutputParameterDesc(i, &(mOutputParameters[i]));
  142. mShaderBuffers.resize(shaderDesc.ConstantBuffers);
  143. for(UINT32 i = 0; i < shaderDesc.ConstantBuffers; i++)
  144. {
  145. ID3D11ShaderReflectionConstantBuffer* shaderReflectionConstantBuffer;
  146. shaderReflectionConstantBuffer = shaderReflection->GetConstantBufferByIndex(i);
  147. populateConstantBufferParameters(shaderReflectionConstantBuffer);
  148. }
  149. shaderReflection->Release();
  150. }
  151. void D3D11HLSLProgram::buildConstantDefinitions() const
  152. {
  153. createParameterMappingStructures(true);
  154. for(auto shaderBufferIter = mShaderBuffers.begin(); shaderBufferIter != mShaderBuffers.end; ++shaderBufferIter)
  155. {
  156. for(size_t i = 0; i < shaderBufferIter->variables.size(); i++)
  157. {
  158. const D3D11_SHADER_VARIABLE_DESC& variableDesc = shaderBufferIter->variables[i];
  159. const D3D11_SHADER_TYPE_DESC& variableType = shaderBufferIter->variableTypes[i];
  160. String name = variableDesc.Name;
  161. if (name.at(0) == '$')
  162. name.erase(name.begin());
  163. // Also trim the '[0]' suffix if it exists, we will add our own indexing later
  164. if (StringUtil::endsWith(name, "[0]", false))
  165. name.erase(name.size() - 3);
  166. UINT32 paramIndex = (UINT32)i;
  167. // TODO - Need to add support for more types. ESPECIALLY TEXTURES & STRUCTS!
  168. if(variableType.Type == D3D_SVT_FLOAT || variableType.Type == D3D_SVT_INT || variableType.Type == D3D_SVT_BOOL || variableType.Type == D3D_SVT_SAMPLER1D ||
  169. variableType.Type == D3D_SVT_SAMPLER2D || variableType.Type == D3D_SVT_SAMPLER3D || variableType.Type == D3D_SVT_SAMPLERCUBE)
  170. {
  171. GpuConstantDefinition def;
  172. def.logicalIndex = paramIndex;
  173. // populate type, array size & element size
  174. populateParameterDefinition(variableDesc, variableType, def);
  175. if(def.isSampler())
  176. {
  177. def.physicalIndex = variableDesc.StartSampler;
  178. CM_LOCK_MUTEX(mSamplerLogicalToPhysical->mutex)
  179. mSamplerLogicalToPhysical->map.insert(
  180. GpuLogicalIndexUseMap::value_type(paramIndex,
  181. GpuLogicalIndexUse(def.physicalIndex, def.arraySize, GPV_GLOBAL)));
  182. mSamplerLogicalToPhysical->bufferSize = std::max(mSamplerLogicalToPhysical->bufferSize, def.physicalIndex + def.arraySize);
  183. mConstantDefs->samplerCount = mSamplerLogicalToPhysical->bufferSize;
  184. }
  185. else
  186. {
  187. if (def.isFloat())
  188. {
  189. def.physicalIndex = variableDesc.StartOffset;
  190. CM_LOCK_MUTEX(mFloatLogicalToPhysical->mutex)
  191. mFloatLogicalToPhysical->map.insert(
  192. GpuLogicalIndexUseMap::value_type(paramIndex,
  193. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  194. mFloatLogicalToPhysical->bufferSize = std::max(mFloatLogicalToPhysical->bufferSize, def.physicalIndex + def.arraySize);
  195. mConstantDefs->floatBufferSize = mFloatLogicalToPhysical->bufferSize;
  196. }
  197. else
  198. {
  199. def.physicalIndex = variableDesc.StartOffset;
  200. CM_LOCK_MUTEX(mIntLogicalToPhysical->mutex)
  201. mIntLogicalToPhysical->map.insert(
  202. GpuLogicalIndexUseMap::value_type(paramIndex,
  203. GpuLogicalIndexUse(def.physicalIndex, def.arraySize * def.elementSize, GPV_GLOBAL)));
  204. mIntLogicalToPhysical->bufferSize = std::max(mIntLogicalToPhysical->bufferSize, def.physicalIndex + def.arraySize);
  205. mConstantDefs->intBufferSize = mIntLogicalToPhysical->bufferSize;
  206. }
  207. }
  208. mConstantDefs->map.insert(GpuConstantDefinitionMap::value_type(name, def));
  209. // Now deal with arrays
  210. mConstantDefs->generateConstantDefinitionArrayEntries(name, def);
  211. }
  212. }
  213. }
  214. }
  215. void D3D11HLSLProgram::populateConstantBufferParameters(ID3D11ShaderReflectionConstantBuffer* bufferReflection)
  216. {
  217. D3D11_SHADER_BUFFER_DESC constantBufferDesc;
  218. HRESULT hr = bufferReflection->GetDesc(&constantBufferDesc);
  219. if (FAILED(hr))
  220. CM_EXCEPT(RenderingAPIException, "Failed to retrieve HLSL constant buffer description.");
  221. if(constantBufferDesc.Type != D3D_CBUFFER_TYPE::D3D_CT_CBUFFER && constantBufferDesc.Type != D3D_CBUFFER_TYPE::D3D_CT_TBUFFER)
  222. {
  223. LOGDBG("D3D11 HLSL parsing: Unsupported constant buffer type, skipping. Type: " + toString(constantBufferDesc.Type));
  224. return;
  225. }
  226. mShaderBuffers.push_back(D3D11_ShaderBufferDesc());
  227. D3D11_ShaderBufferDesc& newShaderBufferDesc = *mShaderBuffers.end();
  228. for(UINT32 j = 0; j < constantBufferDesc.Variables; j++)
  229. {
  230. ID3D11ShaderReflectionVariable* varRef;
  231. varRef = bufferReflection->GetVariableByIndex(j);
  232. D3D11_SHADER_VARIABLE_DESC varDesc;
  233. HRESULT hr = varRef->GetDesc(&varDesc);
  234. if (FAILED(hr))
  235. CM_EXCEPT(RenderingAPIException, "Failed to retrieve HLSL constant buffer variable description.");
  236. ID3D11ShaderReflectionType* varRefType;
  237. varRefType = varRef->GetType();
  238. D3D11_SHADER_TYPE_DESC varTypeDesc;
  239. varRefType->GetDesc(&varTypeDesc);
  240. switch(varTypeDesc.Type)
  241. {
  242. case D3D_SVT_FLOAT:
  243. case D3D_SVT_INT:
  244. case D3D_SVT_SAMPLER1D:
  245. case D3D_SVT_SAMPLER2D:
  246. case D3D_SVT_SAMPLER3D:
  247. case D3D_SVT_SAMPLERCUBE: // TODO - Need to add support for other types!
  248. newShaderBufferDesc.variables.push_back(varDesc);
  249. newShaderBufferDesc.variableTypes.push_back(varTypeDesc);
  250. default:
  251. CM_EXCEPT(RenderingAPIException, "Unsupported shader variable type!");
  252. }
  253. }
  254. }
  255. void D3D11HLSLProgram::populateParameterDefinition(const D3D11_SHADER_VARIABLE_DESC& paramDesc, const D3D11_SHADER_TYPE_DESC& paramType, GpuConstantDefinition& def) const
  256. {
  257. def.arraySize = paramType.Elements + 1;
  258. switch(paramType.Type)
  259. {
  260. case D3D_SVT_SAMPLER1D:
  261. def.constType = GCT_SAMPLER1D;
  262. def.elementSize = paramDesc.SamplerSize / def.arraySize;
  263. break;
  264. case D3D_SVT_SAMPLER2D:
  265. CM_EXCEPT(NotImplementedException, "Break here because I want to check what is the elementSize of the sampler. It has to be 1.");
  266. def.constType = GCT_SAMPLER2D;
  267. def.elementSize = paramDesc.SamplerSize / def.arraySize;
  268. break;
  269. case D3D_SVT_SAMPLER3D:
  270. def.constType = GCT_SAMPLER3D;
  271. def.elementSize = paramDesc.SamplerSize / def.arraySize;
  272. break;
  273. case D3D_SVT_SAMPLERCUBE:
  274. def.constType = GCT_SAMPLERCUBE;
  275. def.elementSize = paramDesc.SamplerSize / def.arraySize;
  276. break;
  277. case D3D_SVT_INT:
  278. switch(paramType.Columns)
  279. {
  280. case 1:
  281. def.constType = GCT_INT1;
  282. def.elementSize = paramDesc.Size / def.arraySize;
  283. break;
  284. case 2:
  285. def.constType = GCT_INT2;
  286. def.elementSize = paramDesc.Size / def.arraySize;
  287. break;
  288. case 3:
  289. def.constType = GCT_INT3;
  290. def.elementSize = paramDesc.Size / def.arraySize;
  291. break;
  292. case 4:
  293. def.constType = GCT_INT4;
  294. def.elementSize = paramDesc.Size / def.arraySize;
  295. break;
  296. } // columns
  297. break;
  298. case D3D_SVT_FLOAT:
  299. CM_EXCEPT(NotImplementedException, "Break here because I want to check if paramDesc.Size is size per element or total size of the array.");
  300. switch(paramType.Rows)
  301. {
  302. case 1:
  303. switch(paramType.Columns)
  304. {
  305. case 1:
  306. def.constType = GCT_FLOAT1;
  307. def.elementSize = paramDesc.Size / def.arraySize;
  308. break;
  309. case 2:
  310. def.constType = GCT_FLOAT2;
  311. def.elementSize = paramDesc.Size / def.arraySize;
  312. break;
  313. case 3:
  314. def.constType = GCT_FLOAT3;
  315. def.elementSize = paramDesc.Size / def.arraySize;
  316. break;
  317. case 4:
  318. def.constType = GCT_FLOAT4;
  319. def.elementSize = paramDesc.Size / def.arraySize;
  320. break;
  321. } // columns
  322. break;
  323. case 2:
  324. switch(paramType.Columns)
  325. {
  326. case 2:
  327. def.constType = GCT_MATRIX_2X2;
  328. def.elementSize = paramDesc.Size / def.arraySize;
  329. break;
  330. case 3:
  331. def.constType = GCT_MATRIX_2X3;
  332. def.elementSize = paramDesc.Size / def.arraySize;
  333. break;
  334. case 4:
  335. def.constType = GCT_MATRIX_2X4;
  336. def.elementSize = paramDesc.Size / def.arraySize;
  337. break;
  338. } // columns
  339. break;
  340. case 3:
  341. switch(paramType.Columns)
  342. {
  343. case 2:
  344. def.constType = GCT_MATRIX_3X2;
  345. def.elementSize = paramDesc.Size / def.arraySize;
  346. break;
  347. case 3:
  348. def.constType = GCT_MATRIX_3X3;
  349. def.elementSize = paramDesc.Size / def.arraySize;
  350. break;
  351. case 4:
  352. def.constType = GCT_MATRIX_3X4;
  353. def.elementSize = paramDesc.Size / def.arraySize;
  354. break;
  355. } // columns
  356. break;
  357. case 4:
  358. switch(paramType.Columns)
  359. {
  360. case 2:
  361. def.constType = GCT_MATRIX_4X2;
  362. def.elementSize = paramDesc.Size / def.arraySize;
  363. break;
  364. case 3:
  365. def.constType = GCT_MATRIX_4X3;
  366. def.elementSize = paramDesc.Size / def.arraySize;
  367. break;
  368. case 4:
  369. def.constType = GCT_MATRIX_4X4;
  370. def.elementSize = paramDesc.Size / def.arraySize;
  371. break;
  372. } // columns
  373. break;
  374. } // rows
  375. break;
  376. default:
  377. break;
  378. };
  379. }
  380. void D3D11HLSLProgram::createConstantBuffers()
  381. {
  382. mConstantBuffers.clear();
  383. for(auto shaderBufferIter = mShaderBuffers.begin(); shaderBufferIter != mShaderBuffers.end; ++shaderBufferIter)
  384. {
  385. HardwareConstantBufferPtr constantBuffer = HardwareBufferManager::instance().createConstantBuffer(shaderBufferIter->desc.Size, HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY);
  386. mConstantBuffers.push_back(constantBuffer);
  387. }
  388. }
  389. }