Authored by Apple: Michael Parkin-White
Ref T96261
Differential D16721
Metal: Remove Vec3 packing from uniform buffer generation as this causes UBO misalignment in Metal. Authored by Jason Fielder (jason_apple) on Dec 8 2022, 12:22 PM.
Details
Authored by Apple: Michael Parkin-White Ref T96261
Diff Detail
Event TimelineComment Actions As far as I can see, the issue is that you cannot pack float after float3 because of the explicit float3 alignment to 16 bytes. I suggest this much simpler solution: diff --git a/source/blender/gpu/intern/gpu_uniform_buffer.cc b/source/blender/gpu/intern/gpu_uniform_buffer.cc index 5d9e2af631e..96b085dee2d 100644 --- a/source/blender/gpu/intern/gpu_uniform_buffer.cc +++ b/source/blender/gpu/intern/gpu_uniform_buffer.cc @@ -56,6 +56,10 @@ static eGPUType get_padded_gpu_type(LinkData *link) { GPUInput *input = (GPUInput *)link->data; eGPUType gputype = input->type; + /* Metal cannot pack floats after vec3. */ + if (GPU_backend_get_type() == GPU_BACKEND_METAL) { + return (gputype == GPU_VEC3) ? GPU_VEC4 : gputype; + } /* Unless the vec3 is followed by a float we need to treat it as a vec4. */ if (gputype == GPU_VEC3 && (link->next != nullptr) && (((GPUInput *)link->next->data)->type != GPU_FLOAT)) { @@ -89,6 +93,11 @@ static void buffer_from_list_inputs_sort(ListBase *inputs) /* Order them as mat4, vec4, vec3, vec2, float. */ BLI_listbase_sort(inputs, inputs_cmp); + /* Metal cannot pack floats after vec3. */ + if (GPU_backend_get_type() == GPU_BACKEND_METAL) { + return; + } + /* Creates a lookup table for the different types; */ LinkData *inputs_lookup[MAX_UBO_GPU_TYPE + 1] = {nullptr}; eGPUType cur_type = static_cast<eGPUType>(MAX_UBO_GPU_TYPE + 1); Also this patch contains a lot of unrelated modifications. Comment Actions Thanks for the feedback! Tested these changes locally and lightweight fix does address the issue at hand. I can't recall if there were any other cases where types had incompatible alignment, though this appears to be working correctly, so will update patch. I will also strip out the other changes and move them into a separate patch for clarity. Comment Actions
Comment Actions Seems like the diff got broken. I will commit only the changes in the gpu_uniform_buffer.cc. |