auto-discard resources during allocation

This commit is contained in:
jacob 2026-02-24 08:56:10 -06:00
parent c3c9be43e3
commit 9ad6b856c5
4 changed files with 72 additions and 39 deletions

View File

@ -785,10 +785,6 @@ void G_Rasterize(
void G_ClearRenderTarget(G_CommandListHandle cl, G_ResourceHandle render_target, Vec4 color, i32 mip);
//- Discard
void G_DiscardRenderTarget(G_CommandListHandle cl, G_ResourceHandle render_target, i32 mip);
//- Log
void G_LogResource(G_CommandListHandle cl, G_ResourceHandle resource);

View File

@ -1263,7 +1263,8 @@ G_ResourceHandle G_PushResource(G_ArenaHandle arena_handle, G_CommandListHandle
//////////////////////////////
//- Initialize d3d resource desc
D3D12_BARRIER_LAYOUT d3d_initial_layout = D3D12_BARRIER_LAYOUT_UNDEFINED;
D3D12_BARRIER_LAYOUT d3d_begin_layout = D3D12_BARRIER_LAYOUT_UNDEFINED;
D3D12_BARRIER_LAYOUT d3d_end_layout = D3D12_BARRIER_LAYOUT_UNDEFINED;
D3D12_CLEAR_VALUE clear_value = Zi;
D3D12_RESOURCE_DESC1 d3d_desc = Zi;
if (is_buffer)
@ -1284,7 +1285,6 @@ G_ResourceHandle G_PushResource(G_ArenaHandle arena_handle, G_CommandListHandle
{
i32 largest_dim = MaxI32(MaxI32(desc.texture.dims.x, desc.texture.dims.y), desc.texture.dims.z);
i32 max_mips = MinI32(FloorF32(Log2F32(largest_dim)) + 1, G_MaxMips);
d3d_initial_layout = G_D12_BarrierLayoutFromLayout(desc.texture.initial_layout);
d3d_desc.Dimension = (
desc.kind == G_ResourceKind_Texture1D ? D3D12_RESOURCE_DIMENSION_TEXTURE1D :
desc.kind == G_ResourceKind_Texture2D ? D3D12_RESOURCE_DIMENSION_TEXTURE2D :
@ -1307,6 +1307,20 @@ G_ResourceHandle G_PushResource(G_ArenaHandle arena_handle, G_CommandListHandle
clear_value.Color[2] = desc.texture.clear_color.z,
clear_value.Color[3] = desc.texture.clear_color.w,
clear_value.Format = d3d_desc.Format;
d3d_begin_layout = G_D12_BarrierLayoutFromLayout(desc.texture.initial_layout);
d3d_end_layout = d3d_begin_layout;
if (!AnyBit(flags, G_ResourceFlag_ZeroMemory))
{
if (AnyBit(d3d_desc.Flags, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET))
{
d3d_begin_layout = D3D12_BARRIER_LAYOUT_RENDER_TARGET;
}
else if (AnyBit(d3d_desc.Flags, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL))
{
d3d_begin_layout = D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE;
}
}
}
//////////////////////////////
@ -1422,7 +1436,7 @@ G_ResourceHandle G_PushResource(G_ArenaHandle arena_handle, G_CommandListHandle
&heap_props,
heap_flags,
&resource->d3d_desc,
d3d_initial_layout,
d3d_begin_layout,
clear_value_arg,
0, // pProtectedSession
0, // NumCastableFormats
@ -1431,10 +1445,46 @@ G_ResourceHandle G_PushResource(G_ArenaHandle arena_handle, G_CommandListHandle
(void **)&resource->d3d_resource
);
Atomic64FetchAdd(&G_D12.cumulative_nonreuse_count, 1);
for (i32 mip_idx = 0; mip_idx < resource->texture_mips; ++mip_idx)
{
resource->cmdlist_texture_layouts[mip_idx] = d3d_initial_layout;
resource->cmdlist_texture_layouts[mip_idx] = d3d_begin_layout;
}
// Queue initial Rtv/Dsv discard
if (!AnyBit(flags, G_ResourceFlag_ZeroMemory))
{
if (d3d_begin_layout == D3D12_BARRIER_LAYOUT_RENDER_TARGET)
{
{
G_D12_Cmd *cmd = G_D12_PushCmd(cl);
cmd->kind = G_D12_CmdKind_Discard;
cmd->discard.resource = resource;
}
{
G_MemoryLayoutSync(
cl_handle, G_D12_MakeHandle(G_ResourceHandle, resource),
G_Stage_RenderTarget, G_Access_RenderTargetWrite,
G_Stage_All, G_Access_All,
desc.texture.initial_layout
);
}
}
else if (d3d_begin_layout == D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE)
{
{
G_D12_Cmd *cmd = G_D12_PushCmd(cl);
cmd->kind = G_D12_CmdKind_Discard;
cmd->discard.resource = resource;
}
{
G_MemoryLayoutSync(
cl_handle, G_D12_MakeHandle(G_ResourceHandle, resource),
G_Stage_DepthStencil, G_Access_DepthStencilWrite,
G_Stage_All, G_Access_All,
desc.texture.initial_layout
);
}
}
}
if (!SUCCEEDED(hr))
@ -2758,18 +2808,6 @@ i64 G_CommitCommandList(G_CommandListHandle cl_handle)
cmd_idx += 1;
} break;
//- Discard rtv
case G_D12_CmdKind_DiscardRtv:
{
D3D12_DISCARD_REGION region = Zi;
region.FirstSubresource = cmd->discard_rtv.mip;
region.NumSubresources = 1;
G_D12_Resource *resource = cmd->discard_rtv.render_target;
ID3D12GraphicsCommandList_DiscardResource(d3d_cl, resource->d3d_resource, 0);
cmd_idx += 1;
} break;
//- Log
case G_D12_CmdKind_Log:
@ -2799,6 +2837,18 @@ i64 G_CommitCommandList(G_CommandListHandle cl_handle)
LogDebug(msg);
cmd_idx += 1;
} break;
//- Discard
case G_D12_CmdKind_Discard:
{
G_D12_Resource *resource = cmd->discard.resource;
D3D12_DISCARD_REGION region = Zi;
region.FirstSubresource = 0;
region.NumSubresources = resource->texture_mips;
ID3D12GraphicsCommandList_DiscardResource(d3d_cl, resource->d3d_resource, 0);
cmd_idx += 1;
} break;
}
}
}
@ -3173,17 +3223,6 @@ void G_ClearRenderTarget(G_CommandListHandle cl_handle, G_ResourceHandle resourc
cmd->clear_rtv.mip = mip;
}
//- Discard
void G_DiscardRenderTarget(G_CommandListHandle cl_handle, G_ResourceHandle resource_handle, i32 mip)
{
G_D12_CmdList *cl = G_D12_CmdListFromHandle(cl_handle);
G_D12_Cmd *cmd = G_D12_PushCmd(cl);
cmd->kind = G_D12_CmdKind_DiscardRtv;
cmd->discard_rtv.render_target = G_D12_ResourceFromHandle(resource_handle);
cmd->discard_rtv.mip = mip;
}
//- Log
void G_LogResource(G_CommandListHandle cl_handle, G_ResourceHandle resource_handle)

View File

@ -306,8 +306,8 @@ Enum(G_D12_CmdKind)
G_D12_CmdKind_Compute,
G_D12_CmdKind_Rasterize,
G_D12_CmdKind_ClearRtv,
G_D12_CmdKind_DiscardRtv,
G_D12_CmdKind_Log,
G_D12_CmdKind_Discard,
};
Struct(G_D12_Cmd)
@ -376,14 +376,13 @@ Struct(G_D12_Cmd)
struct
{
G_D12_Resource *render_target;
i32 mip;
} discard_rtv;
G_D12_Resource *resource;
} log;
struct
{
G_D12_Resource *resource;
} log;
} discard;
};
};

View File

@ -4805,7 +4805,7 @@ void V_TickForever(WaveLaneCtx *lane)
gpu_frame_arena, frame->cl,
G_Format_R16G16B16A16_Float,
frame->screen_dims,
G_Layout_DirectQueue_RenderTarget,
G_Layout_DirectQueue_General,
.flags = G_ResourceFlag_AllowShaderReadWrite | G_ResourceFlag_AllowRenderTarget,
.name = StringF(frame->arena, "Screen target [%F]", FmtSint(frame->tick))
);
@ -4977,7 +4977,6 @@ void V_TickForever(WaveLaneCtx *lane)
}
// Prepare RTs
G_DiscardRenderTarget(frame->cl, screen_target, 0);
G_ClearRenderTarget(frame->cl, albedo_target, VEC4(0, 0, 0, 0), 0);
}