Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions drivers/d3d12/rendering_shader_container_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ uint32_t RenderingShaderContainerD3D12::_to_bytes_footer_extra_data(uint8_t *p_b
}

#if NIR_ENABLED
bool RenderingShaderContainerD3D12::_convert_spirv_to_nir(Span<ReflectedShaderStage> p_spirv, const nir_shader_compiler_options *p_compiler_options, HashMap<int, nir_shader *> &r_stages_nir_shaders, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed) {
bool RenderingShaderContainerD3D12::_convert_spirv_to_nir(Span<ReflectShaderStage> p_spirv, const nir_shader_compiler_options *p_compiler_options, HashMap<int, nir_shader *> &r_stages_nir_shaders, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed) {
r_stages_processed.clear();

dxil_spirv_runtime_conf dxil_runtime_conf = {};
Expand Down Expand Up @@ -428,7 +428,7 @@ bool RenderingShaderContainerD3D12::_convert_nir_to_dxil(const HashMap<int, nir_
return true;
}

bool RenderingShaderContainerD3D12::_convert_spirv_to_dxil(Span<ReflectedShaderStage> p_spirv, HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_dxil_blobs, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed) {
bool RenderingShaderContainerD3D12::_convert_spirv_to_dxil(Span<ReflectShaderStage> p_spirv, HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_dxil_blobs, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed) {
r_dxil_blobs.clear();

HashMap<int, nir_shader *> stages_nir_shaders;
Expand Down Expand Up @@ -763,7 +763,7 @@ void RenderingShaderContainerD3D12::_nir_report_bitcode_bit_offset(uint64_t p_bi
}
#endif

void RenderingShaderContainerD3D12::_set_from_shader_reflection_post(const RenderingDeviceCommons::ShaderReflection &p_reflection) {
void RenderingShaderContainerD3D12::_set_from_shader_reflection_post(const ReflectShader &p_shader) {
reflection_binding_set_uniforms_data_d3d12.resize(reflection_binding_set_uniforms_data.size());
reflection_specialization_data_d3d12.resize(reflection_specialization_data.size());

Expand All @@ -779,8 +779,9 @@ void RenderingShaderContainerD3D12::_set_from_shader_reflection_post(const Rende
}
}

bool RenderingShaderContainerD3D12::_set_code_from_spirv(Span<ReflectedShaderStage> p_spirv) {
bool RenderingShaderContainerD3D12::_set_code_from_spirv(const ReflectShader &p_shader) {
#if NIR_ENABLED
const LocalVector<ReflectShaderStage> &p_spirv = p_shader.shader_stages;
reflection_data_d3d12.nir_runtime_data_root_param_idx = UINT32_MAX;

for (int64_t i = 0; i < reflection_specialization_data.size(); i++) {
Expand Down
8 changes: 4 additions & 4 deletions drivers/d3d12/rendering_shader_container_d3d12.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ class RenderingShaderContainerD3D12 : public RenderingShaderContainer {
uint32_t root_signature_crc = 0;

#if NIR_ENABLED
bool _convert_spirv_to_nir(Span<ReflectedShaderStage> p_spirv, const nir_shader_compiler_options *p_compiler_options, HashMap<int, nir_shader *> &r_stages_nir_shaders, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed);
bool _convert_spirv_to_nir(Span<ReflectShaderStage> p_spirv, const nir_shader_compiler_options *p_compiler_options, HashMap<int, nir_shader *> &r_stages_nir_shaders, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed);
bool _convert_nir_to_dxil(const HashMap<int, nir_shader *> &p_stages_nir_shaders, BitField<RenderingDeviceCommons::ShaderStage> p_stages_processed, HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_dxil_blobs);
bool _convert_spirv_to_dxil(Span<ReflectedShaderStage> p_spirv, HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_dxil_blobs, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed);
bool _convert_spirv_to_dxil(Span<ReflectShaderStage> p_spirv, HashMap<RenderingDeviceCommons::ShaderStage, Vector<uint8_t>> &r_dxil_blobs, Vector<RenderingDeviceCommons::ShaderStage> &r_stages, BitField<RenderingDeviceCommons::ShaderStage> &r_stages_processed);
bool _generate_root_signature(BitField<RenderingDeviceCommons::ShaderStage> p_stages_processed);

// GodotNirCallbacks.
Expand All @@ -146,8 +146,8 @@ class RenderingShaderContainerD3D12 : public RenderingShaderContainer {
virtual uint32_t _to_bytes_reflection_binding_uniform_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
virtual uint32_t _to_bytes_reflection_specialization_extra_data(uint8_t *p_bytes, uint32_t p_index) const override;
virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const override;
virtual void _set_from_shader_reflection_post(const RenderingDeviceCommons::ShaderReflection &p_reflection) override;
virtual bool _set_code_from_spirv(Span<ReflectedShaderStage> p_spirv) override;
virtual void _set_from_shader_reflection_post(const ReflectShader &p_shader) override;
virtual bool _set_code_from_spirv(const ReflectShader &p_shader) override;

public:
struct ShaderReflectionD3D12 {
Expand Down
1 change: 1 addition & 0 deletions drivers/metal/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ env_metal.Append(CCFLAGS=["-fmodules", "-fcxx-modules"])
driver_obj = []

env_metal.add_source_files(driver_obj, "*.mm")
env_metal.add_source_files(driver_obj, "*.cpp")
env.drivers_sources += driver_obj

# Needed to force rebuilding the driver files when the thirdparty library is updated.
Expand Down
129 changes: 129 additions & 0 deletions drivers/metal/metal_device_profile.cpp
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Device Profile is now keyed by platform (macOS, iOS, etc), GPU and minimum OS version. This ensures that when generating or baking the shader, it selects the correct features based on the target OS also.

Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**************************************************************************/
/* metal_device_profile.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "metal_device_profile.h"

#include "metal_utils.h"

Mutex MetalDeviceProfile::profiles_lock;
HashMap<MetalDeviceProfile::ProfileKey, MetalDeviceProfile> MetalDeviceProfile::profiles;

const MetalDeviceProfile *MetalDeviceProfile::get_profile(Platform p_platform, GPU p_gpu, MinOsVersion p_min_os_version) {
DEV_ASSERT(p_platform == Platform::macOS || p_platform == Platform::iOS || p_platform == Platform::visionOS);

MutexLock lock(profiles_lock);

ProfileKey key(p_min_os_version, p_platform, p_gpu);
if (MetalDeviceProfile *profile = profiles.getptr(key)) {
return profile;
}

MetalDeviceProfile res;
res.platform = p_platform;
res.gpu = p_gpu;
res.min_os_version = p_min_os_version;

switch (p_platform) {
case Platform::macOS: {
if (p_min_os_version >= os_version::MACOS_26_0) {
res.features.msl_version = MSL_VERSION_40;
} else if (p_min_os_version >= os_version::MACOS_15_0) {
res.features.msl_version = MSL_VERSION_32;
} else if (p_min_os_version >= os_version::MACOS_14_0) {
res.features.msl_version = MSL_VERSION_31;
} else if (p_min_os_version >= os_version::MACOS_13_0) {
res.features.msl_version = MSL_VERSION_30;
} else if (p_min_os_version >= os_version::MACOS_12_0) {
res.features.msl_version = MSL_VERSION_24;
} else {
res.features.msl_version = MSL_VERSION_23;
}
res.features.use_argument_buffers = p_min_os_version >= os_version::MACOS_13_0;
res.features.simdPermute = true;
} break;

case Platform::iOS: {
if (p_min_os_version >= os_version::IOS_26_0) {
res.features.msl_version = MSL_VERSION_40;
} else if (p_min_os_version >= os_version::IOS_18_0) {
res.features.msl_version = MSL_VERSION_32;
} else if (p_min_os_version >= os_version::IOS_17_0) {
res.features.msl_version = MSL_VERSION_31;
} else if (p_min_os_version >= os_version::IOS_16_0) {
res.features.msl_version = MSL_VERSION_30;
} else if (p_min_os_version >= os_version::IOS_15_0) {
res.features.msl_version = MSL_VERSION_24;
} else {
res.features.msl_version = MSL_VERSION_23;
}

switch (p_gpu) {
case GPU::Apple1:
case GPU::Apple2:
case GPU::Apple3:
case GPU::Apple4:
case GPU::Apple5: {
res.features.simdPermute = false;
res.features.use_argument_buffers = false;
} break;
case GPU::Apple6:
case GPU::Apple7:
case GPU::Apple8:
case GPU::Apple9: {
res.features.use_argument_buffers = p_min_os_version >= os_version::IOS_16_0;
res.features.simdPermute = true;
} break;
}
} break;

case Platform::visionOS: {
if (p_min_os_version >= os_version::VISIONOS_26_0) {
res.features.msl_version = MSL_VERSION_40;
} else if (p_min_os_version >= os_version::VISIONOS_02_4) {
res.features.msl_version = MSL_VERSION_32;
} else {
ERR_FAIL_V_MSG(nullptr, "visionOS 2.4 is the minimum supported version for visionOS.");
}

switch (p_gpu) {
case GPU::Apple8:
case GPU::Apple9: {
res.features.use_argument_buffers = true;
res.features.simdPermute = true;
} break;
default: {
CRASH_NOW_MSG("visionOS hardware has a minimum Apple8 GPU.");
}
}
} break;
}

return &profiles.insert(key, res)->value;
}
157 changes: 157 additions & 0 deletions drivers/metal/metal_device_profile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/**************************************************************************/
/* metal_device_profile.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#include "core/os/mutex.h"
#include "core/string/ustring.h"
#include "core/templates/hash_map.h"
#include "core/typedefs.h"

class MinOsVersion {
uint32_t version;

public:
String to_compiler_os_version() const;
bool is_null() const { return version == UINT32_MAX; }
bool is_valid() const { return version != UINT32_MAX; }

MinOsVersion(const String &p_version);
constexpr explicit MinOsVersion(uint32_t p_version) :
version(p_version) {}
constexpr MinOsVersion(uint32_t p_major, uint32_t p_minor, uint32_t p_patch = 0) :
version(p_major * 10000 + p_minor * 100 + p_patch) {}
constexpr MinOsVersion() :
version(UINT32_MAX) {}

bool operator>(uint32_t p_other) {
return version > p_other;
}
constexpr operator uint32_t() const { return version; }
};

namespace os_version {

constexpr MinOsVersion MACOS_26_0(26'00'00);
constexpr MinOsVersion MACOS_15_0(15'00'00);
constexpr MinOsVersion MACOS_14_0(14'00'00);
constexpr MinOsVersion MACOS_13_0(13'00'00);
constexpr MinOsVersion MACOS_12_0(12'00'00);
constexpr MinOsVersion MACOS_11_0(11'00'00);

constexpr MinOsVersion IOS_26_0(26'00'00);
constexpr MinOsVersion IOS_18_0(18'00'00);
constexpr MinOsVersion IOS_17_0(17'00'00);
constexpr MinOsVersion IOS_16_0(16'00'00);
constexpr MinOsVersion IOS_15_0(15'00'00);

constexpr MinOsVersion VISIONOS_26_0(26'00'00);
constexpr MinOsVersion VISIONOS_02_4(02'04'00);

} //namespace os_version

/// @brief A minimal structure that defines a device profile for Metal.
///
/// This structure is used by the `RenderingShaderContainerMetal` class to
/// determine options for compiling SPIR-V to Metal source. It currently only
/// contains the minimum properties required to transform shaders from SPIR-V to Metal
/// and potentially compile to a `.metallib`.
struct MetalDeviceProfile {
enum class Platform : uint32_t {
macOS = 0,
iOS = 1,
visionOS = 2,
};

/*! @brief The GPU family.
*
* NOTE: These values match Apple's MTLGPUFamily
*/
enum class GPU : uint32_t {
Apple1 = 1001,
Apple2 = 1002,
Apple3 = 1003,
Apple4 = 1004,
Apple5 = 1005,
Apple6 = 1006,
Apple7 = 1007,
Apple8 = 1008,
Apple9 = 1009,
};

enum class ArgumentBuffersTier : uint32_t {
Tier1 = 0,
Tier2 = 1,
};

struct Features {
uint32_t msl_version = 0;
bool use_argument_buffers = false;
bool simdPermute = false;
};

Platform platform = Platform::macOS;
GPU gpu = GPU::Apple4;
MinOsVersion min_os_version;
Features features;

static const MetalDeviceProfile *get_profile(Platform p_platform, GPU p_gpu, MinOsVersion p_min_os_version);

MetalDeviceProfile() = default;

private:
static Mutex profiles_lock; ///< Mutex to protect access to the profiles map.

struct ProfileKey {
friend struct HashMapHasherDefaultImpl<ProfileKey>;
union {
struct {
uint32_t min_os_version;
uint16_t platform;
uint16_t gpu;
};
uint64_t value = 0;
};

ProfileKey() = default;
ProfileKey(MinOsVersion p_min_os_version, Platform p_platform, GPU p_gpu) :
min_os_version(p_min_os_version), platform((uint16_t)p_platform), gpu((uint16_t)p_gpu) {}

_FORCE_INLINE_ uint32_t hash() const {
return hash_one_uint64(value);
}

bool operator==(const ProfileKey &p_other) const {
return value == p_other.value;
}
};

static HashMap<ProfileKey, MetalDeviceProfile> profiles;
};
Loading