convert layers to resourceless-gpu api usage

This commit is contained in:
jacob 2026-03-12 07:04:48 -05:00
parent e3bbeec2be
commit bef6f91b5e
17 changed files with 389 additions and 527 deletions

View File

@ -312,14 +312,13 @@ void GC_TickAsync(WaveLaneCtx *lane, AsyncFrameLaneCtx *base_async_lane_frame)
atlas->dims = VEC2I32(1024, 1024); atlas->dims = VEC2I32(1024, 1024);
{ {
G_ArenaHandle gpu_perm = G_PermArena(); G_ArenaHandle gpu_perm = G_PermArena();
atlas->tex_res = G_PushTexture2D( atlas->tex = G_PushTexture2D(
gpu_perm, cl, cl, gpu_perm,
G_TextureLayout_Simultaneous,
G_Format_R8G8B8A8_Unorm_Srgb, G_Format_R8G8B8A8_Unorm_Srgb,
atlas->dims, atlas->dims,
G_Layout_Simultaneous,
.name = Lit("Glyph atlas") .name = Lit("Glyph atlas")
); );
atlas->tex = G_PushTexture2DRef(gpu_perm, atlas->tex_res);
} }
SllStackPush(GC.first_atlas, atlas); SllStackPush(GC.first_atlas, atlas);
++GC.atlases_count; ++GC.atlases_count;
@ -358,7 +357,7 @@ void GC_TickAsync(WaveLaneCtx *lane, AsyncFrameLaneCtx *base_async_lane_frame)
{ {
G_CopyCpuToTexture( G_CopyCpuToTexture(
cl, cl,
glyph->atlas->tex_res, VEC3I32(glyph->atlas_slice.p0.x, glyph->atlas_slice.p0.y, 0), glyph->atlas->tex, VEC3I32(glyph->atlas_slice.p0.x, glyph->atlas_slice.p0.y, 0),
image_pixels, VEC3I32(image_dims.x, image_dims.y, 1), image_pixels, VEC3I32(image_dims.x, image_dims.y, 1),
RNG3I32( RNG3I32(
VEC3I32(0, 0, 0), VEC3I32(0, 0, 0),

View File

@ -14,8 +14,7 @@ Struct(GC_Atlas)
GC_Atlas *next; GC_Atlas *next;
Vec2I32 dims; Vec2I32 dims;
G_ResourceHandle tex_res; G_TextureRef tex;
G_Texture2DRef tex;
Vec2I32 cur_pos; Vec2I32 cur_pos;
i32 cur_row_height; i32 cur_row_height;
}; };
@ -68,7 +67,7 @@ Struct(GC_RunRect)
f32 baseline_pos; // Horizontal distance from start of baseline f32 baseline_pos; // Horizontal distance from start of baseline
f32 advance; f32 advance;
G_Texture2DRef tex; G_TextureRef tex;
Rng2I32 tex_slice; Rng2I32 tex_slice;
Rng2 tex_slice_uv; Rng2 tex_slice_uv;
}; };

View File

@ -34,7 +34,7 @@ void G_BootstrapCommon(void)
// Init noise texture // Init noise texture
{ {
String noise_data = DataFromResource(ResourceKeyFromStore(&G_Resources, Lit("noise_128x128x64_16.dat"))); String noise_data = DataFromResource(ResourceKeyFromStore(&G_Resources, Lit("noise_128x128x64_16.dat")));
Vec3I32 noise_dims = VEC3I32(128, 128, 64); Vec3I32 noise_dims = G_BasicNoiseDims;
if (noise_data.len != noise_dims.x * noise_dims.y * noise_dims.z * 2) if (noise_data.len != noise_dims.x * noise_dims.y * noise_dims.z * 2)
{ {
Panic(Lit("Unexpected noise texture size")); Panic(Lit("Unexpected noise texture size"));

View File

@ -399,13 +399,6 @@ Struct(G_RenderTargetDesc)
i32 mip; 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 //~ Statistic types
@ -442,8 +435,10 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl, G_ArenaHandle gpu_are
//- Buffer memory //- Buffer memory
#define G_PushStruct(_cl, _arena, _type, ...) G_PushStructs((_cl), (_arena), (_type), 1, __VA_ARGS__) #define G_PushStruct(_cl, _arena, _type, ...) G_PushStructs((_cl), (_arena), (_type), 1, __VA_ARGS__)
#define G_PushStructs(_cl, _arena, _type, _count, ...) ((G_BufferRef) { .v = G_PushMemory((_cl), (_arena), \ #define G_PushStructFromCpu(_cl, _arena, _src, ...) G_PushStructsFromCpu((_cl), (_arena), (_src), 1, __VA_ARGS__)
(G_MemoryDesc) { \
#define G_PushStructs(_cl, _arena, _type, _count, ...) ( \
(G_BufferRef) { .v = G_PushMemory((_cl), (_arena), (G_MemoryDesc) { \
.kind = G_MemoryKind_Buffer, \ .kind = G_MemoryKind_Buffer, \
.buffer = { \ .buffer = { \
.count = (_count), \ .count = (_count), \
@ -453,9 +448,8 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl, G_ArenaHandle gpu_are
} \ } \
)}) )})
#define G_PushStructFromCpu(_cl, _arena, _src, ...) G_PushStructsFromCpu((_cl), (_arena), (_src), 1, __VA_ARGS__) #define G_PushStructsFromCpu(_cl, _arena, _src, _count, ...) ( \
#define G_PushStructsFromCpu(_cl, _arena, _src, _count, ...) ((G_BufferRef) { .v = G_PushMemory((_cl), (_arena), \ (G_BufferRef) { .v = G_PushMemory((_cl), (_arena), (G_MemoryDesc) { \
(G_MemoryDesc) { \
.kind = G_MemoryKind_Buffer, \ .kind = G_MemoryKind_Buffer, \
.buffer = { \ .buffer = { \
.count = (_count), \ .count = (_count), \
@ -466,11 +460,10 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl, G_ArenaHandle gpu_are
} \ } \
)}) )})
//- Texture memory //- Texture memory
#define G_PushTexture1D(_cl, _arena, _initial_layout, _format, _dims, ...) ((G_TextureRef) { .v = G_PushMemory((_cl), (_arena), \ #define G_PushTexture1D(_cl, _arena, _initial_layout, _format, _dims, ...) ( \
(G_MemoryDesc) { \ (G_TextureRef) { .v = G_PushMemory((_cl), (_arena), (G_MemoryDesc) { \
.kind = G_MemoryKind_Texture1D, \ .kind = G_MemoryKind_Texture1D, \
.texture = { \ .texture = { \
.format = (_format), \ .format = (_format), \
@ -482,8 +475,8 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl, G_ArenaHandle gpu_are
)}) )})
#define G_PushTexture2D(_cl, _arena, _initial_layout, _format, _dims, ...) ((G_TextureRef) { .v = G_PushMemory((_cl), (_arena), \ #define G_PushTexture2D(_cl, _arena, _initial_layout, _format, _dims, ...) ( \
(G_MemoryDesc) { \ (G_TextureRef) { .v = G_PushMemory((_cl), (_arena), (G_MemoryDesc) { \
.kind = G_MemoryKind_Texture2D, \ .kind = G_MemoryKind_Texture2D, \
.texture = { \ .texture = { \
.format = (_format), \ .format = (_format), \
@ -494,11 +487,8 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl, G_ArenaHandle gpu_are
} \ } \
)}) )})
#define G_PushTexture3D(_cl, _arena, _initial_layout, _format, _dims, ...) ( \
(G_TextureRef) { .v = G_PushMemory((_cl), (_arena), (G_MemoryDesc) { \
#define G_PushTexture3D(_cl, _arena, _initial_layout, _format, _dims, ...) ((G_TextureRef) { .v = G_PushMemory((_cl), (_arena), \
(G_MemoryDesc) { \
.kind = G_MemoryKind_Texture3D, \ .kind = G_MemoryKind_Texture3D, \
.texture = { \ .texture = { \
.format = (_format), \ .format = (_format), \
@ -509,13 +499,10 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl, G_ArenaHandle gpu_are
} \ } \
)}) )})
//- Sampler memory //- Sampler memory
#define G_PushSampler(_cl, _arena, ...) ((G_SamplerRef) { .v = G_PushMemory((_cl), (_arena), \ #define G_PushSampler(_cl, _arena, ...) ( \
(G_MemoryDesc) { \ (G_SamplerRef) { .v = G_PushMemory((_cl), (_arena), (G_MemoryDesc) { \
.kind = G_MemoryKind_Sampler, \ .kind = G_MemoryKind_Sampler, \
.sampler = { \ .sampler = { \
.filter = G_Filter_MinMagMipPoint, \ .filter = G_Filter_MinMagMipPoint, \
@ -617,11 +604,12 @@ G_QueueCompletions G_CompletionTargetsFromQueues(G_QueueMask queue_mask);
void G_QueueSyncEx(G_QueueBarrierDesc desc); void G_QueueSyncEx(G_QueueBarrierDesc desc);
#define G_QueueSync(completion_mask, ...) \ #define G_QueueSync(completion_mask, ...) ( \
G_QueueSyncEx((G_QueueBarrierDesc) { \ G_QueueSyncEx((G_QueueBarrierDesc) { \
.completions = G_CompletionTargetsFromQueues(completion_mask), \ .completions = G_CompletionTargetsFromQueues(completion_mask), \
__VA_ARGS__ \ __VA_ARGS__ \
}) }) \
)
#define G_QueueSyncGpu(completion_mask, wait_mask) G_QueueSync((completion_mask), .wait_queues = (wait_mask)) #define G_QueueSyncGpu(completion_mask, wait_mask) G_QueueSync((completion_mask), .wait_queues = (wait_mask))
#define G_QueueSyncCpu(completion_mask) G_QueueSync((completion_mask), .wait_cpu = 1); #define G_QueueSyncCpu(completion_mask) G_QueueSync((completion_mask), .wait_cpu = 1);

View File

@ -1206,9 +1206,7 @@ void G_D12_ReleaseDescriptor(G_D12_Descriptor *descriptor)
{ {
G_D12_DescriptorHeap *heap = descriptor->heap; G_D12_DescriptorHeap *heap = descriptor->heap;
G_D12_DescriptorList *free_descriptors = &heap->free_descriptors_table.descriptors_by_bundle_count[descriptor->bundle_count]; G_D12_DescriptorList *free_descriptors = &heap->free_descriptors_table.descriptors_by_bundle_count[descriptor->bundle_count];
Atomic64Set(&descriptor->current_resource_ptr, 0); Atomic64Set(&descriptor->current_resource_ptr, 0);
Lock lock = LockE(&heap->mutex); Lock lock = LockE(&heap->mutex);
{ {
DllQueuePush(free_descriptors->first, free_descriptors->last, descriptor); DllQueuePush(free_descriptors->first, free_descriptors->last, descriptor);
@ -1460,7 +1458,9 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl_handle, G_ArenaHandle
////////////////////////////// //////////////////////////////
//- Allocate D3D12 resource //- Allocate D3D12 resource
if ((is_buffer || is_texture) && !resource->d3d_resource) if (!can_reuse)
{
if (is_buffer || is_texture)
{ {
D3D12_CLEAR_VALUE *clear_value_arg = 0; D3D12_CLEAR_VALUE *clear_value_arg = 0;
if (d3d_desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) if (d3d_desc.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL))
@ -1495,6 +1495,12 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl_handle, G_ArenaHandle
&IID_ID3D12Resource, &IID_ID3D12Resource,
(void **)&resource->d3d_resource (void **)&resource->d3d_resource
); );
if (!SUCCEEDED(hr))
{
// TODO: Don't panic
Panic(Lit("Failed to allocate D3D12 resource"));
}
Atomic64FetchAdd(&G_D12.cumulative_nonreuse_count, 1); Atomic64FetchAdd(&G_D12.cumulative_nonreuse_count, 1);
resource->uid = Atomic64FetchAdd(&G_D12.resource_creation_gen.v, d3d_desc.MipLevels); resource->uid = Atomic64FetchAdd(&G_D12.resource_creation_gen.v, d3d_desc.MipLevels);
@ -1508,11 +1514,6 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl_handle, G_ArenaHandle
cmd->kind = G_D12_CmdKind_Discard; cmd->kind = G_D12_CmdKind_Discard;
cmd->discard.resource = resource; cmd->discard.resource = resource;
} }
if (!SUCCEEDED(hr))
{
// TODO: Don't panic
Panic(Lit("Failed to allocate D3D12 resource"));
} }
if (is_buffer) if (is_buffer)
@ -1811,8 +1812,8 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl_handle, G_ArenaHandle
if ( if (
is_texture &&
can_reuse && can_reuse &&
is_texture &&
memory_desc.texture.initial_layout == G_TextureLayout_Common && memory_desc.texture.initial_layout == G_TextureLayout_Common &&
queue_kind != G_QueueKind_AsyncCopy queue_kind != G_QueueKind_AsyncCopy
) )
@ -1821,6 +1822,13 @@ G_BaseDescriptorIndex G_PushMemory(G_CommandListHandle cl_handle, G_ArenaHandle
} }
////////////////////////////// //////////////////////////////
//- Upload initial data //- Upload initial data

View File

@ -76,21 +76,25 @@ Enum(G_BasicSamplerKind)
G_BasicSamplerKind_COUNT G_BasicSamplerKind_COUNT
}; };
////////////////////////////////////////////////////////////
//~ Basic noise
#define G_BasicNoiseDims VEC3I32(128, 128, 64)
////////////////////////////////////////////////////////////
//~ Index buffers
#define G_IB(_count, _buffer) ((G_IndexBufferDesc) { .count = (_count), .buffer = (_buffer), __VA_ARGS__ })
Struct(G_IndexBufferDesc)
{
u64 count;
G_BufferRef buffer;
};
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Resource dereference //~ Resource dereference
#if IsGpu #if IsGpu
// NOTE: Uniform dereferencing is faster than Non-Uniform on AMD hardware
//- Scalar/Uniform dereference (faster on AMD hardware) //- Scalar/Uniform dereference (faster on AMD hardware)
template<typename T> StructuredBuffer<T> G_UniformDeref(G_BufferRef r) { return ResourceDescriptorHeap[r.v + 0]; } template<typename T> StructuredBuffer<T> G_UniformDeref(G_BufferRef r) { return ResourceDescriptorHeap[r.v + 0]; }
template<typename T> RWStructuredBuffer<T> G_UniformDerefRW(G_BufferRef r) { return ResourceDescriptorHeap[r.v + 1]; } template<typename T> RWStructuredBuffer<T> G_UniformDerefRW(G_BufferRef r) { return ResourceDescriptorHeap[r.v + 1]; }
@ -116,36 +120,6 @@ Enum(G_BasicSamplerKind)
template<typename T> RWTexture2D<T> G_DynamicDerefRW2D(G_TextureRef r, u32 mip=0) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + (mip * 2) + 1)]; } template<typename T> RWTexture2D<T> G_DynamicDerefRW2D(G_TextureRef r, u32 mip=0) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + (mip * 2) + 1)]; }
template<typename T> RWTexture3D<T> G_DynamicDerefRW3D(G_TextureRef r, u32 mip=0) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + (mip * 2) + 1)]; } template<typename T> RWTexture3D<T> G_DynamicDerefRW3D(G_TextureRef r, u32 mip=0) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + (mip * 2) + 1)]; }
SamplerState G_DynamicDeref(G_SamplerRef r) { return SamplerDescriptorHeap[NonUniformResourceIndex(r.v)]; } SamplerState G_DynamicDeref(G_SamplerRef r) { return SamplerDescriptorHeap[NonUniformResourceIndex(r.v)]; }
// //- Scalar/Uniform dereference
// ByteAddressBuffer G_SDerefRaw(G_BufferRef r) { return ResourceDescriptorHeap[r.v + 2]; }
// RWByteAddressBuffer G_SDerefRawRW(G_BufferRef r) { return ResourceDescriptorHeap[r.v + 3]; }
// template<typename T> StructuredBuffer<T> G_SDeref(G_BufferRef r) { return ResourceDescriptorHeap[r.v + 0]; }
// template<typename T> RWStructuredBuffer<T> G_SDerefRW(G_BufferRef r) { return ResourceDescriptorHeap[r.v + 1]; }
// template<typename T> Texture1D<T> G_SDeref(G_Texture1DRef r) { return ResourceDescriptorHeap[r.v + 0]; }
// template<typename T> Texture2D<T> G_SDeref(G_Texture2DRef r) { return ResourceDescriptorHeap[r.v + 0]; }
// template<typename T> Texture3D<T> G_SDeref(G_Texture3DRef r) { return ResourceDescriptorHeap[r.v + 0]; }
// template<typename T> RWTexture1D<T> G_SDerefRW(G_Texture1DRef r) { return ResourceDescriptorHeap[r.v + 1]; }
// template<typename T> RWTexture2D<T> G_SDerefRW(G_Texture2DRef r) { return ResourceDescriptorHeap[r.v + 1]; }
// template<typename T> RWTexture3D<T> G_SDerefRW(G_Texture3DRef r) { return ResourceDescriptorHeap[r.v + 1]; }
// SamplerState G_SDeref(G_SamplerStateRef r) { return SamplerDescriptorHeap[r.v]; }
// //- Vector/Non-Uniform dereference
// ByteAddressBuffer G_VDerefRaw(G_BufferRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 2)]; }
// RWByteAddressBuffer G_VDerefRawRW(G_BufferRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 3)]; }
// template<typename T> StructuredBuffer<T> G_VDeref(G_BufferRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 0)]; }
// template<typename T> RWStructuredBuffer<T> G_VDerefRW(G_BufferRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 1)]; }
// template<typename T> Texture1D<T> G_VDeref(G_Texture1DRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 0)]; }
// template<typename T> Texture2D<T> G_VDeref(G_Texture2DRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 0)]; }
// template<typename T> Texture3D<T> G_VDeref(G_Texture3DRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 0)]; }
// template<typename T> RWTexture1D<T> G_VDerefRW(G_Texture1DRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 1)]; }
// template<typename T> RWTexture2D<T> G_VDerefRW(G_Texture2DRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 1)]; }
// template<typename T> RWTexture3D<T> G_VDerefRW(G_Texture3DRef r) { return ResourceDescriptorHeap[NonUniformResourceIndex(r.v + 1)]; }
// SamplerState G_VDeref(G_SamplerStateRef r) { return SamplerDescriptorHeap[NonUniformResourceIndex(r.v)]; }
#endif #endif
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -400,95 +400,89 @@ void V_TickForever(WaveLaneCtx *lane)
Vec2I32 cells_dims = VEC2I32(P_CellsPitch, P_CellsPitch); Vec2I32 cells_dims = VEC2I32(P_CellsPitch, P_CellsPitch);
//- Init gpu state //- Init gpu state
G_Texture2DRef gpu_tiles = Zi; G_TextureRef gpu_tiles = Zi;
G_StructuredBufferRef gpu_particles = Zi; G_BufferRef gpu_particles = Zi;
G_Texture2DRef gpu_particle_cells[V_ParticleLayer_COUNT]; G_TextureRef gpu_particle_cells[V_ParticleLayer_COUNT];
G_Texture2DRef gpu_particle_densities[V_ParticleLayer_COUNT]; G_TextureRef gpu_particle_densities[V_ParticleLayer_COUNT];
G_Texture2DRef gpu_stains = Zi; G_TextureRef gpu_stains = Zi;
G_Texture2DRef gpu_dry_stains = Zi; G_TextureRef gpu_dry_stains = Zi;
G_Texture2DRef gpu_drynesses = Zi; G_TextureRef gpu_drynesses = Zi;
G_Texture2DRef gpu_occluders = Zi; G_TextureRef gpu_occluders = Zi;
{ {
G_CommandListHandle cl = G_PrepareCommandList(G_QueueKind_Direct); G_CommandListHandle cl = G_PrepareCommandList(G_QueueKind_Direct);
{ {
//- Init tile map texture //- Init tile map texture
gpu_tiles = G_PushTexture2DZero( gpu_tiles = G_PushTexture2D(
cl, cl, gpu_perm,
gpu_perm,
G_TextureLayout_Family, G_TextureLayout_Family,
G_Format_R8_Uint, G_Format_R8_Uint,
tiles_dims, tiles_dims,
.flags = G_MemoryFlag_Zero,
.name = Lit("Tiles") .name = Lit("Tiles")
); );
//- Init particle buffer //- Init particle buffer
gpu_particles = G_PushBufferZero( gpu_particles = G_PushStructs(
cl, cl, gpu_perm,
gpu_perm, V_Particle, V_ParticlesCap,
V_Particle, .flags = G_MemoryFlag_Zero,
V_ParticlesCap,
.name = Lit("Particles") .name = Lit("Particles")
); );
//- Init particle textures //- Init particle textures
for (V_ParticleLayer layer = 0; layer < V_ParticleLayer_COUNT; ++layer) for (V_ParticleLayer layer = 0; layer < V_ParticleLayer_COUNT; ++layer)
{ {
gpu_particle_cells[layer] = G_PushTexture2DZero( gpu_particle_cells[layer] = G_PushTexture2D(
cl, cl, gpu_perm,
gpu_perm,
G_TextureLayout_Family, G_TextureLayout_Family,
G_Format_R32_Uint, G_Format_R32_Uint,
cells_dims, cells_dims,
.flags = G_TextureFlag_AllowRW, .flags = G_MemoryFlag_Zero | G_MemoryFlag_AllowTextureRW,
.name = StringF(perm, "Particle cells - layer %F", FmtSint(layer)) .name = StringF(perm, "Particle cells - layer %F", FmtSint(layer))
); );
gpu_particle_densities[layer] = G_PushTexture2DZero( gpu_particle_densities[layer] = G_PushTexture2D(
cl, cl, gpu_perm,
gpu_perm,
G_TextureLayout_Family, G_TextureLayout_Family,
G_Format_R32_Uint, G_Format_R32_Uint,
cells_dims, cells_dims,
.flags = G_TextureFlag_AllowRW, .flags = G_MemoryFlag_Zero | G_MemoryFlag_AllowTextureRW,
.name = StringF(perm, "Particle densities - layer %F", FmtSint(layer)) .name = StringF(perm, "Particle densities - layer %F", FmtSint(layer))
); );
} }
//- Init stains texture //- Init stains texture
gpu_stains = G_PushTexture2DZero( gpu_stains = G_PushTexture2D(
cl, cl, gpu_perm,
gpu_perm,
G_TextureLayout_Family, G_TextureLayout_Family,
G_Format_R16G16B16A16_Float, G_Format_R16G16B16A16_Float,
cells_dims, cells_dims,
.flags = G_TextureFlag_AllowRW, .flags = G_MemoryFlag_Zero | G_MemoryFlag_AllowTextureRW,
.name = Lit("Stains") .name = Lit("Stains")
); );
//- Init dry stains texture //- Init dry stains texture
gpu_dry_stains = G_PushTexture2DZero( gpu_dry_stains = G_PushTexture2D(
gpu_perm, cl, cl, gpu_perm,
G_TextureLayout_Family, G_TextureLayout_Family,
G_Format_R16G16B16A16_Float, G_Format_R16G16B16A16_Float,
cells_dims, cells_dims,
.flags = G_TextureFlag_AllowRW, .flags = G_MemoryFlag_Zero | G_MemoryFlag_AllowTextureRW,
.name = Lit("Dry stains") .name = Lit("Dry stains")
); );
//- Init dryness texture //- Init dryness texture
gpu_drynesses = G_PushTexture2DZero( gpu_drynesses = G_PushTexture2D(
cl, cl, gpu_perm,
gpu_perm,
G_TextureLayout_Family, G_TextureLayout_Family,
G_Format_R32_Float, G_Format_R32_Float,
cells_dims, cells_dims,
.flags = G_TextureFlag_AllowRW, .flags = G_MemoryFlag_Zero | G_MemoryFlag_AllowTextureRW,
.name = Lit("Drynesses") .name = Lit("Drynesses")
); );
//- Init occluders texture //- Init occluders texture
gpu_occluders = G_PushTexture2DZero( gpu_occluders = G_PushTexture2D(
cl, cl, gpu_perm,
gpu_perm,
G_TextureLayout_Family, G_TextureLayout_Family,
G_Format_R32_Uint, G_Format_R32_Uint,
cells_dims, cells_dims,
.flags = G_TextureFlag_AllowRW, .flags = G_MemoryFlag_Zero | G_MemoryFlag_AllowTextureRW,
.name = Lit("Occluders") .name = Lit("Occluders")
); );
} }
@ -5121,10 +5115,10 @@ void V_TickForever(WaveLaneCtx *lane)
// Screen texture // Screen texture
frame->screen = G_PushTexture2D( frame->screen = G_PushTexture2D(
cl, gpu_frame_arena, cl, gpu_frame_arena,
G_TextureLayout_Family,
G_Format_R16G16B16A16_Float, G_Format_R16G16B16A16_Float,
frame->screen_dims, frame->screen_dims,
G_TextureLayout_Family, .flags = G_MemoryFlag_AllowTextureRW | G_MemoryFlag_AllowTextureDraw,
.flags = G_TextureFlag_AllowRW | G_ResourceFlag_AllowRenderTarget,
.name = StringF(frame->arena, "Screen target [%F]", FmtSint(frame->tick)) .name = StringF(frame->arena, "Screen target [%F]", FmtSint(frame->tick))
); );
Rng3 screen_viewport = RNG3(VEC3(0, 0, 0), VEC3(frame->screen_dims.x, frame->screen_dims.y, 1)); Rng3 screen_viewport = RNG3(VEC3(0, 0, 0), VEC3(frame->screen_dims.x, frame->screen_dims.y, 1));
@ -5133,20 +5127,20 @@ void V_TickForever(WaveLaneCtx *lane)
// Albedo texture // Albedo texture
frame->albedo = G_PushTexture2D( frame->albedo = G_PushTexture2D(
cl, gpu_frame_arena, cl, gpu_frame_arena,
G_TextureLayout_Family,
G_Format_R16G16B16A16_Float, G_Format_R16G16B16A16_Float,
frame->screen_dims, frame->screen_dims,
G_TextureLayout_Family, .flags = G_MemoryFlag_AllowTextureDraw,
.flags = G_ResourceFlag_AllowRenderTarget,
.name = StringF(frame->arena, "Albedo target [%F]", FmtSint(frame->tick)) .name = StringF(frame->arena, "Albedo target [%F]", FmtSint(frame->tick))
); );
// Backdrop texture // Backdrop texture
frame->backdrop_chain = G_PushTexture2D( frame->backdrop_chain = G_PushTexture2D(
cl, gpu_frame_arena, cl, gpu_frame_arena,
G_Format_R16G16B16A16_Float,
G_DimsFromMip2D(G_Count2D(screen_target), 1),
G_TextureLayout_Family, G_TextureLayout_Family,
.flags = G_TextureFlag_AllowRW, G_Format_R16G16B16A16_Float,
G_DimsFromMip2D(G_Count2D(frame->screen), 1),
.flags = G_MemoryFlag_AllowTextureRW,
.name = StringF(frame->arena, "Backdrop target [%F]", FmtSint(frame->tick)), .name = StringF(frame->arena, "Backdrop target [%F]", FmtSint(frame->tick)),
.max_mips = 4 .max_mips = 4
); );
@ -5155,10 +5149,10 @@ void V_TickForever(WaveLaneCtx *lane)
// TODO: We can re-use backdrop mip chain for this // TODO: We can re-use backdrop mip chain for this
frame->bloom_chain = G_PushTexture2D( frame->bloom_chain = G_PushTexture2D(
cl, gpu_frame_arena, cl, gpu_frame_arena,
G_Format_R16G16B16A16_Float,
G_DimsFromMip2D(G_Count2D(screen_target), 1),
G_TextureLayout_Family, G_TextureLayout_Family,
.flags = G_TextureFlag_AllowRW, G_Format_R16G16B16A16_Float,
G_DimsFromMip2D(G_Count2D(frame->screen), 1),
.flags = G_MemoryFlag_AllowTextureRW,
.name = StringF(frame->arena, "Bloom target [%F]", FmtSint(frame->tick)), .name = StringF(frame->arena, "Bloom target [%F]", FmtSint(frame->tick)),
.max_mips = G_MaxMips .max_mips = G_MaxMips
); );
@ -5169,28 +5163,35 @@ void V_TickForever(WaveLaneCtx *lane)
G_TextureLayout_Family, G_TextureLayout_Family,
G_Format_R16G16B16A16_Float, G_Format_R16G16B16A16_Float,
frame->shade_dims, frame->shade_dims,
.flags = G_TextureFlag_AllowRW, .flags = G_MemoryFlag_AllowTextureRW,
.name = StringF(frame->arena, "Shade target [%F]", FmtSint(frame->tick)) .name = StringF(frame->arena, "Shade target [%F]", FmtSint(frame->tick))
); );
Rng3 shade_viewport = RNG3(VEC3(0, 0, 0), VEC3(frame->shade_dims.x, frame->shade_dims.y, 1)); Rng3 shade_viewport = RNG3(VEC3(0, 0, 0), VEC3(frame->shade_dims.x, frame->shade_dims.y, 1));
Rng2 shade_scissor = RNG2(VEC2(shade_viewport.p0.x, shade_viewport.p0.y), VEC2(shade_viewport.p1.x, shade_viewport.p1.y)); Rng2 shade_scissor = RNG2(VEC2(shade_viewport.p0.x, shade_viewport.p0.y), VEC2(shade_viewport.p1.x, shade_viewport.p1.y));
// Quad buffers // Quad buffers
frame->quads = G_PushBufferFromCpuArena( u64 quads_count = ArenaCount(frame->quads_arena, V_Quad);
V_Quad *quads = ArenaFirst(frame->quads_arena, V_Quad);
frame->quads = G_PushStructsFromCpu(
cl, gpu_frame_arena, cl, gpu_frame_arena,
frame->quads_arena, V_GpuQuad, quads, quads_count,
.name = StringF(frame->arena, "quads [%F]", FmtSint(frame->tick)) .name = StringF(frame->arena, "quads [%F]", FmtSint(frame->tick))
); );
// Debug shape buffers // Debug verts
frame->dverts = G_PushBufferFromCpuArena( u64 dverts_count = ArenaCount(frame->dverts_arena, V_DVert);
V_DVert *dverts = ArenaFirst(frame->dverts_arena, V_DVert);
frame->dverts = G_PushStructsFromCpu(
cl, gpu_frame_arena, cl, gpu_frame_arena,
frame->dverts_arena, V_DVert, dverts, dverts_count,
.name = StringF(frame->arena, "dverts [%F]", FmtSint(frame->tick)) .name = StringF(frame->arena, "dverts [%F]", FmtSint(frame->tick))
); );
frame->dvert_idxs = G_PushBufferFromCpu(
// Debug vert indices
frame->dvert_idxs.count = ArenaCount(frame->dvert_idxs_arena, u32);
frame->dvert_idxs.buffer = G_PushStructsFromCpu(
cl, gpu_frame_arena, cl, gpu_frame_arena,
frame->dvert_idxs_arena, u32, ArenaFirst(frame->dvert_idxs_arena, u32), frame->dvert_idxs.count,
.name = StringF(frame->arena, "dvert idxs [%F]", FmtSint(frame->tick)) .name = StringF(frame->arena, "dvert idxs [%F]", FmtSint(frame->tick))
); );
@ -5207,18 +5208,17 @@ void V_TickForever(WaveLaneCtx *lane)
++emitter_idx; ++emitter_idx;
} }
} }
frame->emitters = G_PushBufferFromCpu( frame->emitters = G_PushStructsFromCpu(
cl, gpu_frame_arena, cl, gpu_frame_arena,
StringFromStructs(flattened_emitters, frame->emitters_count), flattened_emitters, frame->emitters_count,
.name = StringF(frame->arena, "emitters [%F]", FmtSint(frame->tick)) .name = StringF(frame->arena, "emitters [%F]", FmtSint(frame->tick))
); );
} }
frame->emitters = G_PushStructuredBufferRef(gpu_frame_arena, gpu_emitters, V_Emitter);
// Upload gpu frame // Upload gpu frame
G_BufferRef gpu_frame = G_PushBufferFromCpu( G_BufferRef gpu_frame = G_PushStructFromCpu(
cl, gpu_frame_arena, cl, gpu_frame_arena,
StringFromStruct(&frame->shared_frame), &frame->shared_frame,
.name = StringF(frame->arena, "Gpu frame [%F]", FmtSint(frame->tick)) .name = StringF(frame->arena, "Gpu frame [%F]", FmtSint(frame->tick))
); );
@ -5226,7 +5226,7 @@ void V_TickForever(WaveLaneCtx *lane)
V_GpuFlag gpu_flags = V_GpuFlag_None; V_GpuFlag gpu_flags = V_GpuFlag_None;
G_SetConstant(cl, V_GpuConst_Flags, gpu_flags); G_SetConstant(cl, V_GpuConst_Flags, gpu_flags);
G_SetConstant(cl, V_GpuConst_Frame, gpu_frame); G_SetConstant(cl, V_GpuConst_Frame, gpu_frame);
G_SetConstant(cl, V_GpuConst_NoiseTex, G_BasicNoiseTexture()); G_SetConstant(cl, V_GpuConst_NoiseTex, G_BasicNoise3D());
// Sync // Sync
G_Sync(cl); G_Sync(cl);
@ -5251,13 +5251,13 @@ void V_TickForever(WaveLaneCtx *lane)
// Backdrop passes // Backdrop passes
{ {
i32 mips_count = G_CountMips(backdrop_target); i32 mips_count = G_CountMips(frame->backdrop_chain);
G_SetConstant(cl, V_GpuConst_MipsCount, mips_count); G_SetConstant(cl, V_GpuConst_MipsCount, mips_count);
//- Downsample //- Downsample
for (i32 mip_idx = 0; mip_idx < mips_count; ++mip_idx) for (i32 mip_idx = 0; mip_idx < mips_count; ++mip_idx)
{ {
Vec2I32 down_dims = G_DimsFromMip2D(G_Count2D(backdrop_target), mip_idx); Vec2I32 down_dims = G_DimsFromMip2D(G_Count2D(frame->backdrop_chain), mip_idx);
G_SetConstant(cl, V_GpuConst_MipIdx, mip_idx); G_SetConstant(cl, V_GpuConst_MipIdx, mip_idx);
G_Compute2D(cl, V_BackdropDownCS, down_dims); G_Compute2D(cl, V_BackdropDownCS, down_dims);
@ -5268,7 +5268,7 @@ void V_TickForever(WaveLaneCtx *lane)
//- Upsample //- Upsample
for (i32 mip_idx = mips_count - 2; mip_idx >= 0; --mip_idx) for (i32 mip_idx = mips_count - 2; mip_idx >= 0; --mip_idx)
{ {
Vec2I32 up_dims = G_DimsFromMip2D(G_Count2D(backdrop_target), mip_idx); Vec2I32 up_dims = G_DimsFromMip2D(G_Count2D(frame->backdrop_chain), mip_idx);
G_SetConstant(cl, V_GpuConst_MipIdx, mip_idx); G_SetConstant(cl, V_GpuConst_MipIdx, mip_idx);
G_Compute2D(cl, V_BackdropUpCS, up_dims); G_Compute2D(cl, V_BackdropUpCS, up_dims);
@ -5291,8 +5291,8 @@ void V_TickForever(WaveLaneCtx *lane)
G_Draw( G_Draw(
cl, cl,
V_QuadVS, V_QuadPS, V_QuadVS, V_QuadPS,
G_CountBuffer(quads_buff, V_Quad), G_QuadIndices(), quads_count, G_QuadIndices(),
1, &G_Rt(frame->albedo, G_BlendMode_CompositeStraightAlpha), 1, &G_RT(frame->albedo, G_BlendMode_CompositeStraightAlpha),
screen_viewport, screen_scissor, screen_viewport, screen_scissor,
G_DrawMode_TriangleList G_DrawMode_TriangleList
); );
@ -5344,7 +5344,7 @@ void V_TickForever(WaveLaneCtx *lane)
//- Bloom passes //- Bloom passes
{ {
i32 mips_count = G_CountMips(bloom_target) + 1; i32 mips_count = G_CountMips(frame->bloom_chain) + 1;
G_SetConstant(cl, V_GpuConst_MipsCount, mips_count); G_SetConstant(cl, V_GpuConst_MipsCount, mips_count);
// NOTE: Because bloom mip chain starts at half screen size, mip_idx 0 // NOTE: Because bloom mip chain starts at half screen size, mip_idx 0
@ -5355,7 +5355,7 @@ void V_TickForever(WaveLaneCtx *lane)
G_ZoneDF(cl, "Bloom down") G_ZoneDF(cl, "Bloom down")
for (i32 mip_idx = 1; mip_idx < mips_count; ++mip_idx) for (i32 mip_idx = 1; mip_idx < mips_count; ++mip_idx)
{ {
Vec2I32 down_dims = G_DimsFromMip2D(G_Count2D(screen_target), mip_idx); Vec2I32 down_dims = G_DimsFromMip2D(G_Count2D(frame->screen), mip_idx);
G_SetConstant(cl, V_GpuConst_MipIdx, mip_idx); G_SetConstant(cl, V_GpuConst_MipIdx, mip_idx);
G_Compute2D(cl, V_BloomDownCS, down_dims); G_Compute2D(cl, V_BloomDownCS, down_dims);
@ -5367,7 +5367,7 @@ void V_TickForever(WaveLaneCtx *lane)
G_ZoneDF(cl, "Bloom up") G_ZoneDF(cl, "Bloom up")
for (i32 mip_idx = mips_count - 2; mip_idx >= 0; --mip_idx) for (i32 mip_idx = mips_count - 2; mip_idx >= 0; --mip_idx)
{ {
Vec2I32 up_dims = G_DimsFromMip2D(G_Count2D(screen_target), mip_idx); Vec2I32 up_dims = G_DimsFromMip2D(G_Count2D(frame->screen), mip_idx);
G_SetConstant(cl, V_GpuConst_MipIdx, mip_idx); G_SetConstant(cl, V_GpuConst_MipIdx, mip_idx);
G_Compute2D(cl, V_BloomUpCS, up_dims); G_Compute2D(cl, V_BloomUpCS, up_dims);
@ -5395,7 +5395,7 @@ void V_TickForever(WaveLaneCtx *lane)
cl, cl,
V_DVertVS, V_DVertPS, V_DVertVS, V_DVertPS,
1, frame->dvert_idxs, 1, frame->dvert_idxs,
1, &G_Rt(frame->screen, G_BlendMode_CompositeStraightAlpha), 1, &G_RT(frame->screen, G_BlendMode_CompositeStraightAlpha),
screen_viewport, screen_scissor, screen_viewport, screen_scissor,
G_DrawMode_TriangleList G_DrawMode_TriangleList
); );

View File

@ -3,9 +3,8 @@
f32 V_RandFromPos(Vec3 pos) f32 V_RandFromPos(Vec3 pos)
{ {
Texture3D<u32> noise3d = G_SDeref<u32>(V_GpuConst_NoiseTex); Texture3D<u32> noise3d = G_UniformDeref3D<u32>(V_GpuConst_NoiseTex);
// TODO: Compile-time noise dims u32 noise = noise3d[(Vec3U32)pos % G_BasicNoiseDims];
u32 noise = noise3d[(Vec3U32)pos % countof(noise3d)];
f32 rand = Norm16(noise); f32 rand = Norm16(noise);
return rand; return rand;
} }
@ -58,10 +57,10 @@ Vec4 V_ColorFromParticle(V_ParticleDesc desc, u32 particle_idx, u32 density)
//- Prepare shade //- Prepare shade
ComputeShader(V_PrepareShadeCS) ComputeShader(V_PrepareShadeCS)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
RWTexture2D<Vec4> shade = G_SDerefRW<Vec4>(frame.shade); RWTexture2D<Vec4> shade = G_UniformDerefRW2D<Vec4>(frame.shade);
Vec2 shade_pos = SV_DispatchThreadID + 0.5; Vec2 shade_pos = SV_DispatchThreadID + 0.5;
if (all(shade_pos < countof(shade))) if (all(shade_pos < G_Count2D(shade)))
{ {
// Clear shade // Clear shade
shade[shade_pos] = 0; shade[shade_pos] = 0;
@ -71,12 +70,12 @@ ComputeShader(V_PrepareShadeCS)
//- Prepare cells //- Prepare cells
ComputeShader(V_PrepareCellsCS) ComputeShader(V_PrepareCellsCS)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
Texture2D<P_TileKind> tiles = G_SDeref<P_TileKind>(frame.tiles); Texture2D<P_TileKind> tiles = G_UniformDeref2D<P_TileKind>(frame.tiles);
RWTexture2D<Vec4> stains = G_SDerefRW<Vec4>(frame.stains); RWTexture2D<Vec4> stains = G_UniformDerefRW2D<Vec4>(frame.stains);
RWTexture2D<Vec4> dry_stains = G_SDerefRW<Vec4>(frame.dry_stains); RWTexture2D<Vec4> dry_stains = G_UniformDerefRW2D<Vec4>(frame.dry_stains);
RWTexture2D<f32> drynesses = G_SDerefRW<f32>(frame.drynesses); RWTexture2D<f32> drynesses = G_UniformDerefRW2D<f32>(frame.drynesses);
RWTexture2D<u32> occluders = G_SDerefRW<u32>(frame.occluders); RWTexture2D<u32> occluders = G_UniformDerefRW2D<u32>(frame.occluders);
Vec2 cell_pos = SV_DispatchThreadID + 0.5; Vec2 cell_pos = SV_DispatchThreadID + 0.5;
if (all(cell_pos < P_WorldCellsDims)) if (all(cell_pos < P_WorldCellsDims))
@ -102,8 +101,8 @@ ComputeShader(V_PrepareCellsCS)
Vec4 over_dry_stain = 0; Vec4 over_dry_stain = 0;
for (V_ParticleLayer layer = (V_ParticleLayer)0; layer < V_ParticleLayer_COUNT; layer += (V_ParticleLayer)1) for (V_ParticleLayer layer = (V_ParticleLayer)0; layer < V_ParticleLayer_COUNT; layer += (V_ParticleLayer)1)
{ {
RWTexture2D<u32> cells = G_VDerefRW<u32>(frame.particle_cells[layer]); RWTexture2D<u32> cells = G_DynamicDerefRW2D<u32>(frame.particle_cells[layer]);
RWTexture2D<u32> densities = G_VDerefRW<u32>(frame.particle_densities[layer]); RWTexture2D<u32> densities = G_DynamicDerefRW2D<u32>(frame.particle_densities[layer]);
u32 packed = cells[cell_pos]; u32 packed = cells[cell_pos];
if (packed & (1 << 31)) if (packed & (1 << 31))
{ {
@ -160,8 +159,8 @@ ComputeShader(V_PrepareCellsCS)
//- Clear particles //- Clear particles
ComputeShader(V_ClearParticlesCS) ComputeShader(V_ClearParticlesCS)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
RWStructuredBuffer<V_Particle> particles = G_SDerefRW<V_Particle>(frame.particles); RWStructuredBuffer<V_Particle> particles = G_UniformDerefRW<V_Particle>(frame.particles);
u32 particle_idx = SV_DispatchThreadID; u32 particle_idx = SV_DispatchThreadID;
if (particle_idx < V_ParticlesCap) if (particle_idx < V_ParticlesCap)
{ {
@ -180,22 +179,16 @@ ComputeShader(V_BackdropDownCS)
i32 mip_idx = V_GpuConst_MipIdx; i32 mip_idx = V_GpuConst_MipIdx;
b32 is_first_pass = mip_idx == 0; b32 is_first_pass = mip_idx == 0;
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
SamplerState sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearMirror]); SamplerState sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearMirror]);
Texture2D<Vec4> bd_up = (
Texture2D<Vec4> bd_up; is_first_pass ?
if (is_first_pass) G_UniformDeref2D<Vec4>(frame.backdrop_src) :
{ G_UniformDeref2D<Vec4>(frame.backdrop_chain, mip_idx - 1)
bd_up = G_SDeref<Vec4>(frame.backdrop_src); );
} RWTexture2D<Vec4> bd_down = G_UniformDerefRW2D<Vec4>(frame.backdrop_chain, mip_idx);
else
{
bd_up = G_SDeref<Vec4>(frame.backdrop_mips[mip_idx - 1]);
}
RWTexture2D<Vec4> bd_down = G_SDerefRW<Vec4>(frame.backdrop_mips[mip_idx]);
Vec2 down_dims = countof(bd_down);
Vec2 down_dims = G_Count2D(bd_down);
Vec2 bd_pos = SV_DispatchThreadID + 0.5; Vec2 bd_pos = SV_DispatchThreadID + 0.5;
Vec2 bd_uv = bd_pos / down_dims; Vec2 bd_uv = bd_pos / down_dims;
Vec2 off_uv = 0.5 / down_dims; Vec2 off_uv = 0.5 / down_dims;
@ -245,13 +238,13 @@ ComputeShader(V_BackdropUpCS)
{ {
i32 mip_idx = V_GpuConst_MipIdx; i32 mip_idx = V_GpuConst_MipIdx;
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
Texture2D<Vec4> bd_down = G_SDeref<Vec4>(frame.backdrop_mips[mip_idx + 1]); Texture2D<Vec4> bd_down = G_UniformDeref2D<Vec4>(frame.backdrop_chain, mip_idx + 1);
RWTexture2D<Vec4> bd_up = G_SDerefRW<Vec4>(frame.backdrop_mips[mip_idx]); RWTexture2D<Vec4> bd_up = G_UniformDerefRW2D<Vec4>(frame.backdrop_chain, mip_idx);
SamplerState sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearMirror]); SamplerState sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearMirror]);
Vec2 down_dims = countof(bd_down); Vec2 down_dims = G_Count2D(bd_down);
Vec2 up_dims = countof(bd_up); Vec2 up_dims = G_Count2D(bd_up);
Vec2 bd_pos = SV_DispatchThreadID + 0.5; Vec2 bd_pos = SV_DispatchThreadID + 0.5;
Vec2 bd_uv = bd_pos / up_dims; Vec2 bd_uv = bd_pos / up_dims;
@ -303,8 +296,8 @@ ComputeShader(V_BackdropUpCS)
VertexShader(V_QuadVS, V_QuadPSInput) VertexShader(V_QuadVS, V_QuadPSInput)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
StructuredBuffer<V_Quad> quads = G_SDeref<V_Quad>(frame.quads); StructuredBuffer<V_Quad> quads = G_UniformDeref<V_Quad>(frame.quads);
V_Quad quad = quads[SV_InstanceID]; V_Quad quad = quads[SV_InstanceID];
@ -327,12 +320,12 @@ VertexShader(V_QuadVS, V_QuadPSInput)
PixelShader(V_QuadPS, V_QuadPSOutput, V_QuadPSInput input) PixelShader(V_QuadPS, V_QuadPSOutput, V_QuadPSInput input)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
SamplerState sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_PointClamp]); SamplerState sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_PointClamp]);
RWTexture2D<u32> occluders = G_SDerefRW<u32>(frame.occluders); RWTexture2D<u32> occluders = G_UniformDerefRW2D<u32>(frame.occluders);
V_Quad quad = input.quad; V_Quad quad = input.quad;
Texture2D<Vec4> tex = G_VDeref<Vec4>(quad.tex); Texture2D<Vec4> tex = G_DynamicDeref2D<Vec4>(quad.tex);
Vec2 world_pos = input.world_pos; Vec2 world_pos = input.world_pos;
Vec2 cell_pos = mul(frame.af.world_to_cell, Vec3(world_pos, 1)); Vec2 cell_pos = mul(frame.af.world_to_cell, Vec3(world_pos, 1));
@ -363,9 +356,9 @@ PixelShader(V_QuadPS, V_QuadPSOutput, V_QuadPSInput input)
ComputeShader(V_EmitParticlesCS) ComputeShader(V_EmitParticlesCS)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
StructuredBuffer<V_Emitter> emitters = G_SDeref<V_Emitter>(frame.emitters); StructuredBuffer<V_Emitter> emitters = G_UniformDeref<V_Emitter>(frame.emitters);
RWStructuredBuffer<V_Particle> particles = G_SDerefRW<V_Particle>(frame.particles); RWStructuredBuffer<V_Particle> particles = G_UniformDerefRW<V_Particle>(frame.particles);
u32 emitter_idx = SV_DispatchThreadID; u32 emitter_idx = SV_DispatchThreadID;
if (emitter_idx < frame.emitters_count) if (emitter_idx < frame.emitters_count)
@ -394,10 +387,10 @@ ComputeShader(V_EmitParticlesCS)
ComputeShader(V_SimParticlesCS) ComputeShader(V_SimParticlesCS)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
Texture2D<P_TileKind> tiles = G_SDeref<P_TileKind>(frame.tiles); Texture2D<P_TileKind> tiles = G_UniformDeref2D<P_TileKind>(frame.tiles);
RWStructuredBuffer<V_Particle> particles = G_SDerefRW<V_Particle>(frame.particles); RWStructuredBuffer<V_Particle> particles = G_UniformDerefRW<V_Particle>(frame.particles);
Texture2D<u32> occluders = G_SDeref<u32>(frame.occluders); Texture2D<u32> occluders = G_UniformDeref2D<u32>(frame.occluders);
u32 particle_idx = SV_DispatchThreadID; u32 particle_idx = SV_DispatchThreadID;
if (particle_idx < V_ParticlesCap) if (particle_idx < V_ParticlesCap)
@ -422,7 +415,7 @@ ComputeShader(V_SimParticlesCS)
if (particle.kind < 0) if (particle.kind < 0)
{ {
u32 emitter_idx = -particle.kind - 1; u32 emitter_idx = -particle.kind - 1;
V_Emitter emitter = G_SDeref<V_Emitter>(frame.emitters)[emitter_idx]; V_Emitter emitter = G_UniformDeref<V_Emitter>(frame.emitters)[emitter_idx];
f32 initial_angle = lerp(emitter.angle.min, emitter.angle.max, rand_angle); f32 initial_angle = lerp(emitter.angle.min, emitter.angle.max, rand_angle);
f32 initial_speed = lerp(emitter.speed.min, emitter.speed.max, rand_speed); f32 initial_speed = lerp(emitter.speed.min, emitter.speed.max, rand_speed);
@ -437,8 +430,8 @@ ComputeShader(V_SimParticlesCS)
if (particle.kind > V_ParticleKind_None && particle.kind < V_ParticleKind_COUNT && !prune) if (particle.kind > V_ParticleKind_None && particle.kind < V_ParticleKind_COUNT && !prune)
{ {
V_ParticleDesc desc = V_DescFromParticleKind((V_ParticleKind)particle.kind); V_ParticleDesc desc = V_DescFromParticleKind((V_ParticleKind)particle.kind);
RWTexture2D<u32> cells = G_VDerefRW<u32>(frame.particle_cells[desc.layer]); RWTexture2D<u32> cells = G_DynamicDerefRW2D<u32>(frame.particle_cells[desc.layer]);
RWTexture2D<u32> densities = G_VDerefRW<u32>(frame.particle_densities[desc.layer]); RWTexture2D<u32> densities = G_DynamicDerefRW2D<u32>(frame.particle_densities[desc.layer]);
u32 packed = 0; u32 packed = 0;
packed |= (particle_idx & ((1 >> 24) - 1)) << 0; packed |= (particle_idx & ((1 >> 24) - 1)) << 0;
@ -671,12 +664,12 @@ ComputeShader(V_SimParticlesCS)
ComputeShader(V_ShadeCS) ComputeShader(V_ShadeCS)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
SamplerState sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_PointClamp]); SamplerState sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_PointClamp]);
Texture2D<P_TileKind> tiles = G_SDeref<P_TileKind>(frame.tiles); Texture2D<P_TileKind> tiles = G_UniformDeref2D<P_TileKind>(frame.tiles);
Texture2D<Vec4> albedo_tex = G_SDeref<Vec4>(frame.albedo); Texture2D<Vec4> albedo_tex = G_UniformDeref2D<Vec4>(frame.albedo);
RWTexture2D<Vec4> shade_tex = G_SDerefRW<Vec4>(frame.shade); RWTexture2D<Vec4> shade_tex = G_UniformDerefRW2D<Vec4>(frame.shade);
Texture2D<f32> drynesses = G_SDeref<f32>(frame.drynesses); Texture2D<f32> drynesses = G_UniformDeref2D<f32>(frame.drynesses);
Vec2 shade_pos = SV_DispatchThreadID + 0.5; Vec2 shade_pos = SV_DispatchThreadID + 0.5;
Vec2 world_pos = mul(frame.af.shade_to_world, Vec3(shade_pos, 1)); Vec2 world_pos = mul(frame.af.shade_to_world, Vec3(shade_pos, 1));
@ -696,7 +689,7 @@ ComputeShader(V_ShadeCS)
////////////////////////////// //////////////////////////////
//- Write result //- Write result
if (all(shade_pos < countof(shade_tex))) if (all(shade_pos < G_Count2D(shade_tex)))
{ {
shade_tex[shade_pos] = result; shade_tex[shade_pos] = result;
} }
@ -707,18 +700,18 @@ ComputeShader(V_ShadeCS)
ComputeShader(V_CompositeCS) ComputeShader(V_CompositeCS)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
// Texture2D<Vec4> shade_tex = G_SDeref<Vec4>(frame.shade); // Texture2D<Vec4> shade_tex = G_UniformDeref2D<Vec4>(frame.shade);
SamplerState point_sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_PointClamp]); SamplerState point_sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_PointClamp]);
SamplerState bilinear_sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearClamp]); SamplerState bilinear_sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearClamp]);
Texture2D<Vec4> albedo_tex = G_SDeref<Vec4>(frame.albedo); Texture2D<Vec4> albedo_tex = G_UniformDeref2D<Vec4>(frame.albedo);
RWTexture2D<Vec4> screen_tex = G_SDerefRW<Vec4>(frame.screen); RWTexture2D<Vec4> screen_tex = G_UniformDerefRW2D<Vec4>(frame.screen);
Texture2D<Vec4> stains = G_SDeref<Vec4>(frame.stains); Texture2D<Vec4> stains = G_UniformDeref2D<Vec4>(frame.stains);
Texture2D<Vec4> dry_stains = G_SDeref<Vec4>(frame.dry_stains); Texture2D<Vec4> dry_stains = G_UniformDeref2D<Vec4>(frame.dry_stains);
Texture2D<f32> drynesses = G_SDeref<f32>(frame.drynesses); Texture2D<f32> drynesses = G_UniformDeref2D<f32>(frame.drynesses);
Texture2D<P_TileKind> tiles = G_SDeref<P_TileKind>(frame.tiles); Texture2D<P_TileKind> tiles = G_UniformDeref2D<P_TileKind>(frame.tiles);
Texture2D<Vec4> backdrop = G_SDeref<Vec4>(frame.backdrop_mips[0]); Texture2D<Vec4> backdrop = G_UniformDeref2D<Vec4>(frame.backdrop_chain);
StructuredBuffer<V_Particle> particles = G_SDeref<V_Particle>(frame.particles); StructuredBuffer<V_Particle> particles = G_UniformDeref<V_Particle>(frame.particles);
Vec2 screen_pos = SV_DispatchThreadID + 0.5; Vec2 screen_pos = SV_DispatchThreadID + 0.5;
Vec2 world_pos = mul(frame.af.screen_to_world, Vec3(screen_pos, 1)); Vec2 world_pos = mul(frame.af.screen_to_world, Vec3(screen_pos, 1));
@ -826,7 +819,7 @@ ComputeShader(V_CompositeCS)
else if (tile != P_TileKind_Empty) else if (tile != P_TileKind_Empty)
{ {
V_TileDesc tile_desc = frame.tile_descs[tile]; V_TileDesc tile_desc = frame.tile_descs[tile];
Texture2D<Vec4> tile_tex = G_VDeref<Vec4>(tile_desc.tex); Texture2D<Vec4> tile_tex = G_DynamicDeref2D<Vec4>(tile_desc.tex);
Vec2 samp_t = clamp(frac(world_pos), 0.00001, 1.0 - 0.00001); Vec2 samp_t = clamp(frac(world_pos), 0.00001, 1.0 - 0.00001);
Vec2 samp_uv = lerp(tile_desc.tex_slice_uv.p0, tile_desc.tex_slice_uv.p1, samp_t); Vec2 samp_uv = lerp(tile_desc.tex_slice_uv.p0, tile_desc.tex_slice_uv.p1, samp_t);
tile_color = tile_tex.SampleLevel(point_sampler, samp_uv, 0); tile_color = tile_tex.SampleLevel(point_sampler, samp_uv, 0);
@ -855,8 +848,8 @@ ComputeShader(V_CompositeCS)
for (V_ParticleLayer layer = (V_ParticleLayer)0; layer < V_ParticleLayer_COUNT; layer += (V_ParticleLayer)1) for (V_ParticleLayer layer = (V_ParticleLayer)0; layer < V_ParticleLayer_COUNT; layer += (V_ParticleLayer)1)
{ {
Texture2D<u32> cells = G_SDeref<u32>(frame.particle_cells[layer]); Texture2D<u32> cells = G_UniformDeref2D<u32>(frame.particle_cells[layer]);
Texture2D<u32> densities = G_SDeref<u32>(frame.particle_densities[layer]); Texture2D<u32> densities = G_UniformDeref2D<u32>(frame.particle_densities[layer]);
u32 packed = cells[cell_pos]; u32 packed = cells[cell_pos];
V_ParticleKind particle_kind = (V_ParticleKind)((packed >> 24) & 0x7F); V_ParticleKind particle_kind = (V_ParticleKind)((packed >> 24) & 0x7F);
if (particle_kind != V_ParticleKind_None) if (particle_kind != V_ParticleKind_None)
@ -1109,24 +1102,19 @@ ComputeShader(V_CompositeCS)
ComputeShader(V_BloomDownCS) ComputeShader(V_BloomDownCS)
{ {
i32 mip_idx = V_GpuConst_MipIdx; i32 mip_idx = V_GpuConst_MipIdx;
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
SamplerState sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearClamp]);
RWTexture2D<Vec4> bloom_down = G_SDerefRW<Vec4>(frame.bloom_mips[mip_idx - 1]);
Texture2D<Vec4> bloom_up;
b32 is_first_pass = mip_idx == 1; b32 is_first_pass = mip_idx == 1;
if (is_first_pass)
{
bloom_up = G_SDeref<Vec4>(frame.screen);
}
else
{
bloom_up = G_SDeref<Vec4>(frame.bloom_mips[mip_idx - 2]);
}
Vec2 down_dims = countof(bloom_down); V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
SamplerState sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearClamp]);
RWTexture2D<Vec4> bloom_down = G_UniformDerefRW2D<Vec4>(frame.bloom_chain, mip_idx - 1);
Texture2D<Vec4> bloom_up = (
is_first_pass ?
G_UniformDeref2D<Vec4>(frame.screen) :
G_UniformDeref2D<Vec4>(frame.bloom_chain, mip_idx - 2)
);
Vec2 down_dims = G_Count2D(bloom_down);
Vec2 bloom_pos = SV_DispatchThreadID + 0.5; Vec2 bloom_pos = SV_DispatchThreadID + 0.5;
Vec2 bloom_uv = bloom_pos / down_dims; Vec2 bloom_uv = bloom_pos / down_dims;
Vec2 off_uv = 0.5 / down_dims; Vec2 off_uv = 0.5 / down_dims;
@ -1185,23 +1173,23 @@ ComputeShader(V_BloomUpCS)
{ {
i32 mip_idx = V_GpuConst_MipIdx; i32 mip_idx = V_GpuConst_MipIdx;
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
SamplerState sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearClamp]); SamplerState sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearClamp]);
Texture2D<Vec4> bloom_down = G_SDeref<Vec4>(frame.bloom_mips[mip_idx]); Texture2D<Vec4> bloom_down = G_UniformDeref2D<Vec4>(frame.bloom_chain, mip_idx);
b32 is_last_pass = mip_idx == 0; b32 is_last_pass = mip_idx == 0;
RWTexture2D<Vec4> bloom_up; RWTexture2D<Vec4> bloom_up;
if (is_last_pass) if (is_last_pass)
{ {
bloom_up = G_SDerefRW<Vec4>(frame.screen); bloom_up = G_UniformDerefRW2D<Vec4>(frame.screen);
} }
else else
{ {
bloom_up = G_SDerefRW<Vec4>(frame.bloom_mips[mip_idx - 1]); bloom_up = G_UniformDerefRW2D<Vec4>(frame.bloom_chain, mip_idx - 1);
} }
Vec2 down_dims = countof(bloom_down); Vec2 down_dims = G_Count2D(bloom_down);
Vec2 up_dims = countof(bloom_up); Vec2 up_dims = G_Count2D(bloom_up);
Vec2 bloom_pos = SV_DispatchThreadID + 0.5; Vec2 bloom_pos = SV_DispatchThreadID + 0.5;
Vec2 bloom_uv = bloom_pos / up_dims; Vec2 bloom_uv = bloom_pos / up_dims;
@ -1250,10 +1238,10 @@ ComputeShader(V_BloomUpCS)
ComputeShader(V_FinalizeCS) ComputeShader(V_FinalizeCS)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
SamplerState bilinear_sampler = G_SDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearClamp]); SamplerState bilinear_sampler = G_UniformDeref(frame.basic_samplers[G_BasicSamplerKind_BilinearClamp]);
Texture2D<Vec4> bloom_tex = G_SDeref<Vec4>(frame.bloom_mips[0]); Texture2D<Vec4> bloom_tex = G_UniformDeref2D<Vec4>(frame.bloom_chain, 0);
RWTexture2D<Vec4> screen_tex = G_SDerefRW<Vec4>(frame.screen); RWTexture2D<Vec4> screen_tex = G_UniformDerefRW2D<Vec4>(frame.screen);
Vec2 screen_pos = SV_DispatchThreadID + 0.5; Vec2 screen_pos = SV_DispatchThreadID + 0.5;
b32 is_in_screen = IsInside(screen_pos, frame.screen_dims); b32 is_in_screen = IsInside(screen_pos, frame.screen_dims);
@ -1283,8 +1271,8 @@ ComputeShader(V_FinalizeCS)
VertexShader(V_DVertVS, V_DVertPSInput) VertexShader(V_DVertVS, V_DVertPSInput)
{ {
V_SharedFrame frame = G_SDeref<V_SharedFrame>(V_GpuConst_Frame)[0]; V_SharedFrame frame = G_UniformDeref<V_SharedFrame>(V_GpuConst_Frame)[0];
StructuredBuffer<V_DVert> verts = G_SDeref<V_DVert>(frame.dverts); StructuredBuffer<V_DVert> verts = G_UniformDeref<V_DVert>(frame.dverts);
V_DVert vert = verts[SV_VertexID]; V_DVert vert = verts[SV_VertexID];

View File

@ -12,8 +12,8 @@ Enum(V_GpuFlag)
}; };
G_DeclConstant(V_GpuFlag, V_GpuConst_Flags, 0); G_DeclConstant(V_GpuFlag, V_GpuConst_Flags, 0);
G_DeclConstant(G_StructuredBufferRef, V_GpuConst_Frame, 1); G_DeclConstant(G_BufferRef, V_GpuConst_Frame, 1);
G_DeclConstant(G_Texture3DRef, V_GpuConst_NoiseTex, 2); G_DeclConstant(G_TextureRef, V_GpuConst_NoiseTex, 2);
G_DeclConstant(i32, V_GpuConst_MipsCount, 3); G_DeclConstant(i32, V_GpuConst_MipsCount, 3);
G_DeclConstant(i32, V_GpuConst_MipIdx, 4); G_DeclConstant(i32, V_GpuConst_MipIdx, 4);
@ -200,7 +200,7 @@ Struct(V_Quad)
V_QuadFlag flags; V_QuadFlag flags;
u32 occluder_id; u32 occluder_id;
Affine quad_uv_to_world_af; Affine quad_uv_to_world_af;
G_Texture2DRef tex; G_TextureRef tex;
Rng2 tex_slice_uv; Rng2 tex_slice_uv;
}; };
@ -218,7 +218,7 @@ Struct(V_DVert)
Struct(V_TileDesc) Struct(V_TileDesc)
{ {
G_Texture2DRef tex; G_TextureRef tex;
Rng2 tex_slice_uv; Rng2 tex_slice_uv;
}; };
@ -346,32 +346,33 @@ Struct(V_SharedFrame)
G_SamplerRef basic_samplers[G_BasicSamplerKind_COUNT]; G_SamplerRef basic_samplers[G_BasicSamplerKind_COUNT];
V_TileDesc tile_descs[P_TileKind_COUNT]; V_TileDesc tile_descs[P_TileKind_COUNT];
G_Texture2DRef tiles; G_TextureRef tiles;
f32 backdrop_parallax; f32 backdrop_parallax;
G_Texture2DRef backdrop_src; G_TextureRef backdrop_src;
G_Texture2DRef backdrop_chain; G_TextureRef backdrop_chain;
G_Texture2DRef screen; G_TextureRef screen;
G_Texture2DRef shade; G_TextureRef shade;
G_Texture2DRef albedo; G_TextureRef albedo;
G_Texture2DRef bloom_chain; G_TextureRef bloom_chain;
u32 emitters_count; u32 emitters_count;
G_StructuredBufferRef emitters; G_BufferRef emitters;
G_StructuredBufferRef particles; G_BufferRef particles;
G_Texture2DRef stains; G_TextureRef stains;
G_Texture2DRef dry_stains; G_TextureRef dry_stains;
G_Texture2DRef drynesses; G_TextureRef drynesses;
G_Texture2DRef occluders; G_TextureRef occluders;
G_Texture2DRef particle_cells[V_ParticleLayer_COUNT]; G_TextureRef particle_cells[V_ParticleLayer_COUNT];
G_Texture2DRef particle_densities[V_ParticleLayer_COUNT]; G_TextureRef particle_densities[V_ParticleLayer_COUNT];
G_StructuredBufferRef dverts; G_IndexBufferDesc dvert_idxs;
G_StructuredBufferRef quads; G_BufferRef dverts;
G_BufferRef quads;
}; };
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -5,23 +5,6 @@ SPR_Ctx SPR = Zi;
void SPR_Bootstrap(void) void SPR_Bootstrap(void)
{ {
G_ArenaHandle gpu_perm = G_PermArena();
G_CommandListHandle cl = G_PrepareCommandList(G_QueueKind_Direct);
{
G_ResourceHandle unready_tex_res = G_PushTexture2D(
gpu_perm, cl,
G_Format_R8G8B8A8_Unorm,
VEC2I32(8, 8),
G_Layout_Common,
.flags = G_ResourceFlag_ZeroMemory,
.name = Lit("Sprite unready texture")
);
SPR.unready_tex = G_PushTexture2DRef(gpu_perm, unready_tex_res);
SPR.unready_tex_dims = Vec2FromVec(G_Count2D(unready_tex_res));
}
G_CommitCommandList(cl);
G_QueueSync(G_QueueMask_Direct, G_QueueMask_All);
OnAsyncTick(SPR_TickAsync); OnAsyncTick(SPR_TickAsync);
} }
@ -344,9 +327,9 @@ SPR_Sprite SPR_SpriteFromSheetEx(SPR_SheetKey sheet_key, SPR_SpanKey span_key, i
} }
else else
{ {
result.tex_dims = SPR.unready_tex_dims; result.tex = G_Blank2D();
result.tex = SPR.unready_tex; result.tex_dims = Vec2FromVec(G_Count2D(result.tex));
result.tex_rect = RNG2(VEC2(0, 0), SPR.unready_tex_dims); result.tex_rect = RNG2(VEC2(0, 0), result.tex_dims);
} }
// Fill rays // Fill rays
@ -446,14 +429,13 @@ void SPR_TickAsync(WaveLaneCtx *lane, AsyncFrameLaneCtx *base_async_lane_frame)
atlas->dims = Vec2I32FromVec(slice_dims); atlas->dims = Vec2I32FromVec(slice_dims);
{ {
G_ArenaHandle gpu_perm = G_PermArena(); G_ArenaHandle gpu_perm = G_PermArena();
atlas->tex_res = G_PushTexture2D( atlas->tex = G_PushTexture2D(
gpu_perm, cl, cl, gpu_perm,
G_TextureLayout_Common,
G_Format_R8G8B8A8_Unorm_Srgb, G_Format_R8G8B8A8_Unorm_Srgb,
atlas->dims, atlas->dims,
G_Layout_Common,
.name = Lit("Isolated sprite texture") .name = Lit("Isolated sprite texture")
); );
atlas->tex = G_PushTexture2DRef(gpu_perm, atlas->tex_res);
} }
} }
else else
@ -471,14 +453,13 @@ void SPR_TickAsync(WaveLaneCtx *lane, AsyncFrameLaneCtx *base_async_lane_frame)
atlas->dims = VEC2I32(atlas_size, atlas_size); atlas->dims = VEC2I32(atlas_size, atlas_size);
{ {
G_ArenaHandle gpu_perm = G_PermArena(); G_ArenaHandle gpu_perm = G_PermArena();
atlas->tex_res = G_PushTexture2D( atlas->tex = G_PushTexture2D(
gpu_perm, cl, cl, gpu_perm,
G_TextureLayout_Simultaneous,
G_Format_R8G8B8A8_Unorm_Srgb, G_Format_R8G8B8A8_Unorm_Srgb,
atlas->dims, atlas->dims,
G_Layout_Simultaneous,
.name = StringF(perm, "Sprite atlas #%F", FmtSint(SPR.atlases_count)) .name = StringF(perm, "Sprite atlas #%F", FmtSint(SPR.atlases_count))
); );
atlas->tex = G_PushTexture2DRef(gpu_perm, atlas->tex_res);
} }
SllStackPush(SPR.first_atlas, atlas); SllStackPush(SPR.first_atlas, atlas);
++SPR.atlases_count; ++SPR.atlases_count;
@ -513,7 +494,7 @@ void SPR_TickAsync(WaveLaneCtx *lane, AsyncFrameLaneCtx *base_async_lane_frame)
// Copy to atlas // Copy to atlas
G_CopyCpuToTexture( G_CopyCpuToTexture(
cl, cl,
atlas->tex_res, VEC3I32(atlas_pos.x, atlas_pos.y, 0), atlas->tex, VEC3I32(atlas_pos.x, atlas_pos.y, 0),
composite.pixels, VEC3I32(slice_dims.x, slice_dims.y, 1), composite.pixels, VEC3I32(slice_dims.x, slice_dims.y, 1),
RNG3I32( RNG3I32(
VEC3I32(0, 0, 0), VEC3I32(0, 0, 0),

View File

@ -14,8 +14,7 @@ Struct(SPR_Atlas)
{ {
SPR_Atlas *next; SPR_Atlas *next;
Vec2I32 dims; Vec2I32 dims;
G_ResourceHandle tex_res; G_TextureRef tex;
G_Texture2DRef tex;
Vec2I32 cur_pos; Vec2I32 cur_pos;
i32 cur_row_height; i32 cur_row_height;
}; };
@ -65,7 +64,7 @@ Struct(SPR_Sprite)
SPR_Ray rays[SPR_RayKind_COUNT]; SPR_Ray rays[SPR_RayKind_COUNT];
Vec2 tex_dims; Vec2 tex_dims;
G_Texture2DRef tex; G_TextureRef tex;
Rng2 tex_rect; Rng2 tex_rect;
b32 matched; b32 matched;
@ -159,9 +158,6 @@ Struct(SPR_Ctx)
{ {
SPR_SheetBin sheet_bins[Kibi(16)]; SPR_SheetBin sheet_bins[Kibi(16)];
G_Texture2DRef unready_tex;
Vec2 unready_tex_dims;
i64 atlases_count; i64 atlases_count;
SPR_Atlas *first_atlas; SPR_Atlas *first_atlas;

View File

@ -14,8 +14,6 @@
@VertexShader UI_DRectVS @VertexShader UI_DRectVS
@PixelShader UI_DRectPS @PixelShader UI_DRectPS
@VertexShader UI_BlitVS
@PixelShader UI_BlitPS
////////////////////////////// //////////////////////////////
//- Api //- Api

View File

@ -513,7 +513,7 @@ UI_Key UI_BuildBoxEx(UI_Key semantic_key)
return key; return key;
} }
void UI_SetRawTexture(UI_Key key, G_Texture2DRef tex, Rng2 uv) void UI_SetRawTexture(UI_Key key, G_TextureRef tex, Rng2 uv)
{ {
UI_Frame *frame = UI_CurrentFrame(); UI_Frame *frame = UI_CurrentFrame();
UI_CmdNode *n = PushStruct(frame->arena, UI_CmdNode); UI_CmdNode *n = PushStruct(frame->arena, UI_CmdNode);
@ -1524,7 +1524,7 @@ void UI_EndFrame(UI_Frame *frame, i32 vsync)
////////////////////////////// //////////////////////////////
//- Render //- Render
G_ResourceHandle backbuffer = frame->window_frame.backbuffer; G_TextureRef draw_target = Zi;
{ {
////////////////////////////// //////////////////////////////
//- Build render data //- Build render data
@ -1703,45 +1703,41 @@ void UI_EndFrame(UI_Frame *frame, i32 vsync)
////////////////////////////// //////////////////////////////
//- Upload data to GPU //- Upload data to GPU
UI_GpuParams params = Zi;
// Target // Target
G_ResourceHandle draw_target = G_PushTexture2D( draw_target = G_PushTexture2D(
UI.gpu_frame_arena, UI.cl, UI.cl, UI.gpu_frame_arena,
G_TextureLayout_Family,
G_Format_R16G16B16A16_Float, G_Format_R16G16B16A16_Float,
monitor_size, monitor_size,
G_Layout_Exclusive, .flags = G_MemoryFlag_AllowTextureDraw,
.flags = G_ResourceFlag_AllowRenderTarget,
.name = Lit("UI draw target") .name = Lit("UI draw target")
); );
G_Texture2DRef draw_target_ro = G_PushTexture2DRef(UI.gpu_frame_arena, draw_target);
// Rects // Rects
UI_GpuRect *rects = ArenaFirst(frame->rects_arena, UI_GpuRect);
u64 rects_count = ArenaCount(frame->rects_arena, UI_GpuRect); u64 rects_count = ArenaCount(frame->rects_arena, UI_GpuRect);
G_ResourceHandle rects_buff = G_PushBufferFromCpuCopy( params.rects = G_PushStructsFromCpu(
UI.gpu_frame_arena, UI.cl, UI.cl, UI.gpu_frame_arena,
StringFromArena(frame->rects_arena), rects, rects_count,
.name = Lit("UI rects") .name = Lit("UI rects")
); );
G_StructuredBufferRef rects_ro = G_PushStructuredBufferRef(UI.gpu_frame_arena, rects_buff, UI_GpuRect);
// Params // Params
UI_GpuParams params = Zi; params.target = draw_target;
{
params.target_size = draw_size; params.target_size = draw_size;
params.target_ro = draw_target_ro;
params.rects = rects_ro;
params.sampler = G_BasicSamplerFromKind(G_BasicSamplerKind_PointClamp); params.sampler = G_BasicSamplerFromKind(G_BasicSamplerKind_PointClamp);
params.cursor_pos = frame->cursor_pos; params.cursor_pos = frame->cursor_pos;
params.aa = TweakFloat("UI anti-aliasing", 1, 0, 1); params.anti_aliasing = TweakFloat("UI anti-aliasing", 1, 0, 1);
} G_BufferRef params_buff = G_PushStructFromCpu(
G_ResourceHandle params_buff = G_PushBufferFromCpuCopy( UI.cl, UI.gpu_frame_arena,
UI.gpu_frame_arena, UI.cl, &params,
StringFromStruct(&params),
.name = Lit("UI gpu params") .name = Lit("UI gpu params")
); );
G_StructuredBufferRef params_ro = G_PushStructuredBufferRef(UI.gpu_frame_arena, params_buff, UI_GpuParams);
// Init constants // Init constants
G_SetConstant(UI.cl, UI_GpuConst_Params, params_ro); G_SetConstant(UI.cl, UI_GpuConst_Params, params_buff);
// Sync // Sync
G_Sync(UI.cl); G_Sync(UI.cl);
@ -1758,57 +1754,43 @@ void UI_EndFrame(UI_Frame *frame, i32 vsync)
//- Rect pass //- Rect pass
if (rects_count > 0)
{
// Render rects // Render rects
G_ZoneDF(UI.cl, "UI rects") G_ZoneDF(UI.cl, "UI rects")
{
G_Draw( G_Draw(
UI.cl, UI.cl,
UI_DRectVS, UI_DRectPS, UI_DRectVS, UI_DRectPS,
rects_count, G_QuadIndices(), rects_count, G_QuadIndices(),
1, &G_Rt(draw_target, G_BlendMode_CompositePremultipliedAlpha), 1, &G_RT(draw_target, G_BlendMode_CompositePremultipliedAlpha),
draw_viewport, draw_scissor, draw_viewport, draw_scissor,
G_DrawMode_TriangleList G_DrawMode_TriangleList
); );
}
// Render rect wireframes // Render rect wireframes
G_ZoneDF(UI.cl, "UI debug rects")
if (AnyBit(frame->frame_flags, UI_FrameFlag_Debug)) if (AnyBit(frame->frame_flags, UI_FrameFlag_Debug))
{
G_ZoneDF(UI.cl, "UI debug rects")
{ {
G_SetConstant(UI.cl, UI_GpuConst_DebugDraw, 1); G_SetConstant(UI.cl, UI_GpuConst_DebugDraw, 1);
G_Draw( G_Draw(
UI.cl, UI.cl,
UI_DRectVS, UI_DRectPS, UI_DRectVS, UI_DRectPS,
rects_count, G_QuadIndices(), rects_count, G_QuadIndices(),
1, &G_Rt(draw_target, G_BlendMode_CompositePremultipliedAlpha), 1, &G_RT(draw_target, G_BlendMode_CompositePremultipliedAlpha),
draw_viewport, draw_scissor, draw_viewport, draw_scissor,
G_DrawMode_WireTriangleList G_DrawMode_WireTriangleList
); );
G_SetConstant(UI.cl, UI_GpuConst_DebugDraw, 0);
} }
} }
//- Backbuffer blit pass
G_SyncLayout(UI.cl, backbuffer, G_Layout_Exclusive);
{
G_ZoneDF(UI.cl, "UI blit to backbuffer")
G_Draw(
UI.cl,
UI_BlitVS, UI_BlitPS,
1, G_QuadIndices(),
1, &G_Rt(backbuffer, G_BlendMode_Opaque),
monitor_viewport, monitor_scissor,
G_DrawMode_TriangleList
);
}
G_SyncLayout(UI.cl, backbuffer, G_Layout_Common);
} }
////////////////////////////// //////////////////////////////
//- End frame //- End frame
G_CommitCommandList(UI.cl); G_CommitCommandList(UI.cl);
WND_EndFrame(frame->window_frame, vsync); WND_EndFrame(frame->window_frame, VEC2I32(0, 0), draw_target, RNG2I32(VEC2I32(0, 0), draw_size), vsync);
EndScratch(scratch); EndScratch(scratch);
} }

View File

@ -124,7 +124,7 @@ Enum(UI_BoxFlag)
X(SpriteSheet, SPR_SheetKey) \ X(SpriteSheet, SPR_SheetKey) \
X(SpriteSpan, SPR_SpanKey) \ X(SpriteSpan, SPR_SpanKey) \
X(SpriteSeq, i64) \ X(SpriteSeq, i64) \
X(BackgroundTexture, G_Texture2DRef) \ X(BackgroundTexture, G_TextureRef) \
X(BackgroundTextureSliceUv, Rng2) \ X(BackgroundTextureSliceUv, Rng2) \
X(Misc, f64) \ X(Misc, f64) \
/* --------------------------------------------- */ \ /* --------------------------------------------- */ \
@ -273,7 +273,7 @@ Struct(UI_Cmd)
struct struct
{ {
UI_Key key; UI_Key key;
G_Texture2DRef tex; G_TextureRef tex;
Rng2 slice_uv; Rng2 slice_uv;
} set_raw_texture; } set_raw_texture;
}; };
@ -311,7 +311,7 @@ Struct(UI_Box)
//- Cmd data //- Cmd data
UI_BoxDesc desc; UI_BoxDesc desc;
G_Texture2DRef raw_texture; G_TextureRef raw_texture;
Rng2 raw_texture_slice_uv; Rng2 raw_texture_slice_uv;
GC_Run glyph_run; GC_Run glyph_run;
SPR_Sprite sprite; SPR_Sprite sprite;
@ -379,7 +379,6 @@ Struct(UI_Frame)
Arena *rects_arena; Arena *rects_arena;
WND_Frame window_frame; WND_Frame window_frame;
G_ResourceHandle backbuffer;
// Time // Time
i64 tick; i64 tick;
@ -514,7 +513,7 @@ UI_Style UI_PopStyle(UI_StyleDesc desc);
UI_Key UI_BuildBoxEx(UI_Key semantic_key); UI_Key UI_BuildBoxEx(UI_Key semantic_key);
#define UI_BuildBox() UI_BuildBoxEx(UI_NilKey) #define UI_BuildBox() UI_BuildBoxEx(UI_NilKey)
void UI_SetRawTexture(UI_Key key, G_Texture2DRef tex, Rng2 uv); void UI_SetRawTexture(UI_Key key, G_TextureRef tex, Rng2 uv);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Report //~ Report

View File

@ -6,8 +6,8 @@
VertexShader(UI_DRectVS, UI_DRectPSInput) VertexShader(UI_DRectVS, UI_DRectPSInput)
{ {
UI_GpuParams params = G_SDeref<UI_GpuParams>(UI_GpuConst_Params)[0]; UI_GpuParams params = G_UniformDeref<UI_GpuParams>(UI_GpuConst_Params)[0];
StructuredBuffer<UI_GpuRect> rects = G_SDeref<UI_GpuRect>(params.rects); StructuredBuffer<UI_GpuRect> rects = G_UniformDeref<UI_GpuRect>(params.rects);
UI_GpuRect rect = rects[SV_InstanceID]; UI_GpuRect rect = rects[SV_InstanceID];
Vec2 rect_uv = RectUvFromIdx(SV_VertexID); Vec2 rect_uv = RectUvFromIdx(SV_VertexID);
@ -33,8 +33,8 @@ VertexShader(UI_DRectVS, UI_DRectPSInput)
PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input) PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input)
{ {
UI_GpuParams params = G_SDeref<UI_GpuParams>(UI_GpuConst_Params)[0]; UI_GpuParams params = G_UniformDeref<UI_GpuParams>(UI_GpuConst_Params)[0];
SamplerState sampler = G_SDeref(params.sampler); SamplerState sampler = G_UniformDeref(params.sampler);
UI_GpuRect rect = input.rect; UI_GpuRect rect = input.rect;
Vec2 rect_uv = input.rect_uv; Vec2 rect_uv = input.rect_uv;
@ -73,7 +73,7 @@ PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input)
} }
else else
{ {
Texture2D<Vec4> tex = G_VDeref<Vec4>(rect.tex); Texture2D<Vec4> tex = G_DynamicDeref2D<Vec4>(rect.tex);
background_premul = tex.SampleLevel(sampler, input.tex_uv, 0); background_premul = tex.SampleLevel(sampler, input.tex_uv, 0);
background_premul.rgb *= background_premul.a; background_premul.rgb *= background_premul.a;
} }
@ -93,7 +93,7 @@ PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input)
//- Compute anti-aliased border-over-background color //- Compute anti-aliased border-over-background color
Vec4 composite_premul = 0; Vec4 composite_premul = 0;
{ {
f32 smoothness = rect_dist_fwidth * params.aa * 0.5; f32 smoothness = rect_dist_fwidth * params.anti_aliasing * 0.5;
f32 border_coverage = smoothstep(smoothness, -smoothness, border_dist); f32 border_coverage = smoothstep(smoothness, -smoothness, border_dist);
composite_premul = lerp(background_premul, border_premul, border_coverage); composite_premul = lerp(background_premul, border_premul, border_coverage);
} }
@ -109,36 +109,3 @@ PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input)
output.sv_target0 = result; output.sv_target0 = result;
return output; return output;
} }
////////////////////////////////////////////////////////////
//~ Blit
//////////////////////////////
//- Vertex shader
VertexShader(UI_BlitVS, UI_BlitPSInput)
{
Vec2 uv = RectUvFromIdx(SV_VertexID);
UI_BlitPSInput result;
result.sv_position = Vec4(NdcFromUv(uv).xy, 0, 1);
result.src_uv = uv;
return result;
}
//////////////////////////////
//- Pixel shader
PixelShader(UI_BlitPS, UI_BlitPSOutput, UI_BlitPSInput input)
{
UI_GpuParams params = G_SDeref<UI_GpuParams>(UI_GpuConst_Params)[0];
Texture2D<Vec4> tex = G_SDeref<Vec4>(params.target_ro);
SamplerState sampler = G_SDeref(params.sampler);
Vec2 uv = input.src_uv;
Vec4 result = tex.Sample(sampler, uv);
UI_BlitPSOutput output;
output.sv_target0 = result;
return output;
}

View File

@ -18,27 +18,9 @@ Struct(UI_DRectPSOutput)
Vec4 Semantic(sv_target0); Vec4 Semantic(sv_target0);
}; };
////////////////////////////////////////////////////////////
//~ Blit shader types
Struct(UI_BlitPSInput)
{
Vec4 Semantic(sv_position);
Vec2 Semantic(src_uv);
};
Struct(UI_BlitPSOutput)
{
Vec4 Semantic(sv_target0);
};
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Shaders //~ Shaders
//- Rects //- Rects
VertexShader(UI_DRectVS, UI_DRectPSInput); VertexShader(UI_DRectVS, UI_DRectPSInput);
PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input); PixelShader(UI_DRectPS, UI_DRectPSOutput, UI_DRectPSInput input);
//- Blit
VertexShader(UI_BlitVS, UI_BlitPSInput);
PixelShader(UI_BlitPS, UI_BlitPSOutput, UI_BlitPSInput input);

View File

@ -1,20 +1,20 @@
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Constant types //~ Constant types
G_DeclConstant(G_SamplerRef, UI_GpuConst_Params, 0); G_DeclConstant(G_BufferRef, UI_GpuConst_Params, 0);
G_DeclConstant(b32, UI_GpuConst_DebugDraw, 1); G_DeclConstant(b32, UI_GpuConst_DebugDraw, 1);
Struct(UI_GpuParams) Struct(UI_GpuParams)
{ {
f32 aa; f32 anti_aliasing;
Vec2I32 target_size; Vec2I32 target_size;
G_Texture2DRef target_ro; G_TextureRef target;
Vec2 cursor_pos; Vec2 cursor_pos;
G_SamplerRef rects; G_BufferRef rects;
G_SamplerStateRef sampler; G_SamplerRef sampler;
}; };
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
@ -24,7 +24,7 @@ Struct(UI_GpuRect)
{ {
Rng2 bounds; Rng2 bounds;
G_Texture2DRef tex; G_TextureRef tex;
Rng2 tex_slice_uv; Rng2 tex_slice_uv;
Vec4 tint_lin; Vec4 tint_lin;