Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

metal: Fix cache invalidation when directly binding push constants

Stuart Carnie2259e87732a1
diff --git a/drivers/metal/metal3_objects.cpp b/drivers/metal/metal3_objects.cpp
index 2eb60cbd8ae..0c4334768c2 100644
--- a/drivers/metal/metal3_objects.cpp
+++ b/drivers/metal/metal3_objects.cpp
@@ -414,11 +414,9 @@ void MDCommandBuffer::bind_pipeline(RDD::PipelineID p_pipeline) {
render.dirty.set_flag((RenderState::DirtyFlag)(RenderState::DIRTY_PIPELINE | RenderState::DIRTY_RASTER));
// Mark all uniforms as dirty, as variants of a shader pipeline may have a different entry point ABI,
// due to setting force_active_argument_buffer_resources = true for spirv_cross::CompilerMSL::Options.
- // As a result, uniform sets with the same layout will generate redundant binding warnings when
- // capturing a Metal frame in Xcode.
//
- // If we don't mark as dirty, then some bindings will generate a validation error.
- // binding_cache.clear();
+ // binding_cache is kept, so unchanged bindings are not reissued. Slots written outside the
+ // cache (push constants, view mask) are invalidated at the write site in _render_set_dirty_state.
render.mark_uniforms_dirty();
render.pipeline = rp;
render.raster_state = rp->raster_state;
@@ -1045,12 +1043,16 @@ void MDCommandBuffer::_render_set_dirty_state() {
_render_bind_uniform_sets();
if (render.dirty.has_flag(RenderState::DIRTY_PUSH)) {
+ uint32_t slot = push_constant_binding.get_binding();
if (push_constant_binding.has(RDD::SHADER_STAGE_VERTEX)) {
- render.encoder->setVertexBytes(push_constant_data, push_constant_data_len, push_constant_binding.get_binding());
+ render.encoder->setVertexBytes(push_constant_data, push_constant_data_len, slot);
+ render.arg_buffer_cache[RenderState::ARG_BUFFER_STAGE_VERTEX].invalidate_buffer(slot);
}
if (push_constant_binding.has(RDD::SHADER_STAGE_FRAGMENT)) {
- render.encoder->setFragmentBytes(push_constant_data, push_constant_data_len, push_constant_binding.get_binding());
+ render.encoder->setFragmentBytes(push_constant_data, push_constant_data_len, slot);
+ render.arg_buffer_cache[RenderState::ARG_BUFFER_STAGE_FRAGMENT].invalidate_buffer(slot);
}
+ binding_cache.invalidate_buffer(slot);
}
const MDSubpass &subpass = render.get_subpass();
@@ -1058,6 +1060,7 @@ void MDCommandBuffer::_render_set_dirty_state() {
uint32_t view_range[2] = { 0, subpass.view_count };
render.encoder->setVertexBytes(view_range, sizeof(view_range), VIEW_MASK_BUFFER_INDEX);
render.encoder->setFragmentBytes(view_range, sizeof(view_range), VIEW_MASK_BUFFER_INDEX);
+ binding_cache.invalidate_buffer(VIEW_MASK_BUFFER_INDEX);
}
if (render.dirty.has_flag(RenderState::DIRTY_PIPELINE)) {
@@ -1630,7 +1633,9 @@ void MDCommandBuffer::_compute_set_dirty_state() {
if (compute.dirty.has_flag(ComputeState::DIRTY_PUSH)) {
if (push_constant_binding.has(RDD::SHADER_STAGE_COMPUTE)) {
- compute.encoder->setBytes(push_constant_data, push_constant_data_len, push_constant_binding.get_binding());
+ uint32_t slot = push_constant_binding.get_binding();
+ compute.encoder->setBytes(push_constant_data, push_constant_data_len, slot);
+ binding_cache.invalidate_buffer(slot);
}
}
diff --git a/drivers/metal/metal3_objects.h b/drivers/metal/metal3_objects.h
index d3e4b6b4c55..cf978baa9c1 100644
--- a/drivers/metal/metal3_objects.h
+++ b/drivers/metal/metal3_objects.h
@@ -140,14 +140,16 @@ struct API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) ResourceTracker {
struct BindingCache {
struct BufferBinding {
- MTL::Buffer *buffer = nullptr;
- NS::UInteger offset = 0;
+ MTL::Buffer *buffer;
+ NS::UInteger offset;
bool operator!=(const BufferBinding &p_other) const {
return buffer != p_other.buffer || offset != p_other.offset;
}
};
+ static_assert(std::is_trivially_constructible_v<BufferBinding>);
+
LocalVector<MTL::Texture *> textures;
LocalVector<MTL::SamplerState *> samplers;
LocalVector<BufferBinding> buffers;
@@ -159,11 +161,14 @@ struct BindingCache {
}
private:
+ // Returns true if p_vec was resized, and therefore new elements were initialized.
template <typename T>
- _FORCE_INLINE_ void ensure_size(LocalVector<T> &p_vec, uint32_t p_required) {
+ _FORCE_INLINE_ bool ensure_size(LocalVector<T> &p_vec, uint32_t p_required) {
if (p_vec.size() < p_required) {
p_vec.resize_initialized(p_required);
+ return true;
}
+ return false;
}
public:
@@ -240,6 +245,15 @@ public:
}
return false;
}
+
+ _FORCE_INLINE_ void invalidate_buffer(uint32_t p_index) {
+ if (!ensure_size(buffers, p_index + 1)) {
+ buffers.ptr()[p_index] = {
+ .buffer = nullptr,
+ .offset = 0,
+ };
+ }
+ }
};
// A type used to encode resources directly to a MTLCommandEncoder