rendering_device_driver.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /**************************************************************************/
  2. /* rendering_device_driver.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rendering_device_driver.h"
  31. #include "thirdparty/spirv-reflect/spirv_reflect.h"
  32. /****************/
  33. /**** SHADER ****/
  34. /****************/
  35. Error RenderingDeviceDriver::_reflect_spirv(VectorView<ShaderStageSPIRVData> p_spirv, ShaderReflection &r_reflection) {
  36. r_reflection = {};
  37. for (uint32_t i = 0; i < p_spirv.size(); i++) {
  38. ShaderStage stage = p_spirv[i].shader_stage;
  39. ShaderStage stage_flag = (ShaderStage)(1 << p_spirv[i].shader_stage);
  40. if (p_spirv[i].shader_stage == SHADER_STAGE_COMPUTE) {
  41. r_reflection.is_compute = true;
  42. ERR_FAIL_COND_V_MSG(p_spirv.size() != 1, FAILED,
  43. "Compute shaders can only receive one stage, dedicated to compute.");
  44. }
  45. ERR_FAIL_COND_V_MSG(r_reflection.stages.has_flag(stage_flag), FAILED,
  46. "Stage " + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + " submitted more than once.");
  47. {
  48. SpvReflectShaderModule module;
  49. const uint8_t *spirv = p_spirv[i].spirv.ptr();
  50. SpvReflectResult result = spvReflectCreateShaderModule(p_spirv[i].spirv.size(), spirv, &module);
  51. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  52. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed parsing shader.");
  53. if (r_reflection.is_compute) {
  54. r_reflection.compute_local_size[0] = module.entry_points->local_size.x;
  55. r_reflection.compute_local_size[1] = module.entry_points->local_size.y;
  56. r_reflection.compute_local_size[2] = module.entry_points->local_size.z;
  57. }
  58. uint32_t binding_count = 0;
  59. result = spvReflectEnumerateDescriptorBindings(&module, &binding_count, nullptr);
  60. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  61. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating descriptor bindings.");
  62. if (binding_count > 0) {
  63. // Parse bindings.
  64. Vector<SpvReflectDescriptorBinding *> bindings;
  65. bindings.resize(binding_count);
  66. result = spvReflectEnumerateDescriptorBindings(&module, &binding_count, bindings.ptrw());
  67. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  68. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed getting descriptor bindings.");
  69. for (uint32_t j = 0; j < binding_count; j++) {
  70. const SpvReflectDescriptorBinding &binding = *bindings[j];
  71. ShaderUniform uniform;
  72. bool need_array_dimensions = false;
  73. bool need_block_size = false;
  74. bool may_be_writable = false;
  75. switch (binding.descriptor_type) {
  76. case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLER: {
  77. uniform.type = UNIFORM_TYPE_SAMPLER;
  78. need_array_dimensions = true;
  79. } break;
  80. case SPV_REFLECT_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
  81. uniform.type = UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
  82. need_array_dimensions = true;
  83. } break;
  84. case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLED_IMAGE: {
  85. uniform.type = UNIFORM_TYPE_TEXTURE;
  86. need_array_dimensions = true;
  87. } break;
  88. case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
  89. uniform.type = UNIFORM_TYPE_IMAGE;
  90. need_array_dimensions = true;
  91. may_be_writable = true;
  92. } break;
  93. case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: {
  94. uniform.type = UNIFORM_TYPE_TEXTURE_BUFFER;
  95. need_array_dimensions = true;
  96. } break;
  97. case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
  98. uniform.type = UNIFORM_TYPE_IMAGE_BUFFER;
  99. need_array_dimensions = true;
  100. may_be_writable = true;
  101. } break;
  102. case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER: {
  103. uniform.type = UNIFORM_TYPE_UNIFORM_BUFFER;
  104. need_block_size = true;
  105. } break;
  106. case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER: {
  107. uniform.type = UNIFORM_TYPE_STORAGE_BUFFER;
  108. need_block_size = true;
  109. may_be_writable = true;
  110. } break;
  111. case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: {
  112. ERR_PRINT("Dynamic uniform buffer not supported.");
  113. continue;
  114. } break;
  115. case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: {
  116. ERR_PRINT("Dynamic storage buffer not supported.");
  117. continue;
  118. } break;
  119. case SPV_REFLECT_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
  120. uniform.type = UNIFORM_TYPE_INPUT_ATTACHMENT;
  121. need_array_dimensions = true;
  122. } break;
  123. case SPV_REFLECT_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR: {
  124. ERR_PRINT("Acceleration structure not supported.");
  125. continue;
  126. } break;
  127. }
  128. if (need_array_dimensions) {
  129. if (binding.array.dims_count == 0) {
  130. uniform.length = 1;
  131. } else {
  132. for (uint32_t k = 0; k < binding.array.dims_count; k++) {
  133. if (k == 0) {
  134. uniform.length = binding.array.dims[0];
  135. } else {
  136. uniform.length *= binding.array.dims[k];
  137. }
  138. }
  139. }
  140. } else if (need_block_size) {
  141. uniform.length = binding.block.size;
  142. } else {
  143. uniform.length = 0;
  144. }
  145. if (may_be_writable) {
  146. uniform.writable = !(binding.type_description->decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE) && !(binding.block.decoration_flags & SPV_REFLECT_DECORATION_NON_WRITABLE);
  147. } else {
  148. uniform.writable = false;
  149. }
  150. uniform.binding = binding.binding;
  151. uint32_t set = binding.set;
  152. ERR_FAIL_COND_V_MSG(set >= MAX_UNIFORM_SETS, FAILED,
  153. "On shader stage '" + String(SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' uses a set (" + itos(set) + ") index larger than what is supported (" + itos(MAX_UNIFORM_SETS) + ").");
  154. if (set < (uint32_t)r_reflection.uniform_sets.size()) {
  155. // Check if this already exists.
  156. bool exists = false;
  157. for (int k = 0; k < r_reflection.uniform_sets[set].size(); k++) {
  158. if (r_reflection.uniform_sets[set][k].binding == uniform.binding) {
  159. // Already exists, verify that it's the same type.
  160. ERR_FAIL_COND_V_MSG(r_reflection.uniform_sets[set][k].type != uniform.type, FAILED,
  161. "On shader stage '" + String(SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' trying to reuse location for set=" + itos(set) + ", binding=" + itos(uniform.binding) + " with different uniform type.");
  162. // Also, verify that it's the same size.
  163. ERR_FAIL_COND_V_MSG(r_reflection.uniform_sets[set][k].length != uniform.length, FAILED,
  164. "On shader stage '" + String(SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' trying to reuse location for set=" + itos(set) + ", binding=" + itos(uniform.binding) + " with different uniform size.");
  165. // Also, verify that it has the same writability.
  166. ERR_FAIL_COND_V_MSG(r_reflection.uniform_sets[set][k].writable != uniform.writable, FAILED,
  167. "On shader stage '" + String(SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' trying to reuse location for set=" + itos(set) + ", binding=" + itos(uniform.binding) + " with different writability.");
  168. // Just append stage mask and return.
  169. r_reflection.uniform_sets.write[set].write[k].stages.set_flag(stage_flag);
  170. exists = true;
  171. break;
  172. }
  173. }
  174. if (exists) {
  175. continue; // Merged.
  176. }
  177. }
  178. uniform.stages.set_flag(stage_flag);
  179. if (set >= (uint32_t)r_reflection.uniform_sets.size()) {
  180. r_reflection.uniform_sets.resize(set + 1);
  181. }
  182. r_reflection.uniform_sets.write[set].push_back(uniform);
  183. }
  184. }
  185. {
  186. // Specialization constants.
  187. uint32_t sc_count = 0;
  188. result = spvReflectEnumerateSpecializationConstants(&module, &sc_count, nullptr);
  189. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  190. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating specialization constants.");
  191. if (sc_count) {
  192. Vector<SpvReflectSpecializationConstant *> spec_constants;
  193. spec_constants.resize(sc_count);
  194. result = spvReflectEnumerateSpecializationConstants(&module, &sc_count, spec_constants.ptrw());
  195. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  196. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed obtaining specialization constants.");
  197. for (uint32_t j = 0; j < sc_count; j++) {
  198. int32_t existing = -1;
  199. ShaderSpecializationConstant sconst;
  200. SpvReflectSpecializationConstant *spc = spec_constants[j];
  201. sconst.constant_id = spc->constant_id;
  202. sconst.int_value = 0; // Clear previous value JIC.
  203. switch (spc->constant_type) {
  204. case SPV_REFLECT_SPECIALIZATION_CONSTANT_BOOL: {
  205. sconst.type = PIPELINE_SPECIALIZATION_CONSTANT_TYPE_BOOL;
  206. sconst.bool_value = spc->default_value.int_bool_value != 0;
  207. } break;
  208. case SPV_REFLECT_SPECIALIZATION_CONSTANT_INT: {
  209. sconst.type = PIPELINE_SPECIALIZATION_CONSTANT_TYPE_INT;
  210. sconst.int_value = spc->default_value.int_bool_value;
  211. } break;
  212. case SPV_REFLECT_SPECIALIZATION_CONSTANT_FLOAT: {
  213. sconst.type = PIPELINE_SPECIALIZATION_CONSTANT_TYPE_FLOAT;
  214. sconst.float_value = spc->default_value.float_value;
  215. } break;
  216. }
  217. sconst.stages.set_flag(stage_flag);
  218. for (int k = 0; k < r_reflection.specialization_constants.size(); k++) {
  219. if (r_reflection.specialization_constants[k].constant_id == sconst.constant_id) {
  220. ERR_FAIL_COND_V_MSG(r_reflection.specialization_constants[k].type != sconst.type, FAILED, "More than one specialization constant used for id (" + itos(sconst.constant_id) + "), but their types differ.");
  221. ERR_FAIL_COND_V_MSG(r_reflection.specialization_constants[k].int_value != sconst.int_value, FAILED, "More than one specialization constant used for id (" + itos(sconst.constant_id) + "), but their default values differ.");
  222. existing = k;
  223. break;
  224. }
  225. }
  226. if (existing > 0) {
  227. r_reflection.specialization_constants.write[existing].stages.set_flag(stage_flag);
  228. } else {
  229. r_reflection.specialization_constants.push_back(sconst);
  230. }
  231. }
  232. r_reflection.specialization_constants.sort();
  233. }
  234. }
  235. if (stage == SHADER_STAGE_VERTEX) {
  236. uint32_t iv_count = 0;
  237. result = spvReflectEnumerateInputVariables(&module, &iv_count, nullptr);
  238. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  239. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating input variables.");
  240. if (iv_count) {
  241. Vector<SpvReflectInterfaceVariable *> input_vars;
  242. input_vars.resize(iv_count);
  243. result = spvReflectEnumerateInputVariables(&module, &iv_count, input_vars.ptrw());
  244. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  245. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed obtaining input variables.");
  246. for (uint32_t j = 0; j < iv_count; j++) {
  247. if (input_vars[j] && input_vars[j]->decoration_flags == 0) { // Regular input.
  248. r_reflection.vertex_input_mask |= (((uint64_t)1) << input_vars[j]->location);
  249. }
  250. }
  251. }
  252. }
  253. if (stage == SHADER_STAGE_FRAGMENT) {
  254. uint32_t ov_count = 0;
  255. result = spvReflectEnumerateOutputVariables(&module, &ov_count, nullptr);
  256. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  257. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating output variables.");
  258. if (ov_count) {
  259. Vector<SpvReflectInterfaceVariable *> output_vars;
  260. output_vars.resize(ov_count);
  261. result = spvReflectEnumerateOutputVariables(&module, &ov_count, output_vars.ptrw());
  262. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  263. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed obtaining output variables.");
  264. for (uint32_t j = 0; j < ov_count; j++) {
  265. const SpvReflectInterfaceVariable *refvar = output_vars[j];
  266. if (refvar != nullptr && refvar->built_in != SpvBuiltInFragDepth) {
  267. r_reflection.fragment_output_mask |= 1 << refvar->location;
  268. }
  269. }
  270. }
  271. }
  272. uint32_t pc_count = 0;
  273. result = spvReflectEnumeratePushConstantBlocks(&module, &pc_count, nullptr);
  274. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  275. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed enumerating push constants.");
  276. if (pc_count) {
  277. ERR_FAIL_COND_V_MSG(pc_count > 1, FAILED,
  278. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "': Only one push constant is supported, which should be the same across shader stages.");
  279. Vector<SpvReflectBlockVariable *> pconstants;
  280. pconstants.resize(pc_count);
  281. result = spvReflectEnumeratePushConstantBlocks(&module, &pc_count, pconstants.ptrw());
  282. ERR_FAIL_COND_V_MSG(result != SPV_REFLECT_RESULT_SUCCESS, FAILED,
  283. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "' failed obtaining push constants.");
  284. #if 0
  285. if (pconstants[0] == nullptr) {
  286. Ref<FileAccess> f = FileAccess::open("res://popo.spv", FileAccess::WRITE);
  287. f->store_buffer((const uint8_t *)&SpirV[0], SpirV.size() * sizeof(uint32_t));
  288. }
  289. #endif
  290. ERR_FAIL_COND_V_MSG(r_reflection.push_constant_size && r_reflection.push_constant_size != pconstants[0]->size, FAILED,
  291. "Reflection of SPIR-V shader stage '" + String(SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + "': Push constant block must be the same across shader stages.");
  292. r_reflection.push_constant_size = pconstants[0]->size;
  293. r_reflection.push_constant_stages.set_flag(stage_flag);
  294. //print_line("Stage: " + String(SHADER_STAGE_NAMES[stage]) + " push constant of size=" + itos(push_constant.push_constant_size));
  295. }
  296. // Destroy the reflection data when no longer required.
  297. spvReflectDestroyShaderModule(&module);
  298. }
  299. r_reflection.stages.set_flag(stage_flag);
  300. }
  301. return OK;
  302. }
  303. /**************/
  304. /**** MISC ****/
  305. /**************/
  306. uint64_t RenderingDeviceDriver::api_trait_get(ApiTrait p_trait) {
  307. // Sensible canonical defaults.
  308. switch (p_trait) {
  309. case API_TRAIT_HONORS_PIPELINE_BARRIERS:
  310. return 1;
  311. case API_TRAIT_SHADER_CHANGE_INVALIDATION:
  312. return SHADER_CHANGE_INVALIDATION_ALL_BOUND_UNIFORM_SETS;
  313. case API_TRAIT_TEXTURE_TRANSFER_ALIGNMENT:
  314. return 1;
  315. case API_TRAIT_TEXTURE_DATA_ROW_PITCH_STEP:
  316. return 1;
  317. case API_TRAIT_SECONDARY_VIEWPORT_SCISSOR:
  318. return 1;
  319. case API_TRAIT_CLEARS_WITH_COPY_ENGINE:
  320. return true;
  321. case API_TRAIT_USE_GENERAL_IN_COPY_QUEUES:
  322. return false;
  323. case API_TRAIT_BUFFERS_REQUIRE_TRANSITIONS:
  324. return false;
  325. default:
  326. ERR_FAIL_V(0);
  327. }
  328. }
  329. /******************/
  330. RenderingDeviceDriver::~RenderingDeviceDriver() {}