rendering_shader_container_metal.mm 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /**************************************************************************/
  2. /* rendering_shader_container_metal.mm */
  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_shader_container_metal.h"
  31. #include "servers/rendering/rendering_device.h"
  32. #import "core/io/marshalls.h"
  33. #import <Metal/Metal.h>
  34. #import <spirv.hpp>
  35. #import <spirv_msl.hpp>
  36. #import <spirv_parser.hpp>
  37. Mutex MetalDeviceProfile::profiles_lock;
  38. HashMap<uint32_t, MetalDeviceProfile> MetalDeviceProfile::profiles;
  39. const MetalDeviceProfile *MetalDeviceProfile::get_profile(MetalDeviceProfile::Platform p_platform, MetalDeviceProfile::GPU p_gpu) {
  40. DEV_ASSERT(p_platform == Platform::macOS || p_platform == Platform::iOS);
  41. MutexLock lock(profiles_lock);
  42. uint32_t key = (uint32_t)p_platform << 16 | (uint32_t)p_gpu;
  43. if (MetalDeviceProfile *profile = profiles.getptr(key)) {
  44. return profile;
  45. }
  46. MetalDeviceProfile res;
  47. res.platform = p_platform;
  48. res.gpu = p_gpu;
  49. if (p_platform == Platform::macOS) {
  50. res.features.mslVersionMajor = 3;
  51. res.features.mslVersionMinor = 2;
  52. res.features.argument_buffers_tier = ArgumentBuffersTier::Tier2;
  53. res.features.simdPermute = true;
  54. } else if (p_platform == Platform::iOS) {
  55. switch (p_gpu) {
  56. case GPU::Apple1:
  57. case GPU::Apple2:
  58. case GPU::Apple3:
  59. case GPU::Apple4:
  60. case GPU::Apple5: {
  61. res.features.simdPermute = false;
  62. res.features.argument_buffers_tier = ArgumentBuffersTier::Tier1;
  63. } break;
  64. case GPU::Apple6:
  65. case GPU::Apple7:
  66. case GPU::Apple8:
  67. case GPU::Apple9: {
  68. res.features.argument_buffers_tier = ArgumentBuffersTier::Tier2;
  69. res.features.simdPermute = true;
  70. } break;
  71. }
  72. res.features.mslVersionMajor = 3;
  73. res.features.mslVersionMinor = 2;
  74. }
  75. return &profiles.insert(key, res)->value;
  76. }
  77. Error RenderingShaderContainerMetal::compile_metal_source(const char *p_source, const StageData &p_stage_data, Vector<uint8_t> &r_binary_data) {
  78. String name(shader_name.ptr());
  79. if (name.contains_char(':')) {
  80. name = name.replace_char(':', '_');
  81. }
  82. Error r_error;
  83. Ref<FileAccess> source_file = FileAccess::create_temp(FileAccess::ModeFlags::READ_WRITE,
  84. name + "_" + itos(p_stage_data.hash.short_sha()),
  85. "metal", false, &r_error);
  86. ERR_FAIL_COND_V_MSG(r_error != OK, r_error, "Unable to create temporary source file.");
  87. if (!source_file->store_buffer((const uint8_t *)p_source, strlen(p_source))) {
  88. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unable to write temporary source file");
  89. }
  90. source_file->flush();
  91. Ref<FileAccess> result_file = FileAccess::create_temp(FileAccess::ModeFlags::READ_WRITE,
  92. name + "_" + itos(p_stage_data.hash.short_sha()),
  93. "metallib", false, &r_error);
  94. ERR_FAIL_COND_V_MSG(r_error != OK, r_error, "Unable to create temporary target file");
  95. String sdk;
  96. switch (device_profile->platform) {
  97. case MetalDeviceProfile::Platform::macOS:
  98. sdk = "macosx";
  99. break;
  100. case MetalDeviceProfile::Platform::iOS:
  101. sdk = "iphoneos";
  102. break;
  103. }
  104. // Build the metallib binary.
  105. {
  106. List<String> args{ "-sdk", sdk, "metal", "-O3" };
  107. if (p_stage_data.is_position_invariant) {
  108. args.push_back("-fpreserve-invariance");
  109. }
  110. args.push_back("-fmetal-math-mode=fast");
  111. args.push_back(source_file->get_path_absolute());
  112. args.push_back("-o");
  113. args.push_back(result_file->get_path_absolute());
  114. String r_pipe;
  115. int exit_code;
  116. Error err = OS::get_singleton()->execute("/usr/bin/xcrun", args, &r_pipe, &exit_code, true);
  117. if (!r_pipe.is_empty()) {
  118. print_line(r_pipe);
  119. }
  120. if (err != OK) {
  121. ERR_PRINT(vformat("Metal compiler returned error code: %d", err));
  122. }
  123. if (exit_code != 0) {
  124. ERR_PRINT(vformat("Metal compiler exited with error code: %d", exit_code));
  125. }
  126. int len = result_file->get_length();
  127. ERR_FAIL_COND_V_MSG(len == 0, ERR_CANT_CREATE, "Metal compiler created empty library");
  128. }
  129. // Strip the source from the binary.
  130. {
  131. List<String> args{ "-sdk", sdk, "metal-dsymutil", "--remove-source", result_file->get_path_absolute() };
  132. String r_pipe;
  133. int exit_code;
  134. Error err = OS::get_singleton()->execute("/usr/bin/xcrun", args, &r_pipe, &exit_code, true);
  135. if (!r_pipe.is_empty()) {
  136. print_line(r_pipe);
  137. }
  138. if (err != OK) {
  139. ERR_PRINT(vformat("metal-dsymutil tool returned error code: %d", err));
  140. }
  141. if (exit_code != 0) {
  142. ERR_PRINT(vformat("metal-dsymutil Compiler exited with error code: %d", exit_code));
  143. }
  144. int len = result_file->get_length();
  145. ERR_FAIL_COND_V_MSG(len == 0, ERR_CANT_CREATE, "metal-dsymutil tool created empty library");
  146. }
  147. r_binary_data = result_file->get_buffer(result_file->get_length());
  148. return OK;
  149. }
  150. #pragma clang diagnostic push
  151. #pragma clang diagnostic ignored "-Wunguarded-availability"
  152. bool RenderingShaderContainerMetal::_set_code_from_spirv(const Vector<RenderingDeviceCommons::ShaderStageSPIRVData> &p_spirv) {
  153. using namespace spirv_cross;
  154. using spirv_cross::CompilerMSL;
  155. using spirv_cross::Resource;
  156. // initialize Metal-specific reflection data
  157. shaders.resize(p_spirv.size());
  158. mtl_shaders.resize(p_spirv.size());
  159. mtl_reflection_binding_set_uniforms_data.resize(reflection_binding_set_uniforms_data.size());
  160. mtl_reflection_specialization_data.resize(reflection_specialization_data.size());
  161. mtl_reflection_data.set_needs_view_mask_buffer(reflection_data.has_multiview);
  162. // set_indexes will contain the starting offsets of each descriptor set in the binding set uniforms data
  163. // including the last one, which is the size of reflection_binding_set_uniforms_count.
  164. LocalVector<uint32_t> set_indexes;
  165. uint32_t set_indexes_size = reflection_binding_set_uniforms_count.size() + 1;
  166. {
  167. // calculate the starting offsets of each descriptor set in the binding set uniforms data
  168. uint32_t size = reflection_binding_set_uniforms_count.size();
  169. set_indexes.resize(set_indexes_size);
  170. uint32_t offset = 0;
  171. for (uint32_t i = 0; i < size; i++) {
  172. set_indexes[i] = offset;
  173. offset += reflection_binding_set_uniforms_count.get(i);
  174. }
  175. set_indexes[set_indexes_size - 1] = offset;
  176. }
  177. CompilerMSL::Options msl_options{};
  178. // MAJOR * 10000 + MINOR * 100
  179. uint32_t msl_version = CompilerMSL::Options::make_msl_version(device_profile->features.mslVersionMajor, device_profile->features.mslVersionMinor);
  180. msl_options.set_msl_version(device_profile->features.mslVersionMajor, device_profile->features.mslVersionMinor);
  181. mtl_reflection_data.msl_version = msl_options.msl_version;
  182. msl_options.platform = device_profile->platform == MetalDeviceProfile::Platform::macOS ? CompilerMSL::Options::macOS : CompilerMSL::Options::iOS;
  183. if (device_profile->platform == MetalDeviceProfile::Platform::iOS) {
  184. msl_options.ios_use_simdgroup_functions = device_profile->features.simdPermute;
  185. msl_options.ios_support_base_vertex_instance = true;
  186. }
  187. bool disable_argument_buffers = false;
  188. if (String v = OS::get_singleton()->get_environment("GODOT_MTL_DISABLE_ARGUMENT_BUFFERS"); v == "1") {
  189. disable_argument_buffers = true;
  190. }
  191. if (device_profile->features.argument_buffers_tier >= MetalDeviceProfile::ArgumentBuffersTier::Tier2 && !disable_argument_buffers) {
  192. msl_options.argument_buffers_tier = CompilerMSL::Options::ArgumentBuffersTier::Tier2;
  193. msl_options.argument_buffers = true;
  194. mtl_reflection_data.set_uses_argument_buffers(true);
  195. } else {
  196. msl_options.argument_buffers_tier = CompilerMSL::Options::ArgumentBuffersTier::Tier1;
  197. // Tier 1 argument buffers don't support writable textures, so we disable them completely.
  198. msl_options.argument_buffers = false;
  199. mtl_reflection_data.set_uses_argument_buffers(false);
  200. }
  201. msl_options.force_active_argument_buffer_resources = true;
  202. // We can't use this, as we have to add the descriptor sets via compiler.add_msl_resource_binding.
  203. // msl_options.pad_argument_buffer_resources = true;
  204. msl_options.texture_buffer_native = true; // Enable texture buffer support.
  205. msl_options.use_framebuffer_fetch_subpasses = false;
  206. msl_options.pad_fragment_output_components = true;
  207. msl_options.r32ui_alignment_constant_id = R32UI_ALIGNMENT_CONSTANT_ID;
  208. msl_options.agx_manual_cube_grad_fixup = true;
  209. if (reflection_data.has_multiview) {
  210. msl_options.multiview = true;
  211. msl_options.multiview_layered_rendering = true;
  212. msl_options.view_mask_buffer_index = VIEW_MASK_BUFFER_INDEX;
  213. }
  214. if (msl_version >= CompilerMSL::Options::make_msl_version(3, 2)) {
  215. // All 3.2+ versions support device coherence, so we can disable texture fences.
  216. msl_options.readwrite_texture_fences = false;
  217. }
  218. CompilerGLSL::Options options{};
  219. options.vertex.flip_vert_y = true;
  220. #if DEV_ENABLED
  221. options.emit_line_directives = true;
  222. #endif
  223. for (uint32_t i = 0; i < p_spirv.size(); i++) {
  224. StageData &stage_data = mtl_shaders.write[i];
  225. RD::ShaderStageSPIRVData const &v = p_spirv[i];
  226. RD::ShaderStage stage = v.shader_stage;
  227. char const *stage_name = RD::SHADER_STAGE_NAMES[stage];
  228. uint32_t const *const ir = reinterpret_cast<uint32_t const *const>(v.spirv.ptr());
  229. size_t word_count = v.spirv.size() / sizeof(uint32_t);
  230. Parser parser(ir, word_count);
  231. try {
  232. parser.parse();
  233. } catch (CompilerError &e) {
  234. ERR_FAIL_V_MSG(false, "Failed to parse IR at stage " + String(RD::SHADER_STAGE_NAMES[stage]) + ": " + e.what());
  235. }
  236. CompilerMSL compiler(std::move(parser.get_parsed_ir()));
  237. compiler.set_msl_options(msl_options);
  238. compiler.set_common_options(options);
  239. std::unordered_set<VariableID> active = compiler.get_active_interface_variables();
  240. ShaderResources resources = compiler.get_shader_resources();
  241. std::string source;
  242. try {
  243. source = compiler.compile();
  244. } catch (CompilerError &e) {
  245. ERR_FAIL_V_MSG(false, "Failed to compile stage " + String(RD::SHADER_STAGE_NAMES[stage]) + ": " + e.what());
  246. }
  247. ERR_FAIL_COND_V_MSG(compiler.get_entry_points_and_stages().size() != 1, false, "Expected a single entry point and stage.");
  248. SmallVector<EntryPoint> entry_pts_stages = compiler.get_entry_points_and_stages();
  249. EntryPoint &entry_point_stage = entry_pts_stages.front();
  250. SPIREntryPoint &entry_point = compiler.get_entry_point(entry_point_stage.name, entry_point_stage.execution_model);
  251. // Process specialization constants.
  252. if (!compiler.get_specialization_constants().empty()) {
  253. uint32_t size = reflection_specialization_data.size();
  254. for (SpecializationConstant const &constant : compiler.get_specialization_constants()) {
  255. uint32_t j = 0;
  256. while (j < size) {
  257. const ReflectionSpecializationData &res = reflection_specialization_data.ptr()[j];
  258. if (res.constant_id == constant.constant_id) {
  259. mtl_reflection_specialization_data.ptrw()[j].used_stages |= 1 << stage;
  260. // emulate labeled for loop and continue
  261. goto outer_continue;
  262. }
  263. ++j;
  264. }
  265. if (j == size) {
  266. WARN_PRINT(String(stage_name) + ": unable to find constant_id: " + itos(constant.constant_id));
  267. }
  268. outer_continue:;
  269. }
  270. }
  271. // Process bindings.
  272. uint32_t uniform_sets_size = reflection_binding_set_uniforms_count.size();
  273. using BT = SPIRType::BaseType;
  274. // Always clearer than a boolean.
  275. enum class Writable {
  276. No,
  277. Maybe,
  278. };
  279. // Returns a std::optional containing the value of the
  280. // decoration, if it exists.
  281. auto get_decoration = [&compiler](spirv_cross::ID id, spv::Decoration decoration) {
  282. uint32_t res = -1;
  283. if (compiler.has_decoration(id, decoration)) {
  284. res = compiler.get_decoration(id, decoration);
  285. }
  286. return res;
  287. };
  288. auto descriptor_bindings = [&compiler, &active, this, &set_indexes, uniform_sets_size, stage, &get_decoration](SmallVector<Resource> &p_resources, Writable p_writable) {
  289. for (Resource const &res : p_resources) {
  290. uint32_t dset = get_decoration(res.id, spv::DecorationDescriptorSet);
  291. uint32_t dbin = get_decoration(res.id, spv::DecorationBinding);
  292. UniformData *found = nullptr;
  293. if (dset != (uint32_t)-1 && dbin != (uint32_t)-1 && dset < uniform_sets_size) {
  294. uint32_t begin = set_indexes[dset];
  295. uint32_t end = set_indexes[dset + 1];
  296. for (uint32_t j = begin; j < end; j++) {
  297. const ReflectionBindingData &ref_bind = reflection_binding_set_uniforms_data[j];
  298. if (dbin == ref_bind.binding) {
  299. found = &mtl_reflection_binding_set_uniforms_data.write[j];
  300. break;
  301. }
  302. }
  303. }
  304. ERR_FAIL_NULL_V_MSG(found, ERR_CANT_CREATE, "UniformData not found");
  305. bool is_active = active.find(res.id) != active.end();
  306. if (is_active) {
  307. found->active_stages |= 1 << stage;
  308. }
  309. BindingInfoData &primary = found->get_binding_for_stage(stage);
  310. SPIRType const &a_type = compiler.get_type(res.type_id);
  311. BT basetype = a_type.basetype;
  312. switch (basetype) {
  313. case BT::Struct: {
  314. primary.data_type = MTLDataTypePointer;
  315. } break;
  316. case BT::Image:
  317. case BT::SampledImage: {
  318. primary.data_type = MTLDataTypeTexture;
  319. } break;
  320. case BT::Sampler: {
  321. primary.data_type = MTLDataTypeSampler;
  322. primary.array_length = 1;
  323. for (uint32_t const &a : a_type.array) {
  324. primary.array_length *= a;
  325. }
  326. } break;
  327. default: {
  328. ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unexpected BaseType");
  329. } break;
  330. }
  331. // Find array length of image.
  332. if (basetype == BT::Image || basetype == BT::SampledImage) {
  333. primary.array_length = 1;
  334. for (uint32_t const &a : a_type.array) {
  335. primary.array_length *= a;
  336. }
  337. primary.is_multisampled = a_type.image.ms;
  338. SPIRType::ImageType const &image = a_type.image;
  339. primary.image_format = image.format;
  340. switch (image.dim) {
  341. case spv::Dim1D: {
  342. if (image.arrayed) {
  343. primary.texture_type = MTLTextureType1DArray;
  344. } else {
  345. primary.texture_type = MTLTextureType1D;
  346. }
  347. } break;
  348. case spv::DimSubpassData: {
  349. [[fallthrough]];
  350. }
  351. case spv::Dim2D: {
  352. if (image.arrayed && image.ms) {
  353. primary.texture_type = MTLTextureType2DMultisampleArray;
  354. } else if (image.arrayed) {
  355. primary.texture_type = MTLTextureType2DArray;
  356. } else if (image.ms) {
  357. primary.texture_type = MTLTextureType2DMultisample;
  358. } else {
  359. primary.texture_type = MTLTextureType2D;
  360. }
  361. } break;
  362. case spv::Dim3D: {
  363. primary.texture_type = MTLTextureType3D;
  364. } break;
  365. case spv::DimCube: {
  366. if (image.arrayed) {
  367. primary.texture_type = MTLTextureTypeCube;
  368. }
  369. } break;
  370. case spv::DimRect: {
  371. } break;
  372. case spv::DimBuffer: {
  373. // VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER
  374. primary.texture_type = MTLTextureTypeTextureBuffer;
  375. } break;
  376. case spv::DimTileImageDataEXT: {
  377. // Godot does not use this extension.
  378. // See: https://registry.khronos.org/vulkan/specs/latest/man/html/VK_EXT_shader_tile_image.html
  379. } break;
  380. case spv::DimMax: {
  381. // Add all enumerations to silence the compiler warning
  382. // and generate future warnings, should a new one be added.
  383. } break;
  384. }
  385. }
  386. // Update writable.
  387. if (p_writable == Writable::Maybe) {
  388. if (basetype == BT::Struct) {
  389. Bitset flags = compiler.get_buffer_block_flags(res.id);
  390. if (!flags.get(spv::DecorationNonWritable)) {
  391. if (flags.get(spv::DecorationNonReadable)) {
  392. primary.access = MTLBindingAccessWriteOnly;
  393. } else {
  394. primary.access = MTLBindingAccessReadWrite;
  395. }
  396. }
  397. } else if (basetype == BT::Image) {
  398. switch (a_type.image.access) {
  399. case spv::AccessQualifierWriteOnly:
  400. primary.access = MTLBindingAccessWriteOnly;
  401. break;
  402. case spv::AccessQualifierReadWrite:
  403. primary.access = MTLBindingAccessReadWrite;
  404. break;
  405. case spv::AccessQualifierReadOnly:
  406. break;
  407. case spv::AccessQualifierMax:
  408. [[fallthrough]];
  409. default:
  410. if (!compiler.has_decoration(res.id, spv::DecorationNonWritable)) {
  411. if (compiler.has_decoration(res.id, spv::DecorationNonReadable)) {
  412. primary.access = MTLBindingAccessWriteOnly;
  413. } else {
  414. primary.access = MTLBindingAccessReadWrite;
  415. }
  416. }
  417. break;
  418. }
  419. }
  420. }
  421. switch (primary.access) {
  422. case MTLBindingAccessReadOnly:
  423. primary.usage = MTLResourceUsageRead;
  424. break;
  425. case MTLBindingAccessWriteOnly:
  426. primary.usage = MTLResourceUsageWrite;
  427. break;
  428. case MTLBindingAccessReadWrite:
  429. primary.usage = MTLResourceUsageRead | MTLResourceUsageWrite;
  430. break;
  431. }
  432. primary.index = compiler.get_automatic_msl_resource_binding(res.id);
  433. // A sampled image contains two bindings, the primary
  434. // is to the image, and the secondary is to the associated sampler.
  435. if (basetype == BT::SampledImage) {
  436. uint32_t binding = compiler.get_automatic_msl_resource_binding_secondary(res.id);
  437. if (binding != (uint32_t)-1) {
  438. BindingInfoData &secondary = found->get_secondary_binding_for_stage(stage);
  439. secondary.data_type = MTLDataTypeSampler;
  440. secondary.index = binding;
  441. secondary.access = MTLBindingAccessReadOnly;
  442. }
  443. }
  444. // An image may have a secondary binding if it is used
  445. // for atomic operations.
  446. if (basetype == BT::Image) {
  447. uint32_t binding = compiler.get_automatic_msl_resource_binding_secondary(res.id);
  448. if (binding != (uint32_t)-1) {
  449. BindingInfoData &secondary = found->get_secondary_binding_for_stage(stage);
  450. secondary.data_type = MTLDataTypePointer;
  451. secondary.index = binding;
  452. secondary.access = MTLBindingAccessReadWrite;
  453. }
  454. }
  455. }
  456. return Error::OK;
  457. };
  458. if (!resources.uniform_buffers.empty()) {
  459. Error err = descriptor_bindings(resources.uniform_buffers, Writable::No);
  460. ERR_FAIL_COND_V(err != OK, false);
  461. }
  462. if (!resources.storage_buffers.empty()) {
  463. Error err = descriptor_bindings(resources.storage_buffers, Writable::Maybe);
  464. ERR_FAIL_COND_V(err != OK, false);
  465. }
  466. if (!resources.storage_images.empty()) {
  467. Error err = descriptor_bindings(resources.storage_images, Writable::Maybe);
  468. ERR_FAIL_COND_V(err != OK, false);
  469. }
  470. if (!resources.sampled_images.empty()) {
  471. Error err = descriptor_bindings(resources.sampled_images, Writable::No);
  472. ERR_FAIL_COND_V(err != OK, false);
  473. }
  474. if (!resources.separate_images.empty()) {
  475. Error err = descriptor_bindings(resources.separate_images, Writable::No);
  476. ERR_FAIL_COND_V(err != OK, false);
  477. }
  478. if (!resources.separate_samplers.empty()) {
  479. Error err = descriptor_bindings(resources.separate_samplers, Writable::No);
  480. ERR_FAIL_COND_V(err != OK, false);
  481. }
  482. if (!resources.subpass_inputs.empty()) {
  483. Error err = descriptor_bindings(resources.subpass_inputs, Writable::No);
  484. ERR_FAIL_COND_V(err != OK, false);
  485. }
  486. if (!resources.push_constant_buffers.empty()) {
  487. for (Resource const &res : resources.push_constant_buffers) {
  488. uint32_t binding = compiler.get_automatic_msl_resource_binding(res.id);
  489. if (binding != (uint32_t)-1) {
  490. stage_data.push_constant_binding = binding;
  491. }
  492. }
  493. }
  494. ERR_FAIL_COND_V_MSG(!resources.atomic_counters.empty(), false, "Atomic counters not supported");
  495. ERR_FAIL_COND_V_MSG(!resources.acceleration_structures.empty(), false, "Acceleration structures not supported");
  496. ERR_FAIL_COND_V_MSG(!resources.shader_record_buffers.empty(), false, "Shader record buffers not supported");
  497. if (!resources.stage_inputs.empty()) {
  498. for (Resource const &res : resources.stage_inputs) {
  499. uint32_t binding = compiler.get_automatic_msl_resource_binding(res.id);
  500. if (binding != (uint32_t)-1) {
  501. stage_data.vertex_input_binding_mask |= 1 << binding;
  502. }
  503. }
  504. }
  505. stage_data.is_position_invariant = compiler.is_position_invariant();
  506. stage_data.supports_fast_math = !entry_point.flags.get(spv::ExecutionModeSignedZeroInfNanPreserve);
  507. stage_data.hash = SHA256Digest(source.c_str(), source.length());
  508. stage_data.source_size = source.length();
  509. ::Vector<uint8_t> binary_data;
  510. binary_data.resize(stage_data.source_size);
  511. memcpy(binary_data.ptrw(), source.c_str(), stage_data.source_size);
  512. if (export_mode) {
  513. // Try to compile the Metal source code
  514. ::Vector<uint8_t> library_data;
  515. Error compile_err = compile_metal_source(source.c_str(), stage_data, library_data);
  516. if (compile_err == OK) {
  517. stage_data.library_size = library_data.size();
  518. binary_data.resize(stage_data.source_size + stage_data.library_size);
  519. memcpy(binary_data.ptrw() + stage_data.source_size, library_data.ptr(), stage_data.library_size);
  520. }
  521. }
  522. uint32_t binary_data_size = binary_data.size();
  523. Shader &shader = shaders.write[i];
  524. shader.shader_stage = stage;
  525. shader.code_decompressed_size = binary_data_size;
  526. shader.code_compressed_bytes.resize(binary_data_size);
  527. uint32_t compressed_size = 0;
  528. bool compressed = compress_code(binary_data.ptr(), binary_data_size, shader.code_compressed_bytes.ptrw(), &compressed_size, &shader.code_compression_flags);
  529. ERR_FAIL_COND_V_MSG(!compressed, false, vformat("Failed to compress native code to native for SPIR-V #%d.", i));
  530. shader.code_compressed_bytes.resize(compressed_size);
  531. }
  532. return true;
  533. }
  534. #pragma clang diagnostic pop
  535. uint32_t RenderingShaderContainerMetal::_to_bytes_reflection_extra_data(uint8_t *p_bytes) const {
  536. if (p_bytes != nullptr) {
  537. *(HeaderData *)p_bytes = mtl_reflection_data;
  538. }
  539. return sizeof(HeaderData);
  540. }
  541. uint32_t RenderingShaderContainerMetal::_to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const {
  542. if (p_bytes != nullptr) {
  543. *(UniformData *)p_bytes = mtl_reflection_binding_set_uniforms_data[p_index];
  544. }
  545. return sizeof(UniformData);
  546. }
  547. uint32_t RenderingShaderContainerMetal::_to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const {
  548. if (p_bytes != nullptr) {
  549. *(SpecializationData *)p_bytes = mtl_reflection_specialization_data[p_index];
  550. }
  551. return sizeof(SpecializationData);
  552. }
  553. uint32_t RenderingShaderContainerMetal::_to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const {
  554. if (p_bytes != nullptr) {
  555. *(StageData *)p_bytes = mtl_shaders[p_index];
  556. }
  557. return sizeof(StageData);
  558. }
  559. uint32_t RenderingShaderContainerMetal::_from_bytes_reflection_extra_data(const uint8_t *p_bytes) {
  560. mtl_reflection_data = *(HeaderData *)p_bytes;
  561. return sizeof(HeaderData);
  562. }
  563. uint32_t RenderingShaderContainerMetal::_from_bytes_reflection_binding_uniform_extra_data_start(const uint8_t *p_bytes) {
  564. mtl_reflection_binding_set_uniforms_data.resize(reflection_binding_set_uniforms_data.size());
  565. return 0;
  566. }
  567. uint32_t RenderingShaderContainerMetal::_from_bytes_reflection_binding_uniform_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
  568. mtl_reflection_binding_set_uniforms_data.ptrw()[p_index] = *(UniformData *)p_bytes;
  569. return sizeof(UniformData);
  570. }
  571. uint32_t RenderingShaderContainerMetal::_from_bytes_reflection_specialization_extra_data_start(const uint8_t *p_bytes) {
  572. mtl_reflection_specialization_data.resize(reflection_specialization_data.size());
  573. return 0;
  574. }
  575. uint32_t RenderingShaderContainerMetal::_from_bytes_reflection_specialization_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
  576. mtl_reflection_specialization_data.ptrw()[p_index] = *(SpecializationData *)p_bytes;
  577. return sizeof(SpecializationData);
  578. }
  579. uint32_t RenderingShaderContainerMetal::_from_bytes_shader_extra_data_start(const uint8_t *p_bytes) {
  580. mtl_shaders.resize(shaders.size());
  581. return 0;
  582. }
  583. uint32_t RenderingShaderContainerMetal::_from_bytes_shader_extra_data(const uint8_t *p_bytes, uint32_t p_index) {
  584. mtl_shaders.ptrw()[p_index] = *(StageData *)p_bytes;
  585. return sizeof(StageData);
  586. }
  587. RenderingShaderContainerMetal::MetalShaderReflection RenderingShaderContainerMetal::get_metal_shader_reflection() const {
  588. MetalShaderReflection res;
  589. res.specialization_constants = mtl_reflection_specialization_data;
  590. uint32_t uniform_set_count = reflection_binding_set_uniforms_count.size();
  591. uint32_t start = 0;
  592. res.uniform_sets.resize(uniform_set_count);
  593. for (uint32_t i = 0; i < uniform_set_count; i++) {
  594. Vector<UniformData> &set = res.uniform_sets.ptrw()[i];
  595. uint32_t count = reflection_binding_set_uniforms_count.get(i);
  596. set.resize(count);
  597. memcpy(set.ptrw(), &mtl_reflection_binding_set_uniforms_data.ptr()[start], count * sizeof(UniformData));
  598. start += count;
  599. }
  600. return res;
  601. }
  602. uint32_t RenderingShaderContainerMetal::_format() const {
  603. return 0x42424242;
  604. }
  605. uint32_t RenderingShaderContainerMetal::_format_version() const {
  606. return FORMAT_VERSION;
  607. }
  608. Ref<RenderingShaderContainer> RenderingShaderContainerFormatMetal::create_container() const {
  609. Ref<RenderingShaderContainerMetal> result;
  610. result.instantiate();
  611. result->set_export_mode(export_mode);
  612. result->set_device_profile(device_profile);
  613. return result;
  614. }
  615. RenderingDeviceCommons::ShaderLanguageVersion RenderingShaderContainerFormatMetal::get_shader_language_version() const {
  616. return SHADER_LANGUAGE_VULKAN_VERSION_1_1;
  617. }
  618. RenderingDeviceCommons::ShaderSpirvVersion RenderingShaderContainerFormatMetal::get_shader_spirv_version() const {
  619. return SHADER_SPIRV_VERSION_1_6;
  620. }
  621. RenderingShaderContainerFormatMetal::RenderingShaderContainerFormatMetal(const MetalDeviceProfile *p_device_profile, bool p_export) :
  622. export_mode(p_export), device_profile(p_device_profile) {
  623. }