From d5a9d624d4b97d94ec0e477c18ce771c6d9a5c9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hernan=20Flo=CC=81rez=20Burgos?= <118863251+partprogramming09@users.noreply.github.com> Date: Sat, 5 Dec 2026 02:25:28 -0500 Subject: [PATCH 1/2] d3d12: occlusion queries keep every draw's count on Apple7 when a pass has many large draws On an M1 Pro (Apple7), a render pass with many counting draws over a large target loses the counts of its first draws: they come back as 0. A plain Metal program shows it too, so it is outside Mullion. Render passes now add their counts to the visibility buffer (MTLVisibilityResultTypeAccumulate, macOS 26 and later) instead of storing them, so a count stored again as 0 is kept. Adding needs each slot to start at 0: VisibilitySlot zeroes a new window, and Close zeroes the slots the list counted in with blit fills after the sums and resolves read them. Each slot is still counted in by one pass only, so adding gives the same number as storing did. Older macOS stores as before; D3D11 is untouched. --- src/d3d12/d3d12_command_allocator.hpp | 3 +++ src/d3d12/d3d12_command_list.cpp | 34 +++++++++++++++++++++++++-- src/d3d12/d3d12_command_queue.cpp | 2 ++ src/winemetal/unix/winemetal_unix.c | 7 ++++++ src/winemetal/winemetal.h | 3 +++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/d3d12/d3d12_command_allocator.hpp b/src/d3d12/d3d12_command_allocator.hpp index 7e933ccdc..2774956f4 100644 --- a/src/d3d12/d3d12_command_allocator.hpp +++ b/src/d3d12/d3d12_command_allocator.hpp @@ -634,6 +634,9 @@ class MTLD3D12CommandAllocatorImpl : public MTLD3D12PageableGetMTLDevice().newBuffer(info), info.gpu_address, info.memory.get()}); + // the passes add to the slots (visibility_accumulate) + if (visibility_.back().counts) + memset(visibility_.back().counts, 0, kVisibilityWindow); } exhausted_ |= !visibility_.back(); visibility_address_ = visibility_.back().address; diff --git a/src/d3d12/d3d12_command_list.cpp b/src/d3d12/d3d12_command_list.cpp index 919ee0167..8e1632f59 100644 --- a/src/d3d12/d3d12_command_list.cpp +++ b/src/d3d12/d3d12_command_list.cpp @@ -459,6 +459,13 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChild active_queries_; std::vector visibility_slots_; + // the slots the list counted in, as runs of a window's buffer: the passes add to a slot (visibility_accumulate), + // so the list leaves them at 0 for when it runs again + struct VisibilityRun { + obj_handle_t buffer; + uint64_t begin, end; + }; + std::vector visibility_runs_; // whether a draw counted in the last slot bool visibility_drawn_ = false; std::vector> occlusion_sums_; @@ -580,8 +587,8 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChildencoder_current && allocator_->encoder_current->type == EncoderType::Render @@ -601,6 +608,10 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChildvisibility_buffer && render->visibility_buffer != window.handle) return allocator_->InvalidateCurrentPass(); visibility_slots_.push_back(address); + if (visibility_runs_.empty() || visibility_runs_.back().buffer != window.handle || + visibility_runs_.back().end != offset) + visibility_runs_.push_back({window.handle, offset, offset}); + visibility_runs_.back().end = offset + sizeof(uint64_t); render->visibility_buffer = window; cmd.mode = WMTVisibilityResultModeCounting; cmd.offset = offset; @@ -757,6 +768,23 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChildInvalidateCurrentPass(); } + // the slots back to 0 after the sums have read them + void + ZeroVisibility() { + if (visibility_runs_.empty()) + return; + PreBlit(); + for (auto &run : visibility_runs_) { + auto &fill = allocator_->EncodeBlitCommand(); + fill.type = WMTBlitCommandFillBuffer; + fill.buffer = run.buffer; + fill.offset = run.begin; + fill.length = run.end - run.begin; + fill.value = 0; + } + allocator_->InvalidateCurrentPass(); + } + // the ended pipeline statistics queries' results, once the passes they counted in are over void SumStatistics() { @@ -919,6 +947,7 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChildencoder_current ? allocator_->encoder_current : allocator_->encoder_last; diff --git a/src/d3d12/d3d12_command_queue.cpp b/src/d3d12/d3d12_command_queue.cpp index ae59eff0a..5fda0eb16 100644 --- a/src/d3d12/d3d12_command_queue.cpp +++ b/src/d3d12/d3d12_command_queue.cpp @@ -2094,6 +2094,8 @@ class MTLD3D12CommandQueueImpl : public MTLD3D12Pageablerender_target_width; render_pass_info.render_target_height = data->render_target_height; render_pass_info.visibility_buffer = data->visibility_buffer; + // a slot is zero until the one pass that counts in it does (VisibilitySlot) + render_pass_info.visibility_accumulate = true; } // its work before it, and that of the passes that go on in it auto befores = [&](auto &&each) { diff --git a/src/winemetal/unix/winemetal_unix.c b/src/winemetal/unix/winemetal_unix.c index 7a5135ec8..25f78c020 100644 --- a/src/winemetal/unix/winemetal_unix.c +++ b/src/winemetal/unix/winemetal_unix.c @@ -643,6 +643,11 @@ renderCommandEncoder(void *obj, const struct WMTSampleBufferAttachmentInfo *samp descriptor.renderTargetHeight = info->render_target_height; descriptor.renderTargetWidth = info->render_target_width; descriptor.visibilityResultBuffer = (id)info->visibility_buffer; + // before macOS 26 a pass stores its counts, and a GPU that starts the pass over loses those before + if (info->visibility_accumulate) { + if (@available(macOS 26.0, *)) + descriptor.visibilityResultType = MTLVisibilityResultTypeAccumulate; + } sample_positions(descriptor, info); @@ -6436,6 +6441,8 @@ _MTL4CommandBuffer_encoder(void *obj) { descriptor.renderTargetHeight = info->render_target_height; descriptor.renderTargetWidth = info->render_target_width; descriptor.visibilityResultBuffer = (id)info->visibility_buffer; + if (info->visibility_accumulate) + descriptor.visibilityResultType = MTLVisibilityResultTypeAccumulate; sample_positions(descriptor, info); if (info->tile_height && info->tile_width) { diff --git a/src/winemetal/winemetal.h b/src/winemetal/winemetal.h index 55a0b3a7a..cad4ea695 100644 --- a/src/winemetal/winemetal.h +++ b/src/winemetal/winemetal.h @@ -741,6 +741,9 @@ struct WMTRenderPassInfo { uint32_t render_target_height; uint32_t render_target_width; obj_handle_t visibility_buffer; + // the pass adds its counts to what visibility_buffer holds instead of storing them: an Apple7 GPU that starts a pass + // over partway (many draws over a large target) stores the counts of the segments before that point as 0 otherwise + uint8_t visibility_accumulate; }; WINEMETAL_API obj_handle_t MTLCommandBuffer_renderCommandEncoder(obj_handle_t cmdbuf, struct WMTRenderPassInfo *info); From c614807d28332dc0332f63a51a14bc77e8f0e1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hernan=20Flo=CC=81rez=20Burgos?= <118863251+partprogramming09@users.noreply.github.com> Date: Sat, 5 Dec 2026 03:37:38 -0500 Subject: [PATCH 2/2] d3d11: occlusion queries keep every draw's count on Apple7 when a pass has many large draws D3D11 counts occlusion like D3D12 and loses counts the same way on an M1 Pro (Apple7): in d3d11_occlusion_wide the 100 triangles over 8192x8192 count about 8% of their samples. Render passes now add their counts to the visibility buffer (macOS 26 and later), as D3D12 passes do. A VisibilityResultReadback window is made for one command buffer and never reused; it is now zeroed when created, since on i386 its memory comes from aligned_malloc. Each segment is still counted in by one pass only, so adding gives the same number as storing did. --- src/mullion/mullion_context.cpp | 3 +++ src/mullion/mullion_query.hpp | 3 +++ tests/d3d11/d3d11_occlusion_wide.cpp | 2 ++ 3 files changed, 8 insertions(+) diff --git a/src/mullion/mullion_context.cpp b/src/mullion/mullion_context.cpp index db6cfea16..c7806bae3 100644 --- a/src/mullion/mullion_context.cpp +++ b/src/mullion/mullion_context.cpp @@ -1290,6 +1290,9 @@ ArgumentEncodingContext::flushCommands(WMT::CommandBuffer cmdbuf, uint64_t seqId assert(readbacks.visibility); render_pass_info.visibility_buffer = readbacks.visibility->visibility_result_windows[data->visibility_result_window]; + // Apple7 loses counts a large pass stores; adding keeps them, and a window starts at 0 and has each of its + // segments counted in by one pass (VisibilityResultReadback) + render_pass_info.visibility_accumulate = true; } auto gpu_buffer_ = data->allocated_argbuf; auto encoder = samples.sample_buffer ? cmdbuf.renderCommandEncoder(render_pass_info, samples) diff --git a/src/mullion/mullion_query.hpp b/src/mullion/mullion_query.hpp index 10553ffdb..24749898c 100644 --- a/src/mullion/mullion_query.hpp +++ b/src/mullion/mullion_query.hpp @@ -193,6 +193,9 @@ class VisibilityResultReadback { info.memory.set(wsi::aligned_malloc(info.length, MULLION_PAGE_SIZE)); #endif visibility_result_windows.emplace_back(device.newBuffer(info)); + // the passes add to the window (visibility_accumulate); aligned_malloc does not zero it + if (info.memory.get()) + memset(info.memory.get(), 0, info.length); } } ~VisibilityResultReadback() { diff --git a/tests/d3d11/d3d11_occlusion_wide.cpp b/tests/d3d11/d3d11_occlusion_wide.cpp index 0e8b5fefc..5670275fc 100644 --- a/tests/d3d11/d3d11_occlusion_wide.cpp +++ b/tests/d3d11/d3d11_occlusion_wide.cpp @@ -6,6 +6,8 @@ // test_occlusion_query caught (it wants 0x190000000, or 0xffffffff from a 32-bit counter). a pass stores the counts of // its first 4096 segments, adds later ones to what the buffer holds and counts nothing from segment 16383 on // (tests/native/visibility.swift): the many-draws query counts more draws than that in one pass of a small target. +// on an M1 Pro (Apple7) a pass of many full-target draws over a large target loses part of the counts it stores: the +// 100 triangles over 8192x8192 count about 8% of their samples unless the pass adds them (visibility_accumulate). // each query runs on the immediate context and in a deferred context's list #include "d3d11_test.hpp"