bundle D3D12 descriptors with resource lifetime

This commit is contained in:
jacob 2026-03-12 05:15:51 -05:00
parent fc1d878847
commit e3bbeec2be
11 changed files with 249 additions and 1111 deletions

View File

@ -774,14 +774,12 @@ Struct(ComputeShaderDesc) { ResourceKey resource; u32 x, y, z; };
String name;
void **addr_ptr;
};
Struct(ApiDesc)
{
String path;
u64 procs_count;
ApiProcDesc *procs;
};
#define DeclApiProcVarX(_name, _return_type, _signature) _return_type (*_name) _signature;
#define DeclApiProcDescX(_name, _return_type, _signature) { .name = CompLit(Stringize(_name)), .addr_ptr = (void **)&_name },
#define DeclApiFromXList(api_name, xlist, api_path) \

View File

@ -13,8 +13,8 @@ void BootstrapResources(u64 archive_strings_count, String *archive_strings)
BB_Reader bbr = BB_ReaderFromBuff(&bb);
u64 magic = BB_ReadUBits(&bbr, 64);
Assert(magic == ResourceEmbeddedMagic);
if (magic == ResourceEmbeddedMagic)
{
// Create & insert entries
u64 num_entries = BB_ReadUBits(&bbr, 64);
for (u64 i = 0; i < num_entries; ++i)
@ -36,6 +36,12 @@ void BootstrapResources(u64 archive_strings_count, String *archive_strings)
}
Base.resource.entries_count += num_entries;
}
else
{
// Unexpected resource header magic
Assert(0);
}
}
}
}

View File

@ -14,7 +14,7 @@
#define GPU_NAMES IsRtcEnabled
#define GPU_SHADER_PRINT 1
#define GPU_SHADER_PRINT_BUFFER_SIZE Kibi(64);
#define GPU_SHADER_PRINT_BUFFER_SIZE Kibi(64)
#define GPU_SHADER_PRINT_LOG 1
// If enabled, bitbuffs will insert/verify magic numbers & length for each read & write

View File

@ -13,7 +13,10 @@ void G_BootstrapCommon(void)
// Init quad index buffer
{
u16 quad_indices[6] = { 0, 1, 2, 0, 2, 3 };
G.quad_indices = G_PushStructsFromCpu(cl, gpu_perm, quad_indices, countof(quad_indices));
G.quad_indices = G_IB(
countof(quad_indices),
G_PushStructsFromCpu(cl, gpu_perm, quad_indices, countof(quad_indices))
);
}
// Init blank texture
@ -224,7 +227,7 @@ G_SamplerRef G_BasicSamplerFromKind(G_BasicSamplerKind kind)
return G.basic_samplers[kind];
}
G_BufferRef G_QuadIndices(void)
G_IndexBufferDesc G_QuadIndices(void)
{
return G.quad_indices;
}

View File

@ -4,7 +4,7 @@
Struct(G_Ctx)
{
// Common shared resources
G_BufferRef quad_indices;
G_IndexBufferDesc quad_indices;
G_TextureRef blank_tex2d;
G_TextureRef basic_noise_tex3d;
G_SamplerRef basic_samplers[G_BasicSamplerKind_COUNT];
@ -48,6 +48,6 @@ Rng2 G_ScissorFromTexture(G_TextureRef texture);
//- Shared resources
G_SamplerRef G_BasicSamplerFromKind(G_BasicSamplerKind kind);
G_BufferRef G_QuadIndices(void);
G_IndexBufferDesc G_QuadIndices(void);
G_TextureRef G_Blank2D(void);
G_TextureRef G_BasicNoise3D(void);

View File

@ -320,8 +320,8 @@ Enum(G_MemoryFlag)
G_MemoryFlag_AllowTextureRW = (1 << 1),
G_MemoryFlag_AllowTextureDraw = (1 << 2),
G_MemoryFlag_AllowTextureDepthStencil = (1 << 3),
G_MemoryFlag_HostCached = (1 << 4), // Will be mapped to write-back Cpu memory
G_MemoryFlag_HostUncached = (1 << 5), // Will be mapped to write-combined Cpu memory
G_MemoryFlag_CpuRead = (1 << 4),
G_MemoryFlag_CpuWrite = (1 << 5),
G_MemoryFlag_ForceNoReuse = (1 << 6),
};
@ -391,7 +391,7 @@ Enum(G_BlendMode)
G_BlendMode_CompositePremultipliedAlpha,
};
#define G_Rt(_tex, _blend_mode, ...) ((G_RenderTargetDesc) { .texture = (_tex), .blend = (_blend_mode), __VA_ARGS__ })
#define G_RT(_tex, _blend_mode, ...) ((G_RenderTargetDesc) { .texture = (_tex), .blend = (_blend_mode), __VA_ARGS__ })
Struct(G_RenderTargetDesc)
{
G_TextureRef texture;
@ -399,6 +399,13 @@ Struct(G_RenderTargetDesc)
i32 mip;
};
#define G_IB(_count, _buffer) ((G_IndexBufferDesc) { .count = (_count), .buffer = (_buffer), __VA_ARGS__ })
Struct(G_IndexBufferDesc)
{
u64 count;
G_BufferRef buffer;
};
////////////////////////////////////////////////////////////
//~ Statistic types
@ -519,9 +526,7 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl, G_ArenaHandle gpu_are
//- Count
u64 G_CountBuffer(G_BufferRef buffer);
u64 G_CountBufferBytes(G_BufferRef buffer);
u64 G_CountBufferStride(G_BufferRef buffer);
u64 G_CountStride(G_BufferRef buffer);
i32 G_Count1D(G_TextureRef texture);
Vec2I32 G_Count2D(G_TextureRef texture);
Vec3I32 G_Count3D(G_TextureRef texture);
@ -592,7 +597,7 @@ void G_ComputeEx(G_CommandListHandle cl, ComputeShaderDesc cs, Vec3I32 threads);
void G_Draw(
G_CommandListHandle cl,
VertexShaderDesc vs, PixelShaderDesc ps,
u32 instances_count, G_BufferRef indices,
u32 instances_count, G_IndexBufferDesc indices,
u32 render_targets_count, G_RenderTargetDesc *render_targets,
Rng3 viewport, Rng2 scissor,
G_DrawMode draw_mode

File diff suppressed because it is too large Load Diff

View File

@ -78,14 +78,21 @@ Struct(G_D12_Resource)
// D3D12 resource
D3D12_RESOURCE_DESC1 d3d_desc;
ID3D12Resource *d3d_resource;
D3D12_GPU_VIRTUAL_ADDRESS buffer_gpu_address;
void *mapped;
struct G_D12_Descriptor *gpu_descriptor;
// Buffer info
D3D12_GPU_VIRTUAL_ADDRESS buffer_gpu_address;
u64 buffer_element_offset;
u64 buffer_element_stride;
u64 buffer_element_count;
// Texture info
b32 is_texture;
G_Format texture_format;
Vec3I32 texture_dims;
i32 texture_mips;
RngI32 texture_mips;
// Sampler info
G_SamplerDesc sampler_desc;
@ -123,32 +130,14 @@ Struct(G_D12_Descriptor)
G_D12_Descriptor *next;
G_D12_Descriptor *prev;
// Persistent data
struct G_D12_DescriptorHeap *heap;
D3D12_CPU_DESCRIPTOR_HANDLE d3d_handle;
u32 base_index;
u64 bundle_count;
// Per-resource data
struct
{
struct G_D12_Arena *gpu_arena;
struct G_D12_Resource *resource;
u64 buffer_element_offset;
u64 buffer_element_count;
u64 buffer_element_stride;
RngI32 mips;
G_QueueKind completion_queue_kind;
i64 completion_queue_target;
} info;
Atomic64 current_resource_ptr;
};
Struct(G_D12_DescriptorList)
{
u64 count;
@ -167,7 +156,7 @@ Struct(G_D12_DescriptorHeap)
Mutex mutex;
Arena *arena;
Arena *indices_to_descriptors_arena;
Arena *gpu_visible_indices_to_descriptors_arena;
D3D12_DESCRIPTOR_HEAP_TYPE type;
ID3D12DescriptorHeap *d3d_heap;
@ -195,10 +184,6 @@ Enum(G_D12_ResourceHeapKind)
Struct(G_D12_Arena)
{
Arena *arena;
G_D12_DescriptorList descriptors;
G_D12_DescriptorTable reset_descriptor_tables_by_heap[G_D12_DescriptorHeapKind_COUNT];
G_D12_ResourceList resources;
G_D12_ResourceList reset_resources;
};
@ -299,22 +284,7 @@ Struct(G_D12_Releasable)
i64 completion_queue_target;
ID3D12Resource *d3d_resource;
// FIXME: Release descriptors as well
G_D12_Descriptor *gpu_descriptor;
};
Struct(G_D12_ReleasableList)
@ -402,7 +372,7 @@ Struct(G_D12_Cmd)
VertexShaderDesc vs;
PixelShaderDesc ps;
u32 instances_count;
G_BufferRef indices;
G_IndexBufferDesc indices;
G_RenderTargetDesc render_target_descs[G_MaxRenderTargets];
Rng3 viewport;
Rng2 scissor;
@ -435,7 +405,6 @@ Struct(G_D12_CmdList)
G_QueueKind queue_kind;
Arena *arena;
G_D12_DescriptorList reset_descriptors;
G_D12_ReleasableList releases;
G_D12_StagingRegionNode *first_staging_region;
@ -753,18 +722,22 @@ void G_D12_ResetArena(G_D12_CmdList *cl, G_D12_Arena *gpu_arena);
////////////////////////////////////////////////////////////
//~ Descriptor
// G_D12_Descriptor *G_D12_DescriptorFromIndex(G_D12_DescriptorHeapKind heap_kind, u32 index);
// G_D12_Descriptor *G_D12_PushDescriptor(G_D12_Arena *gpu_arena, G_D12_DescriptorHeapKind heap_kind);
G_D12_Descriptor *G_D12_AcquireDescriptor(G_D12_DescriptorHeapKind heap_kind, u64 bundle_count);
void G_D12_ReleaseDescriptor(G_D12_Descriptor *descriptor);
void G_D12_InitRtvDescriptor(G_D12_Descriptor *descriptor, G_D12_Resource *resource, i32 mip);
G_D12_Descriptor *G_D12_DescriptorFromBufferRef(G_BufferRef ref);
G_D12_Descriptor *G_D12_DescriptorFromTextureRef(G_TextureRef ref);
G_D12_Descriptor *G_D12_DescriptorFromSamplerRef(G_SamplerRef ref);
G_D12_Resource *G_D12_ResourceFromBufferRef(G_BufferRef ref);
G_D12_Resource *G_D12_ResourceFromTextureRef(G_TextureRef ref);
G_D12_Resource *G_D12_ResourceFromSamplerRef(G_SamplerRef ref);
////////////////////////////////////////////////////////////
//~ Command helpers
G_D12_Cmd *G_D12_PushCmd(G_D12_CmdList *cl);
G_D12_StagingRegionNode *G_D12_PushStagingRegion(G_D12_CmdList *cl, u64 size);
void G_D12_InitRtvDescriptor(G_D12_Descriptor *descriptor, G_D12_Resource *resource, i32 mip);
////////////////////////////////////////////////////////////
//~ Instrumentation

View File

@ -149,22 +149,18 @@ Enum(G_BasicSamplerKind)
#endif
////////////////////////////////////////////////////////////
//~ Resource countof
//~ Texture dimensions
#define G_MaxMips 16
#define G_MaxRenderTargets 8
#if IsGpu
template<typename T> u32 countof(StructuredBuffer<T> obj) { u32 result; obj.GetDimensions(result); return result; }
template<typename T> u32 countof(RWStructuredBuffer<T> obj) { u32 result; u32 stride; obj.GetDimensions(result, stride); return result; }
u32 countof(ByteAddressBuffer obj) { u32 result; obj.GetDimensions(result); return result; }
u32 countof(RWByteAddressBuffer obj) { u32 result; obj.GetDimensions(result); return result; }
template<typename T> u32 countof(Texture1D<T> obj) { u32 result; obj.GetDimensions(result); return result; }
template<typename T> u32 countof(RWTexture1D<T> obj) { u32 result; obj.GetDimensions(result); return result; }
template<typename T> Vec2U32 countof(Texture2D<T> obj) { Vec2U32 result; obj.GetDimensions(result.x, result.y); return result; }
template<typename T> Vec2U32 countof(RWTexture2D<T> obj) { Vec2U32 result; obj.GetDimensions(result.x, result.y); return result; }
template<typename T> Vec3U32 countof(Texture3D<T> obj) { Vec3U32 result; obj.GetDimensions(result.x, result.y, result.z); return result; }
template<typename T> Vec3U32 countof(RWTexture3D<T> obj) { Vec3U32 result; obj.GetDimensions(result.x, result.y, result.z); return result; }
template<typename T> u32 G_Count1D(Texture1D<T> obj) { u32 result; obj.GetDimensions(result); return result; }
template<typename T> u32 G_Count1D(RWTexture1D<T> obj) { u32 result; obj.GetDimensions(result); return result; }
template<typename T> Vec2U32 G_Count2D(Texture2D<T> obj) { Vec2U32 result; obj.GetDimensions(result.x, result.y); return result; }
template<typename T> Vec2U32 G_Count2D(RWTexture2D<T> obj) { Vec2U32 result; obj.GetDimensions(result.x, result.y); return result; }
template<typename T> Vec3U32 G_Count3D(Texture3D<T> obj) { Vec3U32 result; obj.GetDimensions(result.x, result.y, result.z); return result; }
template<typename T> Vec3U32 G_Count3D(RWTexture3D<T> obj) { Vec3U32 result; obj.GetDimensions(result.x, result.y, result.z); return result; }
#endif
////////////////////////////////////////////////////////////
@ -280,7 +276,7 @@ Struct(G_FmtArg)
base += 4; // Offset for success counter
base += 4; // Offset for overflow counter
if ((base + alloc_size) < countof(rw))
if ((base + alloc_size) < GPU_SHADER_PRINT_BUFFER_SIZE)
{
// Increment success counter
rw.InterlockedAdd(4, 1);

View File

@ -58,7 +58,7 @@ void PT_RunForever(WaveLaneCtx *lane)
cl,
PT_BlitVS, PT_BlitPS,
1, G_QuadIndices(),
1, &G_Rt(frame->screen, G_BlendMode_CompositeStraightAlpha),
1, &G_RT(frame->screen, G_BlendMode_CompositeStraightAlpha),
G_ViewportFromTexture(frame->screen), G_ScissorFromTexture(frame->screen),
G_DrawMode_TriangleList
);

View File

@ -6,10 +6,10 @@ ComputeShader(PT_TestCS)
PT_SharedFrame frame = G_UniformDeref<PT_SharedFrame>(PT_ShaderConst_Frame)[0];
RWTexture2D<Vec4> target_tex = G_UniformDerefRW2D<Vec4>(frame.compute_target);
Vec2U32 target_tex_size = countof(target_tex);
Vec2U32 target_tex_size = G_Count2D(target_tex);
Vec2I32 id = SV_DispatchThreadID;
if (id.x < target_tex_size.x && id.y < target_tex_size.y)
if (all(id < G_Count2D(target_tex)))
{
target_tex[id] = Vec4(0, 1, 0, 1);
}