You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Merge pull request #111976 from stuartcarnie/metal_stable_bindings
Metal: Stable argument buffers; GPU rendering crashes; visionOS exports
This commit is contained in:
@@ -43,20 +43,35 @@ static inline uint32_t aligned_to(uint32_t p_size, uint32_t p_alignment) {
|
||||
}
|
||||
}
|
||||
|
||||
RenderingShaderContainer::ReflectedShaderStage::ReflectedShaderStage() :
|
||||
_module(memnew(SpvReflectShaderModule)) {
|
||||
template <class T>
|
||||
const T &RenderingShaderContainer::ReflectSymbol<T>::get_spv_reflect(RDC::ShaderStage p_stage) const {
|
||||
const T *info = _spv_reflect[get_index_for_stage(p_stage)];
|
||||
DEV_ASSERT(info != nullptr); // Caller is expected to specify valid shader stages
|
||||
return *info;
|
||||
}
|
||||
|
||||
RenderingShaderContainer::ReflectedShaderStage::~ReflectedShaderStage() {
|
||||
template <class T>
|
||||
void RenderingShaderContainer::ReflectSymbol<T>::set_spv_reflect(RDC::ShaderStage p_stage, const T *p_spv) {
|
||||
stages.set_flag(1 << p_stage);
|
||||
_spv_reflect[get_index_for_stage(p_stage)] = p_spv;
|
||||
}
|
||||
|
||||
RenderingShaderContainer::ReflectShaderStage::ReflectShaderStage() {
|
||||
_module = memnew(SpvReflectShaderModule);
|
||||
memset(_module, 0, sizeof(SpvReflectShaderModule));
|
||||
}
|
||||
|
||||
RenderingShaderContainer::ReflectShaderStage::~ReflectShaderStage() {
|
||||
spvReflectDestroyShaderModule(_module);
|
||||
memdelete(_module);
|
||||
_module = nullptr;
|
||||
}
|
||||
|
||||
const SpvReflectShaderModule &RenderingShaderContainer::ReflectedShaderStage::module() const {
|
||||
const SpvReflectShaderModule &RenderingShaderContainer::ReflectShaderStage::module() const {
|
||||
return *_module;
|
||||
}
|
||||
|
||||
const Span<uint32_t> RenderingShaderContainer::ReflectedShaderStage::spirv() const {
|
||||
const Span<uint32_t> RenderingShaderContainer::ReflectShaderStage::spirv() const {
|
||||
return _spirv_data.span().reinterpret<uint32_t>();
|
||||
}
|
||||
|
||||
@@ -120,34 +135,128 @@ uint32_t RenderingShaderContainer::_to_bytes_footer_extra_data(uint8_t *) const
|
||||
return 0;
|
||||
}
|
||||
|
||||
void RenderingShaderContainer::_set_from_shader_reflection_post(const RenderingDeviceCommons::ShaderReflection &p_reflection) {
|
||||
void RenderingShaderContainer::_set_from_shader_reflection_post(const ReflectShader &p_shader) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv, LocalVector<ReflectedShaderStage> &r_refl) {
|
||||
static RenderingDeviceCommons::DataFormat spv_image_format_to_data_format(const SpvImageFormat p_format) {
|
||||
using RDC = RenderingDeviceCommons;
|
||||
RDC::ShaderReflection reflection;
|
||||
switch (p_format) {
|
||||
case SpvImageFormatUnknown:
|
||||
return RDC::DATA_FORMAT_MAX;
|
||||
case SpvImageFormatRgba32f:
|
||||
return RDC::DATA_FORMAT_R32G32B32A32_SFLOAT;
|
||||
case SpvImageFormatRgba16f:
|
||||
return RDC::DATA_FORMAT_R16G16B16A16_SFLOAT;
|
||||
case SpvImageFormatR32f:
|
||||
return RDC::DATA_FORMAT_R32_SFLOAT;
|
||||
case SpvImageFormatRgba8:
|
||||
return RDC::DATA_FORMAT_R8G8B8A8_UNORM;
|
||||
case SpvImageFormatRgba8Snorm:
|
||||
return RDC::DATA_FORMAT_R8G8B8A8_SNORM;
|
||||
case SpvImageFormatRg32f:
|
||||
return RDC::DATA_FORMAT_R32G32_SFLOAT;
|
||||
case SpvImageFormatRg16f:
|
||||
return RDC::DATA_FORMAT_R16G16_SFLOAT;
|
||||
case SpvImageFormatR11fG11fB10f:
|
||||
return RDC::DATA_FORMAT_B10G11R11_UFLOAT_PACK32;
|
||||
case SpvImageFormatR16f:
|
||||
return RDC::DATA_FORMAT_R16_SFLOAT;
|
||||
case SpvImageFormatRgba16:
|
||||
return RDC::DATA_FORMAT_R16G16B16A16_UNORM;
|
||||
case SpvImageFormatRgb10A2:
|
||||
return RDC::DATA_FORMAT_A2B10G10R10_UNORM_PACK32;
|
||||
case SpvImageFormatRg16:
|
||||
return RDC::DATA_FORMAT_R16G16_UNORM;
|
||||
case SpvImageFormatRg8:
|
||||
return RDC::DATA_FORMAT_R8G8_UNORM;
|
||||
case SpvImageFormatR16:
|
||||
return RDC::DATA_FORMAT_R16_UNORM;
|
||||
case SpvImageFormatR8:
|
||||
return RDC::DATA_FORMAT_R8_UNORM;
|
||||
case SpvImageFormatRgba16Snorm:
|
||||
return RDC::DATA_FORMAT_R16G16B16A16_SNORM;
|
||||
case SpvImageFormatRg16Snorm:
|
||||
return RDC::DATA_FORMAT_R16G16_SNORM;
|
||||
case SpvImageFormatRg8Snorm:
|
||||
return RDC::DATA_FORMAT_R8G8_SNORM;
|
||||
case SpvImageFormatR16Snorm:
|
||||
return RDC::DATA_FORMAT_R16_SNORM;
|
||||
case SpvImageFormatR8Snorm:
|
||||
return RDC::DATA_FORMAT_R8_SNORM;
|
||||
case SpvImageFormatRgba32i:
|
||||
return RDC::DATA_FORMAT_R32G32B32A32_SINT;
|
||||
case SpvImageFormatRgba16i:
|
||||
return RDC::DATA_FORMAT_R16G16B16A16_SINT;
|
||||
case SpvImageFormatRgba8i:
|
||||
return RDC::DATA_FORMAT_R8G8B8A8_SINT;
|
||||
case SpvImageFormatR32i:
|
||||
return RDC::DATA_FORMAT_R32_SINT;
|
||||
case SpvImageFormatRg32i:
|
||||
return RDC::DATA_FORMAT_R32G32_SINT;
|
||||
case SpvImageFormatRg16i:
|
||||
return RDC::DATA_FORMAT_R16G16_SINT;
|
||||
case SpvImageFormatRg8i:
|
||||
return RDC::DATA_FORMAT_R8G8_SINT;
|
||||
case SpvImageFormatR16i:
|
||||
return RDC::DATA_FORMAT_R16_SINT;
|
||||
case SpvImageFormatR8i:
|
||||
return RDC::DATA_FORMAT_R8_SINT;
|
||||
case SpvImageFormatRgba32ui:
|
||||
return RDC::DATA_FORMAT_R32G32B32A32_UINT;
|
||||
case SpvImageFormatRgba16ui:
|
||||
return RDC::DATA_FORMAT_R16G16B16A16_UINT;
|
||||
case SpvImageFormatRgba8ui:
|
||||
return RDC::DATA_FORMAT_R8G8B8A8_UINT;
|
||||
case SpvImageFormatR32ui:
|
||||
return RDC::DATA_FORMAT_R32_UINT;
|
||||
case SpvImageFormatRgb10a2ui:
|
||||
return RDC::DATA_FORMAT_A2B10G10R10_UINT_PACK32;
|
||||
case SpvImageFormatRg32ui:
|
||||
return RDC::DATA_FORMAT_R32G32_UINT;
|
||||
case SpvImageFormatRg16ui:
|
||||
return RDC::DATA_FORMAT_R16G16_UINT;
|
||||
case SpvImageFormatRg8ui:
|
||||
return RDC::DATA_FORMAT_R8G8_UINT;
|
||||
case SpvImageFormatR16ui:
|
||||
return RDC::DATA_FORMAT_R16_UINT;
|
||||
case SpvImageFormatR8ui:
|
||||
return RDC::DATA_FORMAT_R8_UINT;
|
||||
case SpvImageFormatR64ui:
|
||||
return RDC::DATA_FORMAT_R64_UINT;
|
||||
case SpvImageFormatR64i:
|
||||
return RDC::DATA_FORMAT_R64_SINT;
|
||||
case SpvImageFormatMax:
|
||||
return RDC::DATA_FORMAT_MAX;
|
||||
}
|
||||
return RDC::DATA_FORMAT_MAX;
|
||||
}
|
||||
|
||||
Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv, ReflectShader &r_shader) {
|
||||
ReflectShader &reflection = r_shader;
|
||||
|
||||
shader_name = p_shader_name.utf8();
|
||||
|
||||
const uint32_t spirv_size = p_spirv.size() + 0;
|
||||
|
||||
LocalVector<ReflectShaderStage> &r_refl = r_shader.shader_stages;
|
||||
r_refl.resize(spirv_size);
|
||||
|
||||
for (uint32_t i = 0; i < spirv_size; i++) {
|
||||
RDC::ShaderStage stage = p_spirv[i].shader_stage;
|
||||
RDC::ShaderStage stage_flag = (RDC::ShaderStage)(1 << p_spirv[i].shader_stage);
|
||||
r_refl[i].shader_stage = p_spirv[i].shader_stage;
|
||||
RDC::ShaderStage stage_flag = (RDC::ShaderStage)(1 << stage);
|
||||
r_refl[i].shader_stage = stage;
|
||||
r_refl[i]._spirv_data = p_spirv[i].spirv;
|
||||
|
||||
const Vector<uint64_t> &dynamic_buffers = p_spirv[i].dynamic_buffers;
|
||||
|
||||
if (p_spirv[i].shader_stage == RDC::SHADER_STAGE_COMPUTE) {
|
||||
reflection.is_compute = true;
|
||||
if (stage == RDC::SHADER_STAGE_COMPUTE) {
|
||||
ERR_FAIL_COND_V_MSG(spirv_size != 1, FAILED,
|
||||
"Compute shaders can only receive one stage, dedicated to compute.");
|
||||
}
|
||||
ERR_FAIL_COND_V_MSG(reflection.stages_bits.has_flag(stage_flag), FAILED,
|
||||
"Stage " + String(RDC::SHADER_STAGE_NAMES[p_spirv[i].shader_stage]) + " submitted more than once.");
|
||||
reflection.stages_bits.set_flag(stage_flag);
|
||||
|
||||
{
|
||||
SpvReflectShaderModule &module = *r_refl.ptr()[i]._module;
|
||||
@@ -163,7 +272,7 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
}
|
||||
}
|
||||
|
||||
if (reflection.is_compute) {
|
||||
if (reflection.is_compute()) {
|
||||
reflection.compute_local_size[0] = module.entry_points->local_size.x;
|
||||
reflection.compute_local_size[1] = module.entry_points->local_size.y;
|
||||
reflection.compute_local_size[2] = module.entry_points->local_size.z;
|
||||
@@ -186,11 +295,13 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
for (uint32_t j = 0; j < binding_count; j++) {
|
||||
const SpvReflectDescriptorBinding &binding = *bindings[j];
|
||||
|
||||
RDC::ShaderUniform uniform;
|
||||
ReflectUniform uniform;
|
||||
uniform.set_spv_reflect(stage, &binding);
|
||||
|
||||
bool need_array_dimensions = false;
|
||||
bool need_block_size = false;
|
||||
bool may_be_writable = false;
|
||||
bool is_image = false;
|
||||
|
||||
switch (binding.descriptor_type) {
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLER: {
|
||||
@@ -200,24 +311,29 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
|
||||
uniform.type = RDC::UNIFORM_TYPE_SAMPLER_WITH_TEXTURE;
|
||||
need_array_dimensions = true;
|
||||
is_image = true;
|
||||
} break;
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_SAMPLED_IMAGE: {
|
||||
uniform.type = RDC::UNIFORM_TYPE_TEXTURE;
|
||||
need_array_dimensions = true;
|
||||
is_image = true;
|
||||
} break;
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_IMAGE: {
|
||||
uniform.type = RDC::UNIFORM_TYPE_IMAGE;
|
||||
need_array_dimensions = true;
|
||||
may_be_writable = true;
|
||||
is_image = true;
|
||||
} break;
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER: {
|
||||
uniform.type = RDC::UNIFORM_TYPE_TEXTURE_BUFFER;
|
||||
need_array_dimensions = true;
|
||||
is_image = true;
|
||||
} break;
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER: {
|
||||
uniform.type = RDC::UNIFORM_TYPE_IMAGE_BUFFER;
|
||||
need_array_dimensions = true;
|
||||
may_be_writable = true;
|
||||
is_image = true;
|
||||
} break;
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_UNIFORM_BUFFER: {
|
||||
const uint64_t key = ShaderRD::DynamicBuffer::encode(binding.set, binding.binding);
|
||||
@@ -251,6 +367,7 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_INPUT_ATTACHMENT: {
|
||||
uniform.type = RDC::UNIFORM_TYPE_INPUT_ATTACHMENT;
|
||||
need_array_dimensions = true;
|
||||
is_image = true;
|
||||
} break;
|
||||
case SPV_REFLECT_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR: {
|
||||
ERR_PRINT("Acceleration structure not supported.");
|
||||
@@ -259,18 +376,10 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
}
|
||||
|
||||
if (need_array_dimensions) {
|
||||
if (binding.array.dims_count == 0) {
|
||||
uniform.length = 1;
|
||||
} else {
|
||||
for (uint32_t k = 0; k < binding.array.dims_count; k++) {
|
||||
if (k == 0) {
|
||||
uniform.length = binding.array.dims[0];
|
||||
} else {
|
||||
uniform.length *= binding.array.dims[k];
|
||||
}
|
||||
}
|
||||
uniform.length = 1;
|
||||
for (uint32_t k = 0; k < binding.array.dims_count; k++) {
|
||||
uniform.length *= binding.array.dims[k];
|
||||
}
|
||||
|
||||
} else if (need_block_size) {
|
||||
uniform.length = binding.block.size;
|
||||
} else {
|
||||
@@ -287,6 +396,10 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
uniform.writable = false;
|
||||
}
|
||||
|
||||
if (is_image) {
|
||||
uniform.image.format = spv_image_format_to_data_format(binding.image.image_format);
|
||||
}
|
||||
|
||||
uniform.binding = binding.binding;
|
||||
uint32_t set = binding.set;
|
||||
|
||||
@@ -296,7 +409,7 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
if (set < (uint32_t)reflection.uniform_sets.size()) {
|
||||
// Check if this already exists.
|
||||
bool exists = false;
|
||||
for (int k = 0; k < reflection.uniform_sets[set].size(); k++) {
|
||||
for (uint32_t k = 0; k < reflection.uniform_sets[set].size(); k++) {
|
||||
if (reflection.uniform_sets[set][k].binding == uniform.binding) {
|
||||
// Already exists, verify that it's the same type.
|
||||
ERR_FAIL_COND_V_MSG(reflection.uniform_sets[set][k].type != uniform.type, FAILED,
|
||||
@@ -311,7 +424,7 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
"On shader stage '" + String(RDC::SHADER_STAGE_NAMES[stage]) + "', uniform '" + binding.name + "' trying to reuse location for set=" + itos(set) + ", binding=" + itos(uniform.binding) + " with different writability.");
|
||||
|
||||
// Just append stage mask and return.
|
||||
reflection.uniform_sets.write[set].write[k].stages.set_flag(stage_flag);
|
||||
reflection.uniform_sets[set][k].stages.set_flag(stage_flag);
|
||||
exists = true;
|
||||
break;
|
||||
}
|
||||
@@ -328,7 +441,7 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
reflection.uniform_sets.resize(set + 1);
|
||||
}
|
||||
|
||||
reflection.uniform_sets.write[set].push_back(uniform);
|
||||
reflection.uniform_sets[set].push_back(uniform);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,8 +463,9 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
|
||||
for (uint32_t j = 0; j < sc_count; j++) {
|
||||
int32_t existing = -1;
|
||||
RDC::ShaderSpecializationConstant sconst;
|
||||
ReflectSpecializationConstant sconst;
|
||||
SpvReflectSpecializationConstant *spc = spec_constants[j];
|
||||
sconst.set_spv_reflect(stage, spc);
|
||||
|
||||
sconst.constant_id = spc->constant_id;
|
||||
sconst.int_value = 0; // Clear previous value JIC.
|
||||
@@ -371,7 +485,7 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
}
|
||||
sconst.stages.set_flag(stage_flag);
|
||||
|
||||
for (int k = 0; k < reflection.specialization_constants.size(); k++) {
|
||||
for (uint32_t k = 0; k < reflection.specialization_constants.size(); k++) {
|
||||
if (reflection.specialization_constants[k].constant_id == sconst.constant_id) {
|
||||
ERR_FAIL_COND_V_MSG(reflection.specialization_constants[k].type != sconst.type, FAILED, "More than one specialization constant used for id (" + itos(sconst.constant_id) + "), but their types differ.");
|
||||
ERR_FAIL_COND_V_MSG(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.");
|
||||
@@ -381,7 +495,7 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
}
|
||||
|
||||
if (existing >= 0) {
|
||||
reflection.specialization_constants.write[existing].stages.set_flag(stage_flag);
|
||||
reflection.specialization_constants[existing].stages.set_flag(stage_flag);
|
||||
} else {
|
||||
reflection.specialization_constants.push_back(sconst);
|
||||
}
|
||||
@@ -476,13 +590,11 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
//print_line("Stage: " + String(RDC::SHADER_STAGE_NAMES[stage]) + " push constant of size=" + itos(push_constant.push_constant_size));
|
||||
}
|
||||
}
|
||||
|
||||
reflection.stages_bits.set_flag(stage_flag);
|
||||
}
|
||||
|
||||
// Sort all uniform_sets by binding.
|
||||
for (uint32_t i = 0; i < reflection.uniform_sets.size(); i++) {
|
||||
reflection.uniform_sets.write[i].sort();
|
||||
reflection.uniform_sets[i].sort();
|
||||
}
|
||||
|
||||
set_from_shader_reflection(reflection);
|
||||
@@ -490,7 +602,7 @@ Error RenderingShaderContainer::reflect_spirv(const String &p_shader_name, Span<
|
||||
return OK;
|
||||
}
|
||||
|
||||
void RenderingShaderContainer::set_from_shader_reflection(const RenderingDeviceCommons::ShaderReflection &p_reflection) {
|
||||
void RenderingShaderContainer::set_from_shader_reflection(const ReflectShader &p_reflection) {
|
||||
reflection_binding_set_uniforms_count.clear();
|
||||
reflection_binding_set_uniforms_data.clear();
|
||||
reflection_specialization_data.clear();
|
||||
@@ -499,7 +611,7 @@ void RenderingShaderContainer::set_from_shader_reflection(const RenderingDeviceC
|
||||
reflection_data.vertex_input_mask = p_reflection.vertex_input_mask;
|
||||
reflection_data.fragment_output_mask = p_reflection.fragment_output_mask;
|
||||
reflection_data.specialization_constants_count = p_reflection.specialization_constants.size();
|
||||
reflection_data.is_compute = p_reflection.is_compute;
|
||||
reflection_data.is_compute = p_reflection.is_compute();
|
||||
reflection_data.has_multiview = p_reflection.has_multiview;
|
||||
reflection_data.has_dynamic_buffers = p_reflection.has_dynamic_buffers;
|
||||
reflection_data.compute_local_size[0] = p_reflection.compute_local_size[0];
|
||||
@@ -511,8 +623,8 @@ void RenderingShaderContainer::set_from_shader_reflection(const RenderingDeviceC
|
||||
reflection_data.shader_name_len = shader_name.length();
|
||||
|
||||
ReflectionBindingData binding_data;
|
||||
for (const Vector<RenderingDeviceCommons::ShaderUniform> &uniform_set : p_reflection.uniform_sets) {
|
||||
for (const RenderingDeviceCommons::ShaderUniform &uniform : uniform_set) {
|
||||
for (const ReflectDescriptorSet &uniform_set : p_reflection.uniform_sets) {
|
||||
for (const ReflectUniform &uniform : uniform_set) {
|
||||
binding_data.type = uint32_t(uniform.type);
|
||||
binding_data.binding = uniform.binding;
|
||||
binding_data.stages = uint32_t(uniform.stages);
|
||||
@@ -525,7 +637,7 @@ void RenderingShaderContainer::set_from_shader_reflection(const RenderingDeviceC
|
||||
}
|
||||
|
||||
ReflectionSpecializationData specialization_data;
|
||||
for (const RenderingDeviceCommons::ShaderSpecializationConstant &spec : p_reflection.specialization_constants) {
|
||||
for (const ReflectSpecializationConstant &spec : p_reflection.specialization_constants) {
|
||||
specialization_data.type = uint32_t(spec.type);
|
||||
specialization_data.constant_id = spec.constant_id;
|
||||
specialization_data.int_value = spec.int_value;
|
||||
@@ -533,9 +645,9 @@ void RenderingShaderContainer::set_from_shader_reflection(const RenderingDeviceC
|
||||
reflection_specialization_data.push_back(specialization_data);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < RenderingDeviceCommons::SHADER_STAGE_MAX; i++) {
|
||||
if (p_reflection.stages_bits.has_flag(RenderingDeviceCommons::ShaderStage(1U << i))) {
|
||||
reflection_shader_stages.push_back(RenderingDeviceCommons::ShaderStage(i));
|
||||
for (uint32_t i = 0; i < RDC::SHADER_STAGE_MAX; i++) {
|
||||
if (p_reflection.stages_bits.has_flag(RDC::ShaderStage(1U << i))) {
|
||||
reflection_shader_stages.push_back(RDC::ShaderStage(i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -544,14 +656,14 @@ void RenderingShaderContainer::set_from_shader_reflection(const RenderingDeviceC
|
||||
_set_from_shader_reflection_post(p_reflection);
|
||||
}
|
||||
|
||||
bool RenderingShaderContainer::set_code_from_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv) {
|
||||
LocalVector<ReflectedShaderStage> spirv;
|
||||
ERR_FAIL_COND_V(reflect_spirv(p_shader_name, p_spirv, spirv) != OK, false);
|
||||
return _set_code_from_spirv(spirv.span());
|
||||
bool RenderingShaderContainer::set_code_from_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv) {
|
||||
ReflectShader shader;
|
||||
ERR_FAIL_COND_V(reflect_spirv(p_shader_name, p_spirv, shader) != OK, false);
|
||||
return _set_code_from_spirv(shader);
|
||||
}
|
||||
|
||||
RenderingDeviceCommons::ShaderReflection RenderingShaderContainer::get_shader_reflection() const {
|
||||
RenderingDeviceCommons::ShaderReflection shader_refl;
|
||||
RDC::ShaderReflection shader_refl;
|
||||
shader_refl.push_constant_size = reflection_data.push_constant_size;
|
||||
shader_refl.push_constant_stages = reflection_data.push_constant_stages_mask;
|
||||
shader_refl.vertex_input_mask = reflection_data.vertex_input_mask;
|
||||
@@ -569,13 +681,13 @@ RenderingDeviceCommons::ShaderReflection RenderingShaderContainer::get_shader_re
|
||||
DEV_ASSERT(reflection_binding_set_uniforms_count.size() == reflection_data.set_count && "The amount of elements in the reflection and the shader container can't be different.");
|
||||
uint32_t uniform_index = 0;
|
||||
for (uint32_t i = 0; i < reflection_data.set_count; i++) {
|
||||
Vector<RenderingDeviceCommons::ShaderUniform> &uniform_set = shader_refl.uniform_sets.ptrw()[i];
|
||||
Vector<RDC::ShaderUniform> &uniform_set = shader_refl.uniform_sets.ptrw()[i];
|
||||
uint32_t uniforms_count = reflection_binding_set_uniforms_count[i];
|
||||
uniform_set.resize(uniforms_count);
|
||||
for (uint32_t j = 0; j < uniforms_count; j++) {
|
||||
const ReflectionBindingData &binding = reflection_binding_set_uniforms_data[uniform_index++];
|
||||
RenderingDeviceCommons::ShaderUniform &uniform = uniform_set.ptrw()[j];
|
||||
uniform.type = RenderingDeviceCommons::UniformType(binding.type);
|
||||
RDC::ShaderUniform &uniform = uniform_set.ptrw()[j];
|
||||
uniform.type = RDC::UniformType(binding.type);
|
||||
uniform.writable = binding.writable;
|
||||
uniform.length = binding.length;
|
||||
uniform.binding = binding.binding;
|
||||
@@ -586,8 +698,8 @@ RenderingDeviceCommons::ShaderReflection RenderingShaderContainer::get_shader_re
|
||||
shader_refl.specialization_constants.resize(reflection_data.specialization_constants_count);
|
||||
for (uint32_t i = 0; i < reflection_data.specialization_constants_count; i++) {
|
||||
const ReflectionSpecializationData &spec = reflection_specialization_data[i];
|
||||
RenderingDeviceCommons::ShaderSpecializationConstant &sc = shader_refl.specialization_constants.ptrw()[i];
|
||||
sc.type = RenderingDeviceCommons::PipelineSpecializationConstantType(spec.type);
|
||||
RDC::ShaderSpecializationConstant &sc = shader_refl.specialization_constants.ptrw()[i];
|
||||
sc.type = RDC::PipelineSpecializationConstantType(spec.type);
|
||||
sc.constant_id = spec.constant_id;
|
||||
sc.int_value = spec.int_value;
|
||||
sc.stages = spec.stage_flags;
|
||||
@@ -596,7 +708,7 @@ RenderingDeviceCommons::ShaderReflection RenderingShaderContainer::get_shader_re
|
||||
shader_refl.stages_vector.resize(reflection_data.stage_count);
|
||||
for (uint32_t i = 0; i < reflection_data.stage_count; i++) {
|
||||
shader_refl.stages_vector.set(i, reflection_shader_stages[i]);
|
||||
shader_refl.stages_bits.set_flag(RenderingDeviceCommons::ShaderStage(1U << reflection_shader_stages[i]));
|
||||
shader_refl.stages_bits.set_flag(RDC::ShaderStage(1U << reflection_shader_stages[i]));
|
||||
}
|
||||
|
||||
return shader_refl;
|
||||
@@ -672,11 +784,11 @@ bool RenderingShaderContainer::from_bytes(const PackedByteArray &p_bytes) {
|
||||
|
||||
const uint32_t stage_count = reflection_data.stage_count;
|
||||
if (stage_count > 0) {
|
||||
ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + stage_count * sizeof(RenderingDeviceCommons::ShaderStage)) > p_bytes.size(), false, "Not enough bytes for stages in shader container.");
|
||||
ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + stage_count * sizeof(RDC::ShaderStage)) > p_bytes.size(), false, "Not enough bytes for stages in shader container.");
|
||||
reflection_shader_stages.resize(stage_count);
|
||||
bytes_offset += _from_bytes_shader_extra_data_start(&bytes_ptr[bytes_offset]);
|
||||
memcpy(reflection_shader_stages.ptrw(), &bytes_ptr[bytes_offset], stage_count * sizeof(RenderingDeviceCommons::ShaderStage));
|
||||
bytes_offset += stage_count * sizeof(RenderingDeviceCommons::ShaderStage);
|
||||
memcpy(reflection_shader_stages.ptrw(), &bytes_ptr[bytes_offset], stage_count * sizeof(RDC::ShaderStage));
|
||||
bytes_offset += stage_count * sizeof(RDC::ShaderStage);
|
||||
}
|
||||
|
||||
// Read shaders.
|
||||
@@ -687,7 +799,7 @@ bool RenderingShaderContainer::from_bytes(const PackedByteArray &p_bytes) {
|
||||
|
||||
ERR_FAIL_COND_V_MSG(int64_t(bytes_offset + header.code_compressed_size) > p_bytes.size(), false, "Not enough bytes for a shader in shader container.");
|
||||
Shader &shader = shaders.ptrw()[i];
|
||||
shader.shader_stage = RenderingDeviceCommons::ShaderStage(header.shader_stage);
|
||||
shader.shader_stage = RDC::ShaderStage(header.shader_stage);
|
||||
shader.code_compression_flags = header.code_compression_flags;
|
||||
shader.code_decompressed_size = header.code_decompressed_size;
|
||||
shader.code_compressed_bytes.resize(header.code_compressed_size);
|
||||
@@ -712,7 +824,7 @@ PackedByteArray RenderingShaderContainer::to_bytes() const {
|
||||
total_size += reflection_binding_set_uniforms_count.size() * sizeof(uint32_t);
|
||||
total_size += reflection_binding_set_uniforms_data.size() * sizeof(ReflectionBindingData);
|
||||
total_size += reflection_specialization_data.size() * sizeof(ReflectionSpecializationData);
|
||||
total_size += reflection_shader_stages.size() * sizeof(RenderingDeviceCommons::ShaderStage);
|
||||
total_size += reflection_shader_stages.size() * sizeof(RDC::ShaderStage);
|
||||
|
||||
for (uint32_t i = 0; i < reflection_binding_set_uniforms_data.size(); i++) {
|
||||
total_size += _to_bytes_reflection_binding_uniform_extra_data(nullptr, i);
|
||||
@@ -777,8 +889,8 @@ PackedByteArray RenderingShaderContainer::to_bytes() const {
|
||||
|
||||
if (!reflection_shader_stages.is_empty()) {
|
||||
uint32_t stage_count = reflection_shader_stages.size();
|
||||
memcpy(&bytes_ptr[bytes_offset], reflection_shader_stages.ptr(), stage_count * sizeof(RenderingDeviceCommons::ShaderStage));
|
||||
bytes_offset += stage_count * sizeof(RenderingDeviceCommons::ShaderStage);
|
||||
memcpy(&bytes_ptr[bytes_offset], reflection_shader_stages.ptr(), stage_count * sizeof(RDC::ShaderStage));
|
||||
bytes_offset += stage_count * sizeof(RDC::ShaderStage);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < shaders.size(); i++) {
|
||||
|
||||
@@ -34,6 +34,8 @@
|
||||
#include "servers/rendering/rendering_device_commons.h"
|
||||
|
||||
struct SpvReflectShaderModule;
|
||||
struct SpvReflectDescriptorBinding;
|
||||
struct SpvReflectSpecializationConstant;
|
||||
|
||||
class RenderingShaderContainer : public RefCounted {
|
||||
GDSOFTCLASS(RenderingShaderContainer, RefCounted);
|
||||
@@ -43,6 +45,8 @@ public:
|
||||
static const uint32_t CONTAINER_VERSION = 2;
|
||||
|
||||
protected:
|
||||
using RDC = RenderingDeviceCommons;
|
||||
|
||||
struct ContainerHeader {
|
||||
uint32_t magic_number = 0;
|
||||
uint32_t version = 0;
|
||||
@@ -96,7 +100,7 @@ protected:
|
||||
Vector<uint32_t> reflection_binding_set_uniforms_count;
|
||||
Vector<ReflectionBindingData> reflection_binding_set_uniforms_data;
|
||||
Vector<ReflectionSpecializationData> reflection_specialization_data;
|
||||
Vector<RenderingDeviceCommons::ShaderStage> reflection_shader_stages;
|
||||
Vector<RDC::ShaderStage> reflection_shader_stages;
|
||||
|
||||
virtual uint32_t _format() const = 0;
|
||||
virtual uint32_t _format_version() const = 0;
|
||||
@@ -120,30 +124,159 @@ protected:
|
||||
virtual uint32_t _to_bytes_shader_extra_data(uint8_t *p_bytes, uint32_t p_index) const;
|
||||
virtual uint32_t _to_bytes_footer_extra_data(uint8_t *p_bytes) const;
|
||||
|
||||
// This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.
|
||||
virtual void _set_from_shader_reflection_post(const RenderingDeviceCommons::ShaderReflection &p_reflection);
|
||||
template <class T>
|
||||
struct ReflectSymbol {
|
||||
static constexpr uint32_t STAGE_INDEX[RDC::SHADER_STAGE_MAX] = {
|
||||
0, // SHADER_STAGE_VERTEX
|
||||
1, // SHADER_STAGE_FRAGMENT
|
||||
0, // SHADER_STAGE_TESSELATION_CONTROL
|
||||
1, // SHADER_STAGE_TESSELATION_EVALUATION
|
||||
0, // SHADER_STAGE_COMPUTE
|
||||
};
|
||||
|
||||
class ReflectedShaderStage {
|
||||
BitField<RDC::ShaderStage> stages = {};
|
||||
|
||||
private:
|
||||
const T *_spv_reflect[2] = { nullptr };
|
||||
|
||||
public:
|
||||
_FORCE_INLINE_ constexpr uint32_t get_index_for_stage(RDC::ShaderStage p_stage) const {
|
||||
DEV_ASSERT(stages.has_flag((1 << p_stage)));
|
||||
return STAGE_INDEX[p_stage];
|
||||
}
|
||||
|
||||
const T &get_spv_reflect(RDC::ShaderStage p_stage) const;
|
||||
|
||||
/*! Returns the first valid stage if multiple stages are set.
|
||||
*
|
||||
* Crashes if no stages are set.
|
||||
*/
|
||||
const T &get_spv_reflect() const {
|
||||
for (const T *d : _spv_reflect) {
|
||||
if (d != nullptr) {
|
||||
return *d;
|
||||
}
|
||||
}
|
||||
CRASH_NOW_MSG("No stages set in ReflectSymbol");
|
||||
}
|
||||
void set_spv_reflect(RDC::ShaderStage p_stage, const T *p_spv);
|
||||
};
|
||||
|
||||
struct ReflectImageTraits {
|
||||
RDC::DataFormat format = RDC::DATA_FORMAT_MAX;
|
||||
};
|
||||
|
||||
struct ReflectUniform : ReflectSymbol<SpvReflectDescriptorBinding> {
|
||||
RDC::UniformType type = RDC::UniformType::UNIFORM_TYPE_MAX;
|
||||
uint32_t binding = 0;
|
||||
|
||||
ReflectImageTraits image;
|
||||
|
||||
uint32_t length = 0; // Size of arrays (in total elements), or ubos (in bytes * total elements).
|
||||
bool writable = false;
|
||||
|
||||
bool operator<(const ReflectUniform &p_other) const {
|
||||
if (binding != p_other.binding) {
|
||||
return binding < p_other.binding;
|
||||
}
|
||||
if (type != p_other.type) {
|
||||
return type < p_other.type;
|
||||
}
|
||||
if (writable != p_other.writable) {
|
||||
return writable < p_other.writable;
|
||||
}
|
||||
if (stages != p_other.stages) {
|
||||
return stages < p_other.stages;
|
||||
}
|
||||
if (length != p_other.length) {
|
||||
return length < p_other.length;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
struct ReflectSpecializationConstant : ReflectSymbol<SpvReflectSpecializationConstant> {
|
||||
RDC::PipelineSpecializationConstantType type = {};
|
||||
uint32_t constant_id = 0xffffffff;
|
||||
union {
|
||||
uint32_t int_value = 0;
|
||||
float float_value;
|
||||
bool bool_value;
|
||||
};
|
||||
|
||||
bool operator<(const ReflectSpecializationConstant &p_other) const { return constant_id < p_other.constant_id; }
|
||||
};
|
||||
|
||||
class ReflectShaderStage {
|
||||
friend class RenderingShaderContainer;
|
||||
|
||||
Vector<uint8_t> _spirv_data;
|
||||
SpvReflectShaderModule *_module = nullptr;
|
||||
|
||||
public:
|
||||
RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;
|
||||
RDC::ShaderStage shader_stage = RDC::SHADER_STAGE_MAX;
|
||||
const SpvReflectShaderModule &module() const;
|
||||
const Span<uint32_t> spirv() const;
|
||||
const Vector<uint8_t> spirv_data() const { return _spirv_data; }
|
||||
|
||||
ReflectedShaderStage();
|
||||
~ReflectedShaderStage();
|
||||
ReflectShaderStage();
|
||||
~ReflectShaderStage();
|
||||
};
|
||||
|
||||
// This method will be called when set_code_from_spirv() is called.
|
||||
virtual bool _set_code_from_spirv(Span<ReflectedShaderStage> p_spirv) = 0;
|
||||
typedef LocalVector<ReflectUniform> ReflectDescriptorSet;
|
||||
|
||||
void set_from_shader_reflection(const RenderingDeviceCommons::ShaderReflection &p_reflection);
|
||||
Error reflect_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv, LocalVector<ReflectedShaderStage> &r_refl);
|
||||
struct ReflectShader {
|
||||
uint64_t vertex_input_mask = 0;
|
||||
uint32_t fragment_output_mask = 0;
|
||||
uint32_t compute_local_size[3] = {};
|
||||
uint32_t push_constant_size = 0;
|
||||
bool has_multiview = false;
|
||||
bool has_dynamic_buffers = false;
|
||||
|
||||
LocalVector<ReflectShaderStage> shader_stages;
|
||||
LocalVector<ReflectDescriptorSet> uniform_sets;
|
||||
LocalVector<ReflectSymbol<SpvReflectDescriptorBinding>> reflect_uniforms;
|
||||
LocalVector<ReflectSpecializationConstant> specialization_constants;
|
||||
LocalVector<ReflectSymbol<SpvReflectSpecializationConstant>> reflect_specialization_constants;
|
||||
LocalVector<RDC::ShaderStage> stages_vector;
|
||||
BitField<RDC::ShaderStage> stages_bits = {};
|
||||
BitField<RDC::ShaderStage> push_constant_stages = {};
|
||||
|
||||
_FORCE_INLINE_ bool is_compute() const {
|
||||
return stages_bits.has_flag(RDC::SHADER_STAGE_COMPUTE_BIT);
|
||||
}
|
||||
|
||||
/*! Returns the uniform at the specified global index.
|
||||
*
|
||||
* This is a flattened view of all uniform sets.
|
||||
*/
|
||||
ReflectUniform &uniform_at(uint32_t p_index) {
|
||||
for (LocalVector<ReflectUniform> &set : uniform_sets) {
|
||||
if (p_index < set.size()) {
|
||||
return set[p_index];
|
||||
}
|
||||
p_index -= set.size();
|
||||
}
|
||||
CRASH_NOW_MSG(vformat("Uniform index %d out of range (total %d)", p_index, uniform_count()));
|
||||
}
|
||||
|
||||
uint32_t uniform_count() const {
|
||||
uint32_t count = 0;
|
||||
for (const LocalVector<ReflectUniform> &set : uniform_sets) {
|
||||
count += set.size();
|
||||
}
|
||||
return count;
|
||||
}
|
||||
};
|
||||
|
||||
// This method will be called when set_from_shader_reflection() is finished. Used to update internal structures to match the reflection if necessary.
|
||||
virtual void _set_from_shader_reflection_post(const ReflectShader &p_shader);
|
||||
|
||||
// This method will be called when set_code_from_spirv() is called.
|
||||
virtual bool _set_code_from_spirv(const ReflectShader &p_shader) = 0;
|
||||
|
||||
void set_from_shader_reflection(const ReflectShader &p_reflection);
|
||||
Error reflect_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv, ReflectShader &r_shader);
|
||||
|
||||
public:
|
||||
enum CompressionFlags {
|
||||
@@ -151,7 +284,7 @@ public:
|
||||
};
|
||||
|
||||
struct Shader {
|
||||
RenderingDeviceCommons::ShaderStage shader_stage = RenderingDeviceCommons::SHADER_STAGE_MAX;
|
||||
RDC::ShaderStage shader_stage = RDC::SHADER_STAGE_MAX;
|
||||
PackedByteArray code_compressed_bytes;
|
||||
uint32_t code_compression_flags = 0;
|
||||
uint32_t code_decompressed_size = 0;
|
||||
@@ -160,8 +293,8 @@ public:
|
||||
CharString shader_name;
|
||||
Vector<Shader> shaders;
|
||||
|
||||
bool set_code_from_spirv(const String &p_shader_name, Span<RenderingDeviceCommons::ShaderStageSPIRVData> p_spirv);
|
||||
RenderingDeviceCommons::ShaderReflection get_shader_reflection() const;
|
||||
bool set_code_from_spirv(const String &p_shader_name, Span<RDC::ShaderStageSPIRVData> p_spirv);
|
||||
RDC::ShaderReflection get_shader_reflection() const;
|
||||
bool from_bytes(const PackedByteArray &p_bytes);
|
||||
PackedByteArray to_bytes() const;
|
||||
bool compress_code(const uint8_t *p_decompressed_bytes, uint32_t p_decompressed_size, uint8_t *p_compressed_bytes, uint32_t *r_compressed_size, uint32_t *r_compressed_flags) const;
|
||||
|
||||
Reference in New Issue
Block a user