feat(render): Campaign V slice V6j commit 2 - Dereth draws on Vulkan
The three world renderers' submission arms, both pass executors, and the
composition that reaches them. This is the unit three predecessors stopped at.
What it produces. ACDREAM_RENDER_BACKEND=vulkan on the offline scene renders
terrain with blended textures and road overlays, the water edge, static world
meshes, procedural scenery, and the complete retained UI - the same frame the GL
pixel gate captures, from the same camera, minus the sky. artifacts/v6j-vk2.
The shape, and why it is not V4c's. Section 5.5.6 chose option (B) after NVIDIA
rendered the V4c binary 10/10 where AMD's GL stack did not: GL keeps its raw
world path through to V10 as a documented fork confined to the submission seam,
and the RHI world path ships on Vulkan. So V4c's and V4d-2's content returns as a
SECOND arm rather than a replacement. The GL arm issues the same GL statements in
the same order against the same objects; the encoder arm lives in three .Rhi.cs
partials and is entered by one branch per submission site.
Three differences from V4c, each because the tree moved under it. There is no
binding-9 texture table - V4t put the slot on the device and Vulkan binds set 2,
so the arm that used to intern bindless handles simply has nothing to do. The
pipelines carry the device's sample count rather than 1, because Vulkan requires
rasterizationSamples to match the pass and alpha-to-coverage is a no-op at one
sample. And no renderer opens a pass.
That last one is structural, not tidiness. Under MSAA the frame's one backbuffer
pass resolves into the swapchain image and stores DONT_CARE into the multisampled
scratch, so a second pass declaring Load would load undefined contents; the
backend also permits one open pass per frame. VulkanWorldScenePhase therefore
opens the pass, publishes the encoder on VulkanWorldPassScope for exactly the
span of the inner WorldSceneRenderer, and every renderer borrows it.
Three sections are frame-global on GL and cannot be on Vulkan: the SceneLighting
UBO, the per-cell clip regions, and the terrain clip block. GL binds each to a
global binding point and every consumer inherits it. Vulkan binds a descriptor
set per draw, and a renderer's own binds are what select the scope those sections
must land in - so their writers PUBLISH into WorldFrameSections and each renderer
binds them inside the pass, after its own binds. SceneLightingUboBinding's
per-flight-slot buffer pool disappears with it: a ring allocation is already
distinct memory that lives until the frame retires, which is the property the
pool existed to provide.
Both pass executors became backend-neutral rather than gaining twins. Everything
they do is delegation to a renderer except four concerns - the clip-frame
publication, the doorway scissor, gl_ClipDistance enablement, and retail's
interior depth clear - so those four move behind IWorldPassSurface and retail's
ordering, which is what these classes are actually for, is written once. The GL
implementation issues the statements the executors used to issue inline.
Clip distances are no-ops on the Vulkan arm, and that is safe rather than a
divergence: Vulkan activates every element the shader declares, and all three
world vertex shaders already write 1.0 into every slot past the active count.
The interior depth clear becomes vkCmdClearAttachments, reached through the scope
so the pinned contract stays frozen and the backend-only verb stays in the
backend. The hook for it was already committed at V6i-3 with a cref to a type
that did not exist yet; it exists now.
The collision-wireframe DebugLineRenderer is composed as null on the Vulkan arm.
DrawAndPublish flushes it INSIDE the world phase and it opens its own pass, which
the one-pass rule forbids. The toggle is DevTools-only and DevTools is not
composed there, so nothing is lost - composing it would throw on the first
wireframe frame rather than silently misdraw.
Two seams widened rather than invented. GameWindowGraphics answers whether the
backend has a world-pass seam, because the three composition phases that need it
already borrow that handle and "does this backend work that way" is what the type
exists to answer. And MeshSourceReady replaces the anyVao != 0 gate with the same
question in backend-neutral form - V6i-3 published HasStores for exactly this -
so the predicate evaluates identically on GL.
What is NOT here, and is expected. Sky and weather are still raw GL (V4f), so the
Vulkan frame's sky is the atmosphere fog clear. Particles (V4e), the paperdoll and
appraisal viewports and the portal depth mask (V4g) likewise. The executors
already accepted all of them as absent.
Gates. Release build green. App tests 4,112 passed / 3 skipped, the unchanged
baseline; complete Release suite 9,175 / 5. Strict GL offline pixel gate against
847f14ae: 5.50e-05, 31 differing pixels of 563,200, inside the documented 9-31
band and 18x under the threshold. Characterised rather than accepted, because 31
is the band's top: cross-commit pairs measured 21, 29 and 31 while same-commit
controls measured 12 and 20, and maximumChannelDelta is 46-52 in every comparison
INCLUDING the pure controls - so the few large-delta pixels are a property of the
capture, and a cross-commit pair at 21 against a same-commit pair at 20 is not
what a systematic shift looks like. GL connected repeat gate at 3 runs: 3/3
RENDERED on the desktop witness and 3/3 on the client capture. One offline Vulkan
run with VK_LAYER_KHRONOS_validation proven inserted by the loader: zero
validation errors, zero warnings, a captured world frame, and a graceful close.
Coverage gap, stated rather than assumed. The offline scene is a fixed outdoor
view, so EnvCellRenderer's Vulkan arm draws nothing in it - dungeon interiors are
half of this slice and are unproven by anything automated, exactly as they were
for V4c. The deferred-alpha path and the doorway scissor are likewise untouched
by this scene. They join the accumulated user-gate debt in plan section 5.1.
No divergence-register row: no retail-facing behaviour changes.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
81fe5e1b63
commit
f84eef3256
22 changed files with 2566 additions and 264 deletions
326
src/AcDream.App/Rendering/Wb/EnvCellRenderer.Rhi.cs
Normal file
326
src/AcDream.App/Rendering/Wb/EnvCellRenderer.Rhi.cs
Normal file
|
|
@ -0,0 +1,326 @@
|
|||
using System.Numerics;
|
||||
using System.Runtime.InteropServices;
|
||||
using AcDream.App.Rendering.Gpu;
|
||||
using AcDream.Core.Lighting;
|
||||
using DatReaderWriter.Enums;
|
||||
|
||||
namespace AcDream.App.Rendering.Wb;
|
||||
|
||||
/// <summary>
|
||||
/// Campaign V slice V6j: the dungeon-shell renderer's RHI submission arm.
|
||||
///
|
||||
/// <para>V4c's content, re-landed as a second arm rather than a replacement —
|
||||
/// see <see cref="AcDream.App.Rendering.TerrainModernRenderer"/>'s RHI file for
|
||||
/// why the fork exists and where it is confined.</para>
|
||||
///
|
||||
/// <para>Two structural differences from V4c. It records into the pass
|
||||
/// <c>VulkanWorldScenePhase</c> opened rather than opening
|
||||
/// <c>"envcell-shells"</c> of its own, because the frame's one backbuffer pass
|
||||
/// resolves. And there is no binding-9 texture table: V4t moved the slot onto
|
||||
/// the device, and on Vulkan that table is set 2, which the encoder binds.</para>
|
||||
/// </summary>
|
||||
public sealed unsafe partial class EnvCellRenderer
|
||||
{
|
||||
private readonly IGpuDevice? _device;
|
||||
private readonly ICurrentGpuFrameSource? _frames;
|
||||
private readonly IWorldPassScope? _scope;
|
||||
private IGpuPipeline? _opaquePipeline;
|
||||
private IGpuPipeline? _alphaPipeline;
|
||||
private IGpuPipeline? _additivePipeline;
|
||||
|
||||
/// <summary>
|
||||
/// The RHI arm's constructor. It also completes <c>Initialize</c>'s job: the
|
||||
/// three pipelines ARE this renderer's program, so there is no second step
|
||||
/// and no <c>Shader</c> to hand in.
|
||||
/// </summary>
|
||||
internal EnvCellRenderer(
|
||||
IGpuDevice device,
|
||||
ICurrentGpuFrameSource frames,
|
||||
IWorldPassScope scope,
|
||||
ObjectMeshManager meshManager,
|
||||
WbFrustum frustum)
|
||||
{
|
||||
_device = device ?? throw new ArgumentNullException(nameof(device));
|
||||
_frames = frames ?? throw new ArgumentNullException(nameof(frames));
|
||||
_scope = scope ?? throw new ArgumentNullException(nameof(scope));
|
||||
_meshManager = meshManager ?? throw new ArgumentNullException(nameof(meshManager));
|
||||
_frustum = frustum ?? throw new ArgumentNullException(nameof(frustum));
|
||||
|
||||
_opaquePipeline = CreateShellPipeline(
|
||||
device, "envcell-opaque", GpuBlendMode.None, depthWrite: true, scope.SampleCount);
|
||||
_alphaPipeline = CreateShellPipeline(
|
||||
device, "envcell-alpha", GpuBlendMode.StraightAlpha, depthWrite: false, scope.SampleCount);
|
||||
_additivePipeline = CreateShellPipeline(
|
||||
device, "envcell-additive", GpuBlendMode.Additive, depthWrite: false, scope.SampleCount);
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One pipeline per blend state the shell pass uses. Everything else is
|
||||
/// shared: <c>mesh_modern</c>, the 32-byte world-mesh vertex, triangle lists,
|
||||
/// back-face culling with clockwise front faces.
|
||||
///
|
||||
/// <para>Depth compare is <c>Less</c>, not the contract's <c>LessOrEqual</c>
|
||||
/// default. The world frame runs under <c>GL_LESS</c> and this renderer never
|
||||
/// called <c>glDepthFunc</c>, so it inherited it; baking <c>LessOrEqual</c>
|
||||
/// would change which of two coplanar retail surfaces wins.</para>
|
||||
/// </summary>
|
||||
private static IGpuPipeline CreateShellPipeline(
|
||||
IGpuDevice device,
|
||||
string name,
|
||||
GpuBlendMode blend,
|
||||
bool depthWrite,
|
||||
int sampleCount) =>
|
||||
device.CreatePipeline(new GpuPipelineDescription
|
||||
{
|
||||
Name = name,
|
||||
Shaders = new GpuShaderSet("mesh_modern"),
|
||||
VertexLayout = GpuVertexLayout.WorldMesh,
|
||||
Topology = GpuPrimitiveTopology.TriangleList,
|
||||
Blend = blend,
|
||||
Depth = new GpuDepthState(Test: true, Write: depthWrite, GpuCompareOp.Less),
|
||||
Cull = GpuCullMode.Back,
|
||||
FrontFace = GpuFrontFace.Clockwise,
|
||||
AlphaToCoverage = false,
|
||||
ColorWrite = true,
|
||||
SampleCount = sampleCount,
|
||||
});
|
||||
|
||||
/// <summary>
|
||||
/// Writes this pass's sections into the frame ring and records the same
|
||||
/// per-group multi-draw runs the GL arm issues, in the same order.
|
||||
/// </summary>
|
||||
private void SubmitRhi(
|
||||
List<InstanceData> allInstances,
|
||||
WbRenderPass renderPass,
|
||||
int totalDraws,
|
||||
int uniqueInstanceCount)
|
||||
{
|
||||
IWorldPassScope scope = _scope!;
|
||||
IGpuPassEncoder encoder = scope.RequireEncoder();
|
||||
IGpuFrame frame = _frames!.CurrentFrame
|
||||
?? throw new InvalidOperationException(
|
||||
"EnvCellRenderer requires an open IGpuFrame (see GpuDeviceFrameLifetime).");
|
||||
GlobalMeshBuffer mesh = _meshManager.GlobalBuffer
|
||||
?? throw new InvalidOperationException("The shared mesh arena is not published.");
|
||||
|
||||
if (_gpuInstanceTransforms.Length < uniqueInstanceCount)
|
||||
{
|
||||
Array.Resize(
|
||||
ref _gpuInstanceTransforms,
|
||||
Math.Max(_gpuInstanceTransforms.Length * 2, uniqueInstanceCount));
|
||||
}
|
||||
for (int i = 0; i < uniqueInstanceCount; i++)
|
||||
_gpuInstanceTransforms[i] = allInstances[i].Transform;
|
||||
|
||||
// Phase U.4: per-instance clip slots, laid out parallel to the transforms
|
||||
// so instanceClipSlot[BaseInstance + gl_InstanceID] tracks Instances[].
|
||||
if (_clipSlotData.Length < uniqueInstanceCount)
|
||||
_clipSlotData = new uint[Math.Max(_clipSlotData.Length * 2, uniqueInstanceCount)];
|
||||
if (_cellIdToSlot is null
|
||||
|| AcDream.Core.Rendering.RenderingDiagnostics.ClipDebugNoShellTrim)
|
||||
{
|
||||
Array.Clear(_clipSlotData, 0, uniqueInstanceCount);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < uniqueInstanceCount; i++)
|
||||
{
|
||||
_clipSlotData[i] =
|
||||
_cellIdToSlot.TryGetValue(allInstances[i].CellId, out int slot)
|
||||
? (uint)slot
|
||||
: 0u;
|
||||
}
|
||||
}
|
||||
|
||||
// A7 Fix D (D-2): per-instance 8-int light set, keyed on the cell each
|
||||
// shell instance belongs to.
|
||||
int lightStride = LightManager.MaxLightsPerObject;
|
||||
if (_lightSetData.Length < uniqueInstanceCount * lightStride)
|
||||
{
|
||||
_lightSetData = new int[Math.Max(
|
||||
_lightSetData.Length * 2,
|
||||
uniqueInstanceCount * lightStride)];
|
||||
}
|
||||
for (int i = 0; i < uniqueInstanceCount; i++)
|
||||
{
|
||||
int[] cellSet = GetCellLightSet(allInstances[i].CellId);
|
||||
Array.Copy(cellSet, 0, _lightSetData, i * lightStride, lightStride);
|
||||
}
|
||||
|
||||
if (renderPass == WbRenderPass.Opaque
|
||||
&& AcDream.Core.Rendering.RenderingDiagnostics.ProbeSeamDrawEnabled)
|
||||
{
|
||||
EmitSeamDrawProbe(_renderDrawCalls, allInstances, _seamProbeFilter);
|
||||
}
|
||||
|
||||
int lightCount = GlobalLightPacker.Pack(_pointSnapshot, ref _globalLightData);
|
||||
int globalLightUploadCount = lightCount > 0 ? lightCount : 1;
|
||||
|
||||
var pushConstants = new GpuPushConstants
|
||||
{
|
||||
ViewProjection = _lastViewProjection,
|
||||
DrawIdOffset = 0,
|
||||
// A7 Fix D D-3/D-4: EnvCell bake — wrap points, no sun.
|
||||
LightingMode = 1,
|
||||
RenderPass = (int)renderPass,
|
||||
LightDebug = AcDream.Core.Rendering.RenderingDiagnostics.LightDebugMode,
|
||||
TextureIndexA = 0,
|
||||
TextureIndexB = 0,
|
||||
ParamA = 0f,
|
||||
ParamB = 0f,
|
||||
};
|
||||
|
||||
// Bind the pass's base pipeline first so the ring binds land on a live
|
||||
// program; the per-range switches below rebind the mesh with it.
|
||||
IGpuPipeline basePipeline = renderPass == WbRenderPass.Transparent
|
||||
? _alphaPipeline!
|
||||
: _opaquePipeline!;
|
||||
BindPipelineWithMesh(encoder, basePipeline, mesh);
|
||||
encoder.SetPushConstants(in pushConstants);
|
||||
|
||||
BindRingSection<Matrix4x4>(
|
||||
encoder, frame, GpuBindingModel.StorageInstances,
|
||||
_gpuInstanceTransforms.AsSpan(0, uniqueInstanceCount));
|
||||
BindRingSection<ModernBatchData>(
|
||||
encoder, frame, GpuBindingModel.StorageBatches,
|
||||
_modernBatches.AsSpan(0, totalDraws));
|
||||
BindRingSection<uint>(
|
||||
encoder, frame, GpuBindingModel.StorageClipSlots,
|
||||
_clipSlotData.AsSpan(0, uniqueInstanceCount));
|
||||
BindRingSection<float>(
|
||||
encoder, frame, GpuBindingModel.StorageGlobalLights,
|
||||
_globalLightData.AsSpan(
|
||||
0,
|
||||
globalLightUploadCount * GlobalLightPacker.FloatsPerLight));
|
||||
BindRingSection<int>(
|
||||
encoder, frame, GpuBindingModel.StorageInstanceLightSets,
|
||||
_lightSetData.AsSpan(0, uniqueInstanceCount * lightStride));
|
||||
|
||||
// The frame-global sections, bound after this renderer's own binds
|
||||
// because those binds are what select the descriptor scope.
|
||||
AcDream.App.Rendering.WorldFrameSectionBinding.BindClipRegions(
|
||||
encoder, scope.Sections, frame);
|
||||
AcDream.App.Rendering.WorldFrameSectionBinding.BindSceneLighting(
|
||||
encoder, scope.Sections, frame);
|
||||
|
||||
GpuRingAllocation commands = frame.AllocateRing(
|
||||
totalDraws * sizeof(DrawElementsIndirectCommand),
|
||||
GpuRingUsage.Indirect);
|
||||
MemoryMarshal.AsBytes(_commands.AsSpan(0, totalDraws)).CopyTo(commands.Data);
|
||||
IGpuBuffer commandBuffer = commands.Buffer;
|
||||
uint commandBase = commands.OffsetBytes;
|
||||
|
||||
for (int drawRangeIndex = 0; drawRangeIndex < _mdiDrawRanges.Count; drawRangeIndex++)
|
||||
{
|
||||
MdiDrawRange drawRange = _mdiDrawRanges[drawRangeIndex];
|
||||
int groupIndex = drawRange.GroupIndex;
|
||||
var cullMode = (CullMode)(groupIndex % 4);
|
||||
// Phase A8 visual-gate evidence: cell meshes use CullMode.Landblock
|
||||
// uniformly, but the room surfaces need to be visible from inside.
|
||||
// Render cell polys double-sided, exactly as the GL arm does.
|
||||
if (cullMode == CullMode.Landblock) cullMode = CullMode.None;
|
||||
|
||||
bool isAdditive = groupIndex >= 4;
|
||||
if (renderPass == WbRenderPass.Transparent)
|
||||
{
|
||||
// Blend state is the pipeline's; switching variants mid-pass has
|
||||
// to re-establish the mesh, which is vertex-array state.
|
||||
BindPipelineWithMesh(
|
||||
encoder,
|
||||
isAdditive ? _additivePipeline! : _alphaPipeline!,
|
||||
mesh);
|
||||
}
|
||||
|
||||
// Must follow the pipeline bind: BindPipeline re-issues the
|
||||
// pipeline's own cull/front-face/depth-write defaults.
|
||||
SetCullMode(encoder, cullMode);
|
||||
|
||||
pushConstants.RenderPass = isAdditive
|
||||
? (int)renderPass | 0x100
|
||||
: (int)renderPass;
|
||||
pushConstants.DrawIdOffset = drawRange.FirstCommand;
|
||||
encoder.SetPushConstants(in pushConstants);
|
||||
encoder.MultiDrawIndexedIndirect(
|
||||
commandBuffer,
|
||||
commandBase + (uint)(drawRange.FirstCommand * sizeof(DrawElementsIndirectCommand)),
|
||||
(uint)drawRange.CommandCount,
|
||||
(uint)sizeof(DrawElementsIndirectCommand));
|
||||
}
|
||||
}
|
||||
|
||||
private void BindPipelineWithMesh(
|
||||
IGpuPassEncoder encoder,
|
||||
IGpuPipeline pipeline,
|
||||
GlobalMeshBuffer mesh)
|
||||
{
|
||||
encoder.BindPipeline(pipeline);
|
||||
encoder.BindVertexBuffer(
|
||||
mesh.VertexStore ?? throw new InvalidOperationException(
|
||||
"The shared mesh arena has no vertex store."),
|
||||
0);
|
||||
encoder.BindIndexBuffer(
|
||||
mesh.IndexStore ?? throw new InvalidOperationException(
|
||||
"The shared mesh arena has no index store."),
|
||||
0,
|
||||
GpuIndexType.UInt16);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WB <c>BaseObjectRenderManager.cs:850-866</c> applies CullMode per MDI
|
||||
/// group; WB <c>GameScene.cs:843</c> sets FrontFace(CW) globally. Both are
|
||||
/// dynamic state in core Vulkan 1.3, so they stay per-run calls.
|
||||
/// </summary>
|
||||
private static void SetCullMode(IGpuPassEncoder encoder, CullMode mode)
|
||||
{
|
||||
encoder.SetFrontFace(GpuFrontFace.Clockwise);
|
||||
switch (mode)
|
||||
{
|
||||
case CullMode.None:
|
||||
encoder.SetCullMode(GpuCullMode.None);
|
||||
break;
|
||||
case CullMode.Clockwise:
|
||||
encoder.SetCullMode(GpuCullMode.Front);
|
||||
break;
|
||||
case CullMode.CounterClockwise:
|
||||
case CullMode.Landblock:
|
||||
encoder.SetCullMode(GpuCullMode.Back);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reserves this frame's ring, copies into it, and binds the slice. A
|
||||
/// logically empty section still reserves one element so the bound range is
|
||||
/// never zero-length — the "bind at least one element so the shader never
|
||||
/// reads an unbound SSBO" rule the light buffers already stated.
|
||||
/// </summary>
|
||||
private static void BindRingSection<T>(
|
||||
IGpuPassEncoder encoder,
|
||||
IGpuFrame frame,
|
||||
uint binding,
|
||||
ReadOnlySpan<T> data)
|
||||
where T : unmanaged
|
||||
{
|
||||
int elementBytes = sizeof(T);
|
||||
int byteCount = Math.Max(data.Length * elementBytes, elementBytes);
|
||||
GpuRingAllocation allocation = frame.AllocateRing(byteCount, GpuRingUsage.Storage);
|
||||
if (!data.IsEmpty)
|
||||
data.CopyTo(allocation.AsSpan<T>());
|
||||
encoder.BindStorageBuffer(
|
||||
binding,
|
||||
allocation.Buffer,
|
||||
allocation.OffsetBytes,
|
||||
(uint)byteCount);
|
||||
}
|
||||
|
||||
private void DisposeRhiResources()
|
||||
{
|
||||
_opaquePipeline?.Dispose();
|
||||
_opaquePipeline = null;
|
||||
_alphaPipeline?.Dispose();
|
||||
_alphaPipeline = null;
|
||||
_additivePipeline?.Dispose();
|
||||
_additivePipeline = null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue