Skip to content

Commit

Permalink
feat: add resource loader and scrach alloc
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Pollind <[email protected]>
  • Loading branch information
pollend committed Jan 30, 2025
1 parent 2f9cb63 commit 4a83073
Show file tree
Hide file tree
Showing 18 changed files with 869 additions and 460 deletions.
26 changes: 11 additions & 15 deletions source/ref_nri/r_frame_cmd_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include "stb_ds.h"
#include "qhash.h"



void FR_CmdResetAttachmentToBackbuffer( struct frame_cmd_buffer_s *cmd )
{
const NriTextureDesc *colorDesc = rsh.nri.coreI.GetTextureDesc( cmd->textureBuffers.colorTexture );
Expand Down Expand Up @@ -184,7 +186,7 @@ void FrameCmdBufferFree(struct frame_cmd_buffer_s* cmd) {
arrfree(cmd->freeTextures);
arrfree(cmd->freeBuffers);
arrfree(cmd->frameTemporaryDesc);
BlockBufferPoolFree( &rsh.nri, &cmd->uboBlockBuffer );
FreeRIScratchAlloc( &rsh.nri, &cmd->uboBlockBuffer );

memset( &cmd->uboSceneFrame, 0, sizeof( struct ubo_frame_instance_s ) );
memset( &cmd->uboSceneObject, 0, sizeof( struct ubo_frame_instance_s ) );
Expand All @@ -198,25 +200,19 @@ void FrameCmdBufferFree(struct frame_cmd_buffer_s* cmd) {
}
void ResetFrameCmdBuffer( struct nri_backend_s *backend, struct frame_cmd_buffer_s *cmd )
{
const uint32_t swapchainIndex = RISwapchainAcquireNextTexture( &rsh.device, &rsh.riSwapchain );
cmd->pogoAttachment[0] = &rsh.pogoAttachment[2 * swapchainIndex];
cmd->pogoAttachment[1] = &rsh.pogoAttachment[( 2 * swapchainIndex ) + 1];
cmd->colorAttachment = &rsh.colorAttachment[swapchainIndex];
cmd->depthAttachment = &rsh.depthAttachment[swapchainIndex];
cmd->textureBuffers = rsh.backBuffers[rsh.nri.swapChainI.AcquireNextSwapChainTexture( rsh.swapchain )];

for( size_t i = 0; i < arrlen( cmd->freeTextures ); i++ ) {
rsh.nri.coreI.DestroyTexture( cmd->freeTextures[i] );
}
for( size_t i = 0; i < arrlen( cmd->freeBuffers ); i++ ) {
rsh.nri.coreI.DestroyBuffer( cmd->freeBuffers[i] );
}
for( size_t i = 0; i < arrlen( cmd->freeMemory ); i++ ) {
rsh.nri.coreI.FreeMemory( cmd->freeMemory[i] );
}
for( size_t i = 0; i < arrlen( cmd->frameTemporaryDesc ); i++ ) {
backend->coreI.DestroyDescriptor( cmd->frameTemporaryDesc[i] );
}

// TODO: need to re-work this logic
arrsetlen( cmd->freeMemory, 0 );
arrsetlen( cmd->freeTextures, 0 );
arrsetlen( cmd->freeBuffers, 0 );
arrsetlen( cmd->frameTemporaryDesc, 0);
BlockBufferPoolReset( &cmd->uboBlockBuffer );
//RIResetScratchAlloc( &cmd->uboBlockBuffer );

memset( &cmd->uboSceneFrame, 0, sizeof( struct ubo_frame_instance_s ) );
memset( &cmd->uboSceneObject, 0, sizeof( struct ubo_frame_instance_s ) );
Expand Down
15 changes: 9 additions & 6 deletions source/ref_nri/r_frame_cmd_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct frame_cmd_save_attachment_s {
NriDescriptor const *depthAttachment;
};


// the serialized state of the pipeline
struct frame_cmd_state_s {
uint32_t dirty;
Expand Down Expand Up @@ -140,17 +141,20 @@ struct ubo_frame_instance_s {
};

struct frame_cmd_buffer_s {

union {
union {
#if(DEVICE_IMPL_VULKAN)
struct {
VkCommandPool pool;
VkCommandBuffer cmd;
} vk;
#endif
};
struct RICmdHandle_s command;

struct RIScratchAlloc_s* uboScratchAlloc;
struct RIDescriptor_s* colorAttachment;
struct RIDescriptor_s* depthAttachment;
struct RIDescriptor_s* pogoAttachment[2]; // for portals a pogo attachment is not provided
uint64_t frameCount; // this value is bound by NUMBER_FRAMES_FLIGHT

struct block_buffer_pool_s uboBlockBuffer;
struct frame_cmd_state_s state;
struct frame_tex_buffers_s textureBuffers;
Expand Down Expand Up @@ -178,7 +182,6 @@ struct frame_cmd_buffer_s {
int stackCmdBeingRendered;
};


struct frame_cmd_save_attachment_s R_CmdState_StashAttachment(struct frame_cmd_buffer_s* cmd);
void R_CmdState_RestoreAttachment(struct frame_cmd_buffer_s* cmd, const struct frame_cmd_save_attachment_s* stashed);

Expand Down
778 changes: 423 additions & 355 deletions source/ref_nri/r_frontend.c

Large diffs are not rendered by default.

35 changes: 22 additions & 13 deletions source/ref_nri/r_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../qcommon/mod_win.h"
#include "r_graphics.h"
#include "ri_resource_upload.h"
#include "ri_scratch_alloc.h"

typedef struct { char *name; void **funcPointer; } dllfunc_t;

Expand Down Expand Up @@ -317,25 +318,33 @@ typedef struct
struct RIDevice_s device;

uint64_t frameCount;
uint64_t swapchainCount;
uint64_t frameCmdBufferIndex;
NriCommandQueue* cmdQueue;
NriSwapChain* swapchain;
NriFence* frameFence;
struct frame_tex_buffers_s* backBuffers;
struct frame_cmd_buffer_s frameCmds[NUMBER_FRAMES_FLIGHT];

struct frame_cmd_buffer_s primaryCmd;

union {
#if ( DEVICE_IMPL_VULKAN )
struct {
VkImage depthImages[NUMBER_FRAMES_FLIGHT];
VkImage pogo[NUMBER_FRAMES_FLIGHT * 2];

struct RIDescriptor_s depthAttachment[NUMBER_FRAMES_FLIGHT];
struct RIDescriptor_s colorAttachment[NUMBER_FRAMES_FLIGHT];
struct RIDescriptor_s pogoAttachment[NUMBER_FRAMES_FLIGHT * 2];
VkSemaphore frameSemaphore;
VkImage depthImages[RI_MAX_SWAPCHAIN_IMAGES];
VkImage pogo[RI_MAX_SWAPCHAIN_IMAGES * 2];
struct {
VkCommandPool pool;
VkCommandBuffer primaryBuffer;
uint32_t numSecondary;
VkCommandBuffer secondary[NUMBER_SUBFRAMES_FLIGHT]; // secondary views
} frameSets[NUMBER_FRAMES_FLIGHT];
} vk;
#endif
};
struct RIScratchAlloc_s uboFrameScratchAlloc[NUMBER_FRAMES_FLIGHT];
struct RIDescriptor_s colorAttachment[RI_MAX_SWAPCHAIN_IMAGES];
struct RIDescriptor_s depthAttachment[RI_MAX_SWAPCHAIN_IMAGES];
struct RIDescriptor_s pogoAttachment[RI_MAX_SWAPCHAIN_IMAGES * 2];

byte_vec4_t customColors[NUM_CUSTOMCOLORS];
} r_shared_t;
Expand Down Expand Up @@ -985,10 +994,10 @@ typedef struct
extern mapconfig_t mapConfig;
extern refinst_t rn;

static inline struct frame_cmd_buffer_s *R_ActiveFrameCmd()
{
const uint32_t bufferedFrameIndex = rsh.swapchainCount % NUMBER_FRAMES_FLIGHT;
return &rsh.frameCmds[bufferedFrameIndex];
}
//static inline struct frame_cmd_buffer_s *R_ActiveFrameCmd()
//{
// const uint32_t bufferedFrameIndex = rsh.frameCmdBufferIndex % NUMBER_FRAMES_FLIGHT;
// return &rsh.frameCmds[bufferedFrameIndex];
//}

#endif // R_LOCAL_H
1 change: 1 addition & 0 deletions source/ref_nri/r_nri.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const static uint32_t UBOBlockerBufferSize = 256 * 128;
const static uint32_t UBOBlockerBufferAlignmentReq = 256;

#define NUMBER_FRAMES_FLIGHT 3
#define NUMBER_SUBFRAMES_FLIGHT 64
#define NUMBER_RESERVED_BACKBUFFERS 4
#define DESCRIPTOR_MAX_BINDINGS 32
#define MAX_COLOR_ATTACHMENTS 8
Expand Down
60 changes: 55 additions & 5 deletions source/ref_nri/r_program.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#include "../../gameshared/q_arch.h"
#include "qhash.h"
#include "vulkan/vulkan_core.h"

#define MAX_GLSL_PROGRAMS 1024
#define GLSL_PROGRAMS_HASH_SIZE 256
Expand Down Expand Up @@ -1037,6 +1038,53 @@ struct pipeline_hash_s *RP_ResolvePipeline( struct glsl_program_s *program, stru
if(pipeline->pipeline) {
return pipeline; // pipeline is present in slot
}
GPU_VULKAN_BLOCK( ( &rsh.renderer ), ( {
uint32_t numModules = 0;
VkShaderModule modules[4] = {};
VkPipelineShaderStageCreateInfo stageCreateInfo[4] = {};
VkGraphicsPipelineCreateInfo pipelineCreateInfo = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
pipelineCreateInfo.pStages = stageCreateInfo;

if( program->shaderBin[GLSL_STAGE_VERTEX].bin && program->shaderBin[GLSL_STAGE_FRAGMENT].bin ) {
pipelineCreateInfo.stageCount = 2;
const VkShaderModuleCreateInfo vertModuleCreateInfo = {
VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
NULL,
(VkShaderModuleCreateFlags)0,
(size_t)program->shaderBin[GLSL_STAGE_VERTEX].size,
(const uint32_t *)program->shaderBin[GLSL_STAGE_VERTEX].bin,
};
vkCreateShaderModule( rsh.device.vk.device, &vertModuleCreateInfo, NULL, &modules[numModules] );
stageCreateInfo[0] = (VkPipelineShaderStageCreateInfo){
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_VERTEX_BIT,
.module = modules[numModules]
};
numModules++;

const VkShaderModuleCreateInfo fragModuleCreateInfo = {
VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
NULL,
(VkShaderModuleCreateFlags)0,
(size_t)program->shaderBin[GLSL_STAGE_VERTEX].size,
(const uint32_t *)program->shaderBin[GLSL_STAGE_VERTEX].bin,
};
vkCreateShaderModule( rsh.device.vk.device, &fragModuleCreateInfo, NULL, &modules[numModules] );
stageCreateInfo[1] = (VkPipelineShaderStageCreateInfo){
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.stage = VK_SHADER_STAGE_FRAGMENT_BIT ,
.module = modules[numModules]
};
numModules++;
} else {
assert( false && "failed to resolve bin" );
}

for( size_t i = 0; i < numModules; i++ ) {
vkDestroyShaderModule( rsh.device.vk.device, modules[i], NULL );
}
}));

NriGraphicsPipelineDesc graphicsPipelineDesc = {0};
NriColorAttachmentDesc colorAttachmentDesc[MAX_COLOR_ATTACHMENTS] = {0};
for( size_t i = 0; i < def->numColorAttachments; i++ ) {
Expand Down Expand Up @@ -1093,6 +1141,8 @@ struct pipeline_hash_s *RP_ResolvePipeline( struct glsl_program_s *program, stru
} else {
assert(false && "failed to resolve bin");
}


NRI_ABORT_ON_FAILURE( rsh.nri.coreI.CreateGraphicsPipeline( rsh.nri.device, &graphicsPipelineDesc, &pipeline->pipeline ) );
rsh.nri.coreI.SetPipelineDebugName( pipeline->pipeline, program->name );

Expand All @@ -1107,7 +1157,7 @@ static NriDescriptorRangeDesc *__FindAndInsertNriDescriptorRange( const SpvRefle
return &( *ranges )[i];
}
}

const size_t insertIndex = arraddnindex( (*ranges), 1 );
memset( &( *ranges )[insertIndex], 0, sizeof( NriDescriptorRangeDesc ) );
return &( *ranges )[insertIndex];
Expand Down Expand Up @@ -1137,9 +1187,9 @@ void RP_BindDescriptorSets(struct frame_cmd_buffer_s* cmd, struct glsl_program_s
struct glsl_commit_slots_s {
struct glsl_descriptor_binding_s* binding;
const struct descriptor_reflection_s* reflection;
} slots[DESCRIPTOR_MAX_BINDINGS];
} commit[DESCRIPTOR_SET_MAX] = {0};
} slots[DESCRIPTOR_MAX_BINDINGS];
} commit[DESCRIPTOR_SET_MAX] = { 0 };

for(size_t i = 0; i < numDescriptorData; i++) {
if(!bindings[i].descriptor.descriptor)
continue;
Expand Down Expand Up @@ -1662,7 +1712,7 @@ void RP_Shutdown( void )
for( i = 0, program = r_glslprograms; i < r_numglslprograms; i++, program++ ) {
RF_DeleteProgram( program );
}

Trie_Destroy( glsl_cache_trie );
glsl_cache_trie = NULL;

Expand Down
8 changes: 8 additions & 0 deletions source/ref_nri/r_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@ struct glsl_program_s {
NriPipelineLayout *layout;
struct pipeline_hash_s {
uint64_t hash;

union {
#if ( DEVICE_IMPL_VULKAN )
struct {

} vk;
#endif
};
NriPipeline *pipeline;
} pipelines[PIPELINE_LAYOUT_HASH_SIZE];

Expand Down
17 changes: 13 additions & 4 deletions source/ref_nri/r_scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,10 @@ void __ColorCorrection_PostProcessing(const refdef_t* ref,struct frame_cmd_buffe
}


//TODO: remove frame only bound to primary backbuffer
void R_RenderScene(struct frame_cmd_buffer_s* frame, const refdef_t *fd )
{
assert(frame == &rsh.primaryCmd);
R_FlushTransitionBarrier(frame->cmd);

uint8_t pogoIndex = 0;
Expand Down Expand Up @@ -337,11 +339,16 @@ void R_RenderScene(struct frame_cmd_buffer_s* frame, const refdef_t *fd )
VectorCopy( fd->vieworg, rn.pvsOrigin );
VectorCopy( fd->vieworg, rn.lodOrigin );

if(numPostProcessing > 0) {
if( numPostProcessing > 0 ) {
GPU_VULKAN_BLOCK( ( &rsh.renderer ), ( {
vkCmdEndRendering(frame->vk.cmd);
} ) );

FR_BindPogoBufferAttachment(frame, &frame->textureBuffers.pogoBuffers[pogoIndex]);
} else {
FR_CmdResetAttachmentToBackbuffer(frame);
}
}
//else {
// FR_CmdResetAttachmentToBackbuffer(frame);
//}

FR_CmdSetViewportAll(frame, (NriViewport) {
.x = fd->x,
Expand Down Expand Up @@ -398,6 +405,8 @@ void R_RenderScene(struct frame_cmd_buffer_s* frame, const refdef_t *fd )
FR_CmdBeginRendering(frame);
postProcessingHandlers[numPostProcessing - 1](fd, frame, src->shaderDescriptor);
FR_CmdEndRendering(frame);


}
FR_CmdResetAttachmentToBackbuffer(frame);

Expand Down
4 changes: 4 additions & 0 deletions source/ref_nri/ri_block_buffer_alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

struct RIBlockBufferPool_s {

};
18 changes: 12 additions & 6 deletions source/ref_nri/ri_renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ int EnumerateRIAdapters( struct RIRenderer_s *renderer, struct RIPhysicalAdapter
R_VK_ADD_STRUCT( &features, &features11 );
R_VK_ADD_STRUCT( &features, &features12 );
R_VK_ADD_STRUCT( &features, &features13 );


VkPhysicalDevicePresentIdFeaturesKHR presentIdFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRESENT_ID_FEATURES_KHR };
if( __VK_SupportExtension( extensionProperties, extensionNum, qCToStrRef( VK_KHR_PRESENT_ID_EXTENSION_NAME ) ) ) {
R_VK_ADD_STRUCT( &features, &features13 );
}

VkPhysicalDeviceMemoryProperties memoryProperties = {};
vkGetPhysicalDeviceMemoryProperties( physicalDevice, &memoryProperties );
vkGetPhysicalDeviceProperties2( physicalDevice, &properties );
Expand All @@ -194,12 +199,13 @@ int EnumerateRIAdapters( struct RIRenderer_s *renderer, struct RIPhysicalAdapter
physicalAdapter->vk.apiVersion = properties.properties.apiVersion;

physicalAdapter->vk.isSwapChainSupported = __VK_SupportExtension(extensionProperties, extensionNum,qCToStrRef(VK_KHR_SWAPCHAIN_EXTENSION_NAME));

physicalAdapter->vk.isBufferDeviceAddressSupported = physicalAdapter->vk.apiVersion >= VK_API_VERSION_1_2 ||
__VK_SupportExtension(extensionProperties, extensionNum,qCToStrRef(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME));
physicalAdapter->vk.isBufferDeviceAddressSupported =__VK_SupportExtension(extensionProperties, extensionNum,qCToStrRef(VK_AMD_DEVICE_COHERENT_MEMORY_EXTENSION_NAME));

physicalAdapter->vk.isPresentIDSupported = presentIdFeatures.presentId;
physicalAdapter->vk.isBufferDeviceAddressSupported = physicalAdapter->vk.apiVersion >= VK_API_VERSION_1_2 ||
__VK_SupportExtension(extensionProperties, extensionNum,qCToStrRef(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME));
physicalAdapter->vk.isBufferDeviceAddressSupported =__VK_SupportExtension(extensionProperties, extensionNum,qCToStrRef(VK_AMD_DEVICE_COHERENT_MEMORY_EXTENSION_NAME));

const VkPhysicalDeviceLimits* limits = &properties.properties.limits;
const VkPhysicalDeviceLimits* limits = &properties.properties.limits;

physicalAdapter->viewportMaxNum = limits->maxViewports;
physicalAdapter->viewportBoundsRange[0] = limits->viewportBoundsRange[0];
Expand Down
1 change: 1 addition & 0 deletions source/ref_nri/ri_resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ VkResult RI_VK_InitImageView(struct RIDevice_s* dev,VkImageViewCreateInfo* info,
assert(dev->renderer->api == RI_DEVICE_API_VK);
desc->cookie = cookie++;
desc->vk.type = RI_DESCRIPTOR_BUFFER_VIEW;
desc->vk.image.handle = info->image;
return vkCreateImageView( dev->vk.device, info, NULL, &desc->vk.image.view);
}
VkResult RI_VK_InitSampler(struct RIDevice_s* dev, VkSamplerCreateInfo* info, struct RIDescriptor_s* desc) {
Expand Down
Loading

0 comments on commit 4a83073

Please sign in to comment.