d3d11-shader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "d3d11-subsystem.hpp"
  15. #include "d3d11-shaderprocessor.hpp"
  16. #include <graphics/vec2.h>
  17. #include <graphics/vec3.h>
  18. #include <graphics/matrix3.h>
  19. #include <graphics/matrix4.h>
  20. void gs_vertex_shader::GetBuffersExpected(
  21. const vector<D3D11_INPUT_ELEMENT_DESC> &inputs)
  22. {
  23. for (size_t i = 0; i < inputs.size(); i++) {
  24. const D3D11_INPUT_ELEMENT_DESC &input = inputs[i];
  25. if (strcmp(input.SemanticName, "NORMAL") == 0)
  26. hasNormals = true;
  27. else if (strcmp(input.SemanticName, "TANGENT") == 0)
  28. hasTangents = true;
  29. else if (strcmp(input.SemanticName, "COLOR") == 0)
  30. hasColors = true;
  31. else if (strcmp(input.SemanticName, "TEXCOORD") == 0)
  32. nTexUnits++;
  33. }
  34. }
  35. gs_vertex_shader::gs_vertex_shader(gs_device_t *device, const char *file,
  36. const char *shaderString)
  37. : gs_shader(device, gs_type::gs_vertex_shader, GS_SHADER_VERTEX),
  38. hasNormals(false),
  39. hasColors(false),
  40. hasTangents(false),
  41. nTexUnits(0)
  42. {
  43. ShaderProcessor processor(device);
  44. ComPtr<ID3D10Blob> shaderBlob;
  45. string outputString;
  46. HRESULT hr;
  47. processor.Process(shaderString, file);
  48. processor.BuildString(outputString);
  49. processor.BuildParams(params);
  50. processor.BuildInputLayout(layoutData);
  51. GetBuffersExpected(layoutData);
  52. BuildConstantBuffer();
  53. Compile(outputString.c_str(), file, "vs_4_0", shaderBlob.Assign());
  54. data.resize(shaderBlob->GetBufferSize());
  55. memcpy(&data[0], shaderBlob->GetBufferPointer(), data.size());
  56. hr = device->device->CreateVertexShader(data.data(), data.size(), NULL,
  57. shader.Assign());
  58. if (FAILED(hr))
  59. throw HRError("Failed to create vertex shader", hr);
  60. const UINT layoutSize = (UINT)layoutData.size();
  61. if (layoutSize > 0) {
  62. hr = device->device->CreateInputLayout(layoutData.data(),
  63. (UINT)layoutSize,
  64. data.data(), data.size(),
  65. layout.Assign());
  66. if (FAILED(hr))
  67. throw HRError("Failed to create input layout", hr);
  68. }
  69. viewProj = gs_shader_get_param_by_name(this, "ViewProj");
  70. world = gs_shader_get_param_by_name(this, "World");
  71. }
  72. gs_pixel_shader::gs_pixel_shader(gs_device_t *device, const char *file,
  73. const char *shaderString)
  74. : gs_shader(device, gs_type::gs_pixel_shader, GS_SHADER_PIXEL)
  75. {
  76. ShaderProcessor processor(device);
  77. ComPtr<ID3D10Blob> shaderBlob;
  78. string outputString;
  79. HRESULT hr;
  80. processor.Process(shaderString, file);
  81. processor.BuildString(outputString);
  82. processor.BuildParams(params);
  83. processor.BuildSamplers(samplers);
  84. BuildConstantBuffer();
  85. Compile(outputString.c_str(), file, "ps_4_0", shaderBlob.Assign());
  86. data.resize(shaderBlob->GetBufferSize());
  87. memcpy(&data[0], shaderBlob->GetBufferPointer(), data.size());
  88. hr = device->device->CreatePixelShader(data.data(), data.size(), NULL,
  89. shader.Assign());
  90. if (FAILED(hr))
  91. throw HRError("Failed to create pixel shader", hr);
  92. }
  93. /*
  94. * Shader compilers will pack constants in to single registers when possible.
  95. * For example:
  96. *
  97. * uniform float3 test1;
  98. * uniform float test2;
  99. *
  100. * will inhabit a single constant register (c0.xyz for 'test1', and c0.w for
  101. * 'test2')
  102. *
  103. * However, if two constants cannot inhabit the same register, the second one
  104. * must begin at a new register, for example:
  105. *
  106. * uniform float2 test1;
  107. * uniform float3 test2;
  108. *
  109. * 'test1' will inhabit register constant c0.xy. However, because there's no
  110. * room for 'test2, it must use a new register constant entirely (c1.xyz).
  111. *
  112. * So if we want to calculate the position of the constants in the constant
  113. * buffer, we must take this in to account.
  114. */
  115. void gs_shader::BuildConstantBuffer()
  116. {
  117. for (size_t i = 0; i < params.size(); i++) {
  118. gs_shader_param &param = params[i];
  119. size_t size = 0;
  120. switch (param.type) {
  121. case GS_SHADER_PARAM_BOOL:
  122. case GS_SHADER_PARAM_INT:
  123. case GS_SHADER_PARAM_FLOAT:
  124. size = sizeof(float);
  125. break;
  126. case GS_SHADER_PARAM_INT2:
  127. case GS_SHADER_PARAM_VEC2:
  128. size = sizeof(vec2);
  129. break;
  130. case GS_SHADER_PARAM_INT3:
  131. case GS_SHADER_PARAM_VEC3:
  132. size = sizeof(float) * 3;
  133. break;
  134. case GS_SHADER_PARAM_INT4:
  135. case GS_SHADER_PARAM_VEC4:
  136. size = sizeof(vec4);
  137. break;
  138. case GS_SHADER_PARAM_MATRIX4X4:
  139. size = sizeof(float) * 4 * 4;
  140. break;
  141. case GS_SHADER_PARAM_TEXTURE:
  142. case GS_SHADER_PARAM_STRING:
  143. case GS_SHADER_PARAM_UNKNOWN:
  144. continue;
  145. }
  146. if (param.arrayCount)
  147. size *= param.arrayCount;
  148. /* checks to see if this constant needs to start at a new
  149. * register */
  150. if (size && (constantSize & 15) != 0) {
  151. size_t alignMax = (constantSize + 15) & ~15;
  152. if ((size + constantSize) > alignMax)
  153. constantSize = alignMax;
  154. }
  155. param.pos = constantSize;
  156. constantSize += size;
  157. }
  158. memset(&bd, 0, sizeof(bd));
  159. if (constantSize) {
  160. HRESULT hr;
  161. bd.ByteWidth = (constantSize + 15) & 0xFFFFFFF0; /* align */
  162. bd.Usage = D3D11_USAGE_DYNAMIC;
  163. bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
  164. bd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  165. hr = device->device->CreateBuffer(&bd, NULL,
  166. constants.Assign());
  167. if (FAILED(hr))
  168. throw HRError("Failed to create constant buffer", hr);
  169. }
  170. for (size_t i = 0; i < params.size(); i++)
  171. gs_shader_set_default(&params[i]);
  172. }
  173. void gs_shader::Compile(const char *shaderString, const char *file,
  174. const char *target, ID3D10Blob **shader)
  175. {
  176. ComPtr<ID3D10Blob> errorsBlob;
  177. HRESULT hr;
  178. if (!shaderString)
  179. throw "No shader string specified";
  180. hr = device->d3dCompile(shaderString, strlen(shaderString), file, NULL,
  181. NULL, "main", target,
  182. D3D10_SHADER_OPTIMIZATION_LEVEL1, 0, shader,
  183. errorsBlob.Assign());
  184. if (FAILED(hr)) {
  185. if (errorsBlob != NULL && errorsBlob->GetBufferSize())
  186. throw ShaderError(errorsBlob, hr);
  187. else
  188. throw HRError("Failed to compile shader", hr);
  189. }
  190. #ifdef DISASSEMBLE_SHADERS
  191. ComPtr<ID3D10Blob> asmBlob;
  192. if (!device->d3dDisassemble)
  193. return;
  194. hr = device->d3dDisassemble((*shader)->GetBufferPointer(),
  195. (*shader)->GetBufferSize(), 0, nullptr,
  196. &asmBlob);
  197. if (SUCCEEDED(hr) && !!asmBlob && asmBlob->GetBufferSize()) {
  198. blog(LOG_INFO, "=============================================");
  199. blog(LOG_INFO, "Disassembly output for shader '%s':\n%s", file,
  200. asmBlob->GetBufferPointer());
  201. }
  202. #endif
  203. }
  204. inline void gs_shader::UpdateParam(vector<uint8_t> &constData,
  205. gs_shader_param &param, bool &upload)
  206. {
  207. if (param.type != GS_SHADER_PARAM_TEXTURE) {
  208. if (!param.curValue.size())
  209. throw "Not all shader parameters were set";
  210. /* padding in case the constant needs to start at a new
  211. * register */
  212. if (param.pos > constData.size()) {
  213. uint8_t zero = 0;
  214. constData.insert(constData.end(),
  215. param.pos - constData.size(), zero);
  216. }
  217. constData.insert(constData.end(), param.curValue.begin(),
  218. param.curValue.end());
  219. if (param.changed) {
  220. upload = true;
  221. param.changed = false;
  222. }
  223. } else if (param.curValue.size() == sizeof(gs_texture_t *)) {
  224. gs_texture_t *tex;
  225. memcpy(&tex, param.curValue.data(), sizeof(gs_texture_t *));
  226. device_load_texture(device, tex, param.textureID);
  227. if (param.nextSampler) {
  228. ID3D11SamplerState *state = param.nextSampler->state;
  229. device->context->PSSetSamplers(param.textureID, 1,
  230. &state);
  231. param.nextSampler = nullptr;
  232. }
  233. }
  234. }
  235. void gs_shader::UploadParams()
  236. {
  237. vector<uint8_t> constData;
  238. bool upload = false;
  239. constData.reserve(constantSize);
  240. for (size_t i = 0; i < params.size(); i++)
  241. UpdateParam(constData, params[i], upload);
  242. if (constData.size() != constantSize)
  243. throw "Invalid constant data size given to shader";
  244. if (upload) {
  245. D3D11_MAPPED_SUBRESOURCE map;
  246. HRESULT hr;
  247. hr = device->context->Map(constants, 0, D3D11_MAP_WRITE_DISCARD,
  248. 0, &map);
  249. if (FAILED(hr))
  250. throw HRError("Could not lock constant buffer", hr);
  251. memcpy(map.pData, constData.data(), constData.size());
  252. device->context->Unmap(constants, 0);
  253. }
  254. }
  255. void gs_shader_destroy(gs_shader_t *shader)
  256. {
  257. if (shader && shader->device->lastVertexShader == shader)
  258. shader->device->lastVertexShader = nullptr;
  259. delete shader;
  260. }
  261. int gs_shader_get_num_params(const gs_shader_t *shader)
  262. {
  263. return (int)shader->params.size();
  264. }
  265. gs_sparam_t *gs_shader_get_param_by_idx(gs_shader_t *shader, uint32_t param)
  266. {
  267. return &shader->params[param];
  268. }
  269. gs_sparam_t *gs_shader_get_param_by_name(gs_shader_t *shader, const char *name)
  270. {
  271. for (size_t i = 0; i < shader->params.size(); i++) {
  272. gs_shader_param &param = shader->params[i];
  273. if (strcmp(param.name.c_str(), name) == 0)
  274. return &param;
  275. }
  276. return NULL;
  277. }
  278. gs_sparam_t *gs_shader_get_viewproj_matrix(const gs_shader_t *shader)
  279. {
  280. if (shader->type != GS_SHADER_VERTEX)
  281. return NULL;
  282. return static_cast<const gs_vertex_shader *>(shader)->viewProj;
  283. }
  284. gs_sparam_t *gs_shader_get_world_matrix(const gs_shader_t *shader)
  285. {
  286. if (shader->type != GS_SHADER_VERTEX)
  287. return NULL;
  288. return static_cast<const gs_vertex_shader *>(shader)->world;
  289. }
  290. void gs_shader_get_param_info(const gs_sparam_t *param,
  291. struct gs_shader_param_info *info)
  292. {
  293. if (!param)
  294. return;
  295. info->name = param->name.c_str();
  296. info->type = param->type;
  297. }
  298. static inline void shader_setval_inline(gs_shader_param *param,
  299. const void *data, size_t size)
  300. {
  301. assert(param);
  302. if (!param)
  303. return;
  304. bool size_changed = param->curValue.size() != size;
  305. if (size_changed)
  306. param->curValue.resize(size);
  307. if (size_changed || memcmp(param->curValue.data(), data, size) != 0) {
  308. memcpy(param->curValue.data(), data, size);
  309. param->changed = true;
  310. }
  311. }
  312. void gs_shader_set_bool(gs_sparam_t *param, bool val)
  313. {
  314. int b_val = (int)val;
  315. shader_setval_inline(param, &b_val, sizeof(int));
  316. }
  317. void gs_shader_set_float(gs_sparam_t *param, float val)
  318. {
  319. shader_setval_inline(param, &val, sizeof(float));
  320. }
  321. void gs_shader_set_int(gs_sparam_t *param, int val)
  322. {
  323. shader_setval_inline(param, &val, sizeof(int));
  324. }
  325. void gs_shader_set_matrix3(gs_sparam_t *param, const struct matrix3 *val)
  326. {
  327. struct matrix4 mat;
  328. matrix4_from_matrix3(&mat, val);
  329. shader_setval_inline(param, &mat, sizeof(matrix4));
  330. }
  331. void gs_shader_set_matrix4(gs_sparam_t *param, const struct matrix4 *val)
  332. {
  333. shader_setval_inline(param, val, sizeof(matrix4));
  334. }
  335. void gs_shader_set_vec2(gs_sparam_t *param, const struct vec2 *val)
  336. {
  337. shader_setval_inline(param, val, sizeof(vec2));
  338. }
  339. void gs_shader_set_vec3(gs_sparam_t *param, const struct vec3 *val)
  340. {
  341. shader_setval_inline(param, val, sizeof(float) * 3);
  342. }
  343. void gs_shader_set_vec4(gs_sparam_t *param, const struct vec4 *val)
  344. {
  345. shader_setval_inline(param, val, sizeof(vec4));
  346. }
  347. void gs_shader_set_texture(gs_sparam_t *param, gs_texture_t *val)
  348. {
  349. shader_setval_inline(param, &val, sizeof(gs_texture_t *));
  350. }
  351. void gs_shader_set_val(gs_sparam_t *param, const void *val, size_t size)
  352. {
  353. shader_setval_inline(param, val, size);
  354. }
  355. void gs_shader_set_default(gs_sparam_t *param)
  356. {
  357. if (param->defaultValue.size())
  358. shader_setval_inline(param, param->defaultValue.data(),
  359. param->defaultValue.size());
  360. }
  361. void gs_shader_set_next_sampler(gs_sparam_t *param, gs_samplerstate_t *sampler)
  362. {
  363. param->nextSampler = sampler;
  364. }