117 lines
3.8 KiB
C
117 lines
3.8 KiB
C
GPU_SharedUtilState GPU_shared_util_state = ZI;
|
|
ThreadLocal GPU_ArenaHandle GPU_t_perm_arena = ZI;
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Startup
|
|
|
|
void GPU_StartupCommon(void)
|
|
{
|
|
GPU_SharedUtilState *g = &GPU_shared_util_state;
|
|
|
|
GPU_ArenaHandle gpu_perm = GPU_PermArena();
|
|
|
|
/* Init point sampler */
|
|
{
|
|
GPU_ResourceHandle pt_sampler = GPU_PushSampler(gpu_perm, (GPU_SamplerDesc) { .filter = GPU_Filter_MinMagMipPoint });
|
|
g->pt_sampler = GPU_PushSamplerStateHandle(gpu_perm, pt_sampler);
|
|
}
|
|
|
|
GPU_CommandListHandle cl = GPU_PrepareCommandList(GPU_QueueKind_Direct);
|
|
{
|
|
/* Init quad index buffer */
|
|
{
|
|
u16 quad_data[6] = { 0, 1, 2, 0, 2, 3 };
|
|
GPU_ResourceHandle quad_indices = GPU_PushBuffer(gpu_perm, u16, countof(quad_data));
|
|
GPU_CopyCpuToBuffer(cl, quad_indices, 0, quad_data, RNGU64(0, sizeof(quad_data)));
|
|
g->quad_indices.resource = quad_indices;
|
|
g->quad_indices.index_size = sizeof(quad_data[0]);
|
|
g->quad_indices.index_count = countof(quad_data);
|
|
}
|
|
|
|
/* TODO: Init noise texture */
|
|
{
|
|
String noise_data = DataFromResource(ResourceKeyFromStore(&GPU_Resources, Lit("noise_128x128x64_16.dat")));
|
|
Vec3I32 noise_dims = VEC3I32(128, 128, 64);
|
|
if (noise_data.len != noise_dims.x * noise_dims.y * noise_dims.z * 2)
|
|
{
|
|
Panic(Lit("Unexpected noise texture size"));
|
|
}
|
|
GPU_ResourceHandle noise_tex = GPU_PushTexture3D(gpu_perm,
|
|
GPU_Format_R16_Uint,
|
|
noise_dims,
|
|
GPU_Layout_AnyQueue_ShaderRead_CopyRead_CopyWrite_Present);
|
|
GPU_CopyCpuToTexture(cl,
|
|
noise_tex, VEC3I32(0, 0, 0),
|
|
noise_data.text, noise_dims,
|
|
RNG3I32(VEC3I32(0, 0, 0), noise_dims));
|
|
g->noise_tex = GPU_PushTexture3DHandle(gpu_perm, noise_tex);
|
|
}
|
|
}
|
|
GPU_CommitCommandList(cl);
|
|
|
|
GPU_SyncAllQueues(GPU_QueueKind_Direct);
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////
|
|
//~ Helpers
|
|
|
|
//- Arena
|
|
|
|
GPU_ArenaHandle GPU_PermArena(void)
|
|
{
|
|
GPU_ArenaHandle perm = GPU_t_perm_arena;
|
|
if (GPU_IsArenaNil(perm))
|
|
{
|
|
GPU_t_perm_arena = GPU_AcquireArena();
|
|
perm = GPU_t_perm_arena;
|
|
}
|
|
return perm;
|
|
}
|
|
|
|
//- Cpu -> Gpu copy
|
|
|
|
StructuredBufferHandle GPU_PushStructuredBufferFromCpu_(GPU_ArenaHandle gpu_arena, GPU_CommandListHandle cl, void *src, u32 element_size, u32 element_count)
|
|
{
|
|
u64 size = (u64)element_size * (u64)element_count;
|
|
GPU_ResourceHandle buffer = GPU_PushBufferEx(gpu_arena, (GPU_BufferDesc) { .size = size });
|
|
GPU_CopyCpuToBuffer(cl, buffer, 0, src, RNGU64(0, size));
|
|
StructuredBufferHandle structured_buffer = GPU_PushStructuredBufferHandleEx(gpu_arena, buffer, element_size, RNGU32(0, element_count));
|
|
GPU_MemorySync(
|
|
cl, buffer,
|
|
GPU_Stage_None, GPU_Access_None,
|
|
GPU_Stage_AllShading, GPU_Access_ShaderRead
|
|
);
|
|
return structured_buffer;
|
|
}
|
|
|
|
//- Viewport / scissor
|
|
|
|
Rng3 GPU_ViewportFromTexture(GPU_ResourceHandle texture)
|
|
{
|
|
Vec2I32 dims = GPU_Count2D(texture);
|
|
return RNG3(VEC3(0, 0, 0), VEC3(dims.x, dims.y, 1));
|
|
}
|
|
|
|
Rng2 GPU_ScissorFromTexture(GPU_ResourceHandle texture)
|
|
{
|
|
Vec2I32 dims = GPU_Count2D(texture);
|
|
return RNG2(VEC2(0, 0), VEC2(dims.x, dims.y));
|
|
}
|
|
|
|
//- Shared resources
|
|
|
|
SamplerStateHandle GPU_GetSharedPointSampler(void)
|
|
{
|
|
return GPU_shared_util_state.pt_sampler;
|
|
}
|
|
|
|
GPU_IndexBufferDesc GPU_GetSharedQuadIndices(void)
|
|
{
|
|
return GPU_shared_util_state.quad_indices;
|
|
}
|
|
|
|
Texture3DHandle GPU_GetSharedNoise(void)
|
|
{
|
|
return GPU_shared_util_state.noise_tex;
|
|
}
|