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
3 changes: 3 additions & 0 deletions src/d3d12/d3d12_command_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ class MTLD3D12CommandAllocatorImpl : public MTLD3D12Pageable<MTLD3D12CommandAllo
info.length = kVisibilityWindow;
info.options = WMTResourceHazardTrackingModeUntracked;
visibility_.push_back({device_->GetMTLDevice().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;
Expand Down
34 changes: 32 additions & 2 deletions src/d3d12/d3d12_command_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,13 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChild<MTLD3D12Graph
};
std::vector<ActiveQuery> active_queries_;
std::vector<uint64_t> 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<VisibilityRun> visibility_runs_;
// whether a draw counted in the last slot
bool visibility_drawn_ = false;
std::vector<std::pair<MTLD3D12QueryHeap *, OcclusionSum>> occlusion_sums_;
Expand Down Expand Up @@ -580,8 +587,8 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChild<MTLD3D12Graph
}
}

// a render pass counts samples in a new slot whenever the running queries change: Metal accumulates a slot within
// a pass, and starts it over in the next
// a render pass counts samples in a new slot whenever the running queries change: each slot is counted in by one
// pass only, which adds to it (visibility_accumulate), and the list zeroes its slots again at Close
void
CountVisibility() {
auto render = allocator_->encoder_current && allocator_->encoder_current->type == EncoderType::Render
Expand All @@ -601,6 +608,10 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChild<MTLD3D12Graph
if (render->visibility_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;
Expand Down Expand Up @@ -757,6 +768,23 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChild<MTLD3D12Graph
allocator_->InvalidateCurrentPass();
}

// 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<wmtcmd_blit_fillbuffer>();
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() {
Expand Down Expand Up @@ -919,6 +947,7 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChild<MTLD3D12Graph
SetPipelineState(pInitialPipelineState);
active_queries_.clear();
visibility_slots_.clear();
visibility_runs_.clear();
occlusion_sums_.clear();
active_statistics_.clear();
statistics_sums_.clear();
Expand Down Expand Up @@ -979,6 +1008,7 @@ class MTLD3D12GraphicsCommandListImpl : public MTLD3D12DeviceChild<MTLD3D12Graph
SumOcclusion();
SumStatistics();
EndPredication();
ZeroVisibility();
// the next list of the call follows this one's last pass as a pass of this list would (Overlap), or a pass that
// encodes nothing orders it after all before
auto last = allocator_->encoder_current ? allocator_->encoder_current : allocator_->encoder_last;
Expand Down
2 changes: 2 additions & 0 deletions src/d3d12/d3d12_command_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,8 @@ class MTLD3D12CommandQueueImpl : public MTLD3D12Pageable<MTLD3D12CommandQueue, I
render_pass_info.render_target_width = data->render_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) {
Expand Down
3 changes: 3 additions & 0 deletions src/mullion/mullion_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/mullion/mullion_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions src/winemetal/unix/winemetal_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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<MTLBuffer>)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);

Expand Down Expand Up @@ -6436,6 +6441,8 @@ _MTL4CommandBuffer_encoder(void *obj) {
descriptor.renderTargetHeight = info->render_target_height;
descriptor.renderTargetWidth = info->render_target_width;
descriptor.visibilityResultBuffer = (id<MTLBuffer>)info->visibility_buffer;
if (info->visibility_accumulate)
descriptor.visibilityResultType = MTLVisibilityResultTypeAccumulate;
sample_positions(descriptor, info);

if (info->tile_height && info->tile_width) {
Expand Down
3 changes: 3 additions & 0 deletions src/winemetal/winemetal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/d3d11/d3d11_occlusion_wide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Loading