Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Jan 17, 2025
1 parent 8ba4e66 commit f8b1903
Show file tree
Hide file tree
Showing 24 changed files with 732 additions and 704 deletions.
4 changes: 3 additions & 1 deletion extensions/pl_debug_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Index of this file:
// core
#include <float.h>
#include "pl.h"
#include "pl_ds.h"

#include "pl_debug_ext.h"
#define PL_MATH_INCLUDE_FUNCTIONS
#include "pl_math.h"
Expand Down Expand Up @@ -103,6 +103,8 @@ static plDebugContext* gptDebugCtx = NULL;

#endif

#include "pl_ds.h"

//-----------------------------------------------------------------------------
// [SECTION] internal api
//-----------------------------------------------------------------------------
Expand Down
26 changes: 13 additions & 13 deletions extensions/pl_draw_backend_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Index of this file:
#include <float.h>
#include "pl.h"
#include "pl_memory.h"
#include "pl_ds.h"
#define PL_MATH_INCLUDE_FUNCTIONS
#include "pl_math.h"

Expand All @@ -33,8 +32,21 @@ Index of this file:

#ifdef PL_UNITY_BUILD
#include "pl_unity_ext.inc"
#else
static const plMemoryI* gptMemory = NULL;
#define PL_ALLOC(x) gptMemory->tracked_realloc(NULL, (x), __FILE__, __LINE__)
#define PL_REALLOC(x, y) gptMemory->tracked_realloc((x), (y), __FILE__, __LINE__)
#define PL_FREE(x) gptMemory->tracked_realloc((x), 0, __FILE__, __LINE__)

#ifndef PL_DS_ALLOC
#define PL_DS_ALLOC(x) gptMemory->tracked_realloc(NULL, (x), __FILE__, __LINE__)
#define PL_DS_ALLOC_INDIRECT(x, FILE, LINE) gptMemory->tracked_realloc(NULL, (x), FILE, LINE)
#define PL_DS_FREE(x) gptMemory->tracked_realloc((x), 0, __FILE__, __LINE__)
#endif
#endif

#include "pl_ds.h"

//-----------------------------------------------------------------------------
// [SECTION] internal structs
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -85,18 +97,6 @@ typedef struct _plDrawBackendContext

static plDrawBackendContext* gptDrawBackendCtx = NULL;

#ifndef PL_UNITY_BUILD
static const plMemoryI* gptMemory = NULL;
#define PL_ALLOC(x) gptMemory->tracked_realloc(NULL, (x), __FILE__, __LINE__)
#define PL_REALLOC(x, y) gptMemory->tracked_realloc((x), (y), __FILE__, __LINE__)
#define PL_FREE(x) gptMemory->tracked_realloc((x), 0, __FILE__, __LINE__)

static const plGraphicsI* gptGfx = NULL;
static const plStatsI* gptStats = NULL;
static const plDrawI* gptDraw = NULL;
static const plShaderI* gptShader = NULL;
#endif

//-----------------------------------------------------------------------------
// [SECTION] internal api
//-----------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ Index of this file:
#define PL_MATH_INCLUDE_FUNCTIONS
#include "pl.h"
#include "pl_ecs_ext.h"
#include "pl_ds.h"
#include "pl_math.h"

// extensions
Expand Down Expand Up @@ -51,6 +50,8 @@ Index of this file:
static const plLogI* gptLog = NULL;
#endif

#include "pl_ds.h"

//-----------------------------------------------------------------------------
// [SECTION] structs
//-----------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion extensions/pl_gpu_allocators_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Index of this file:
#include "pl_graphics_ext.h"
#define PL_MATH_INCLUDE_FUNCTIONS
#include "pl_math.h"
#include "pl_ds.h"

#ifdef PL_UNITY_BUILD
#include "pl_unity_ext.inc"
Expand All @@ -40,6 +39,8 @@ Index of this file:
static const plDataRegistryI* gptDataRegistry = NULL;
#endif

#include "pl_ds.h"

//-----------------------------------------------------------------------------
// [SECTION] defines
//-----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions extensions/pl_graphics_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ pl_load_graphics_ext(plApiRegistryI* ptApiRegistry, bool bReload)
.present = pl_present,
.copy_buffer_to_texture = pl_copy_buffer_to_texture,
.copy_texture_to_buffer = pl_copy_texture_to_buffer,
.copy_texture = pl_copy_texture,
.generate_mipmaps = pl_generate_mipmaps,
.copy_buffer = pl_copy_buffer,
.signal_semaphore = pl_signal_semaphore,
Expand Down
32 changes: 30 additions & 2 deletions extensions/pl_graphics_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Index of this file:
// [SECTION] apis
//-----------------------------------------------------------------------------

#define plGraphicsI_version (plVersion){1, 2, 0}
#define plGraphicsI_version (plVersion){1, 2, 1}

//-----------------------------------------------------------------------------
// [SECTION] includes
Expand Down Expand Up @@ -95,6 +95,7 @@ typedef struct _plRenderEncoder plRenderEncoder; // opaque type for
typedef struct _plComputeEncoder plComputeEncoder; // opaque type for command buffer encoder for compute ops
typedef struct _plBlitEncoder plBlitEncoder; // opaque type for command buffer encoder for blit ops
typedef struct _plBufferImageCopy plBufferImageCopy; // used for copying between buffers & textures with blit encoder
typedef struct _plImageCopy plImageCopy; // used for copying between textures with blit encoder
typedef struct _plPassTextureResource plPassTextureResource;
typedef struct _plPassBufferResource plPassBufferResource;
typedef struct _plPassResources plPassResources;
Expand Down Expand Up @@ -299,6 +300,7 @@ typedef struct _plGraphicsI
void (*set_texture_usage) (plBlitEncoder*, plTextureHandle, plTextureUsage tNewUsage, plTextureUsage tOldUsage);
void (*copy_buffer_to_texture) (plBlitEncoder*, plBufferHandle, plTextureHandle, uint32_t regionCount, const plBufferImageCopy*);
void (*copy_texture_to_buffer) (plBlitEncoder*, plTextureHandle, plBufferHandle, uint32_t regionCount, const plBufferImageCopy*);
void (*copy_texture) (plBlitEncoder*, plTextureHandle, plTextureHandle, uint32_t regionCount, const plImageCopy*);
void (*generate_mipmaps) (plBlitEncoder*, plTextureHandle);
void (*copy_buffer) (plBlitEncoder*, plBufferHandle source, plBufferHandle destination, uint32_t sourceOffset, uint32_t destinationOffset, size_t);

Expand Down Expand Up @@ -556,8 +558,10 @@ typedef struct _plBindGroupUpdateBufferData
{
plBufferHandle tBuffer;
uint32_t uSlot;
size_t szOffset;
size_t szBufferRange;

// [INTERNAL]
size_t _szOffset; // vulkan only :(
} plBindGroupUpdateBufferData;

typedef struct _plBindGroupUpdateData
Expand Down Expand Up @@ -885,6 +889,30 @@ typedef struct _plBufferImageCopy
plTextureUsage tCurrentImageUsage;
} plBufferImageCopy;

typedef struct _plImageCopy
{
// source info
int iSourceOffsetX;
int iSourceOffsetY;
int iSourceOffsetZ;
uint32_t uSourceExtentX;
uint32_t uSourceExtentY;
uint32_t uSourceExtentZ;
uint32_t uSourceMipLevel;
uint32_t uSourceBaseArrayLayer;
uint32_t uSourceLayerCount;
plTextureUsage tSourceImageUsage;

// destination offset
int iDestinationOffsetX;
int iDestinationOffsetY;
int iDestinationOffsetZ;
uint32_t uDestinationMipLevel;
uint32_t uDestinationBaseArrayLayer;
uint32_t uDestinationLayerCount;
plTextureUsage tDestinationImageUsage;
} plImageCopy;

typedef struct _plDrawArea
{
plRenderViewport atViewports[PL_MAX_VIEWPORTS];
Expand Down
111 changes: 110 additions & 1 deletion extensions/pl_graphics_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,115 @@ pl_copy_texture_to_buffer(plBlitEncoder* ptEncoder, plTextureHandle tTextureHand
pl_temp_allocator_reset(&gptGraphics->tTempAllocator);
}

void
pl_copy_texture(plBlitEncoder* ptEncoder, plTextureHandle tSrcHandle, plTextureHandle tDstHandle, uint32_t uRegionCount, const plImageCopy* ptRegions)
{
plCommandBuffer* ptCmdBuffer = ptEncoder->ptCommandBuffer;
plDevice* ptDevice = ptCmdBuffer->ptDevice;

// allocate temporary memory
VkImageCopy* atCopyRegions = pl_temp_allocator_alloc(&gptGraphics->tTempAllocator, sizeof(VkImageCopy) * uRegionCount);
memset(atCopyRegions, 0, sizeof(VkImageCopy) * uRegionCount);

VkImageSubresourceRange* atSubResourceRanges = pl_temp_allocator_alloc(&gptGraphics->tTempAllocator, sizeof(VkImageSubresourceRange) * uRegionCount);
memset(atSubResourceRanges, 0, sizeof(VkImageSubresourceRange) * uRegionCount * 2);

// setup copy regions
plTexture* ptColdSrcTexture = pl__get_texture(ptDevice, tSrcHandle);
plTexture* ptColdDstTexture = pl__get_texture(ptDevice, tDstHandle);

uint32_t uCurrentSrcBaseArrayLevel = UINT32_MAX;
uint32_t uCurrentSrcMipLevel = UINT32_MAX;

uint32_t uCurrentDestBaseArrayLevel = UINT32_MAX;
uint32_t uCurrentDestMipLevel = UINT32_MAX;

for (uint32_t i = 0; i < uRegionCount; i++)
{

atSubResourceRanges[i].aspectMask = ptColdSrcTexture->tDesc.tUsage & PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
atSubResourceRanges[i].baseMipLevel = ptRegions[i].uSourceMipLevel;
atSubResourceRanges[i].levelCount = ptRegions[i].uSourceLayerCount;
atSubResourceRanges[i].baseArrayLayer = ptRegions[i].uSourceBaseArrayLayer;
atSubResourceRanges[i].layerCount = ptRegions[i].uSourceLayerCount;

atSubResourceRanges[i + uRegionCount].aspectMask = ptColdDstTexture->tDesc.tUsage & PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
atSubResourceRanges[i + uRegionCount].baseMipLevel = ptRegions[i].uDestinationMipLevel;
atSubResourceRanges[i + uRegionCount].levelCount = ptRegions[i].uDestinationLayerCount;
atSubResourceRanges[i + uRegionCount].baseArrayLayer = ptRegions[i].uDestinationBaseArrayLayer;
atSubResourceRanges[i + uRegionCount].layerCount = ptRegions[i].uDestinationLayerCount;


if(uCurrentSrcBaseArrayLevel != atSubResourceRanges[i].baseArrayLayer || uCurrentSrcMipLevel != atSubResourceRanges[i].baseMipLevel)
{
pl__transition_image_layout(ptCmdBuffer->tCmdBuffer, ptDevice->sbtTexturesHot[tSrcHandle.uIndex].tImage, pl__vulkan_layout(ptRegions[i].tSourceImageUsage), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, atSubResourceRanges[i], VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
uCurrentSrcBaseArrayLevel = atSubResourceRanges[i].baseArrayLayer;
uCurrentSrcMipLevel = atSubResourceRanges[i].baseMipLevel;
}

if(uCurrentDestBaseArrayLevel != atSubResourceRanges[i + uRegionCount].baseArrayLayer || uCurrentDestMipLevel != atSubResourceRanges[i + uRegionCount].baseMipLevel)
{
pl__transition_image_layout(ptCmdBuffer->tCmdBuffer, ptDevice->sbtTexturesHot[tDstHandle.uIndex].tImage, pl__vulkan_layout(ptRegions[i].tDestinationImageUsage), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, atSubResourceRanges[i + uRegionCount], VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
uCurrentDestBaseArrayLevel = atSubResourceRanges[i + uRegionCount].baseArrayLayer;
uCurrentDestMipLevel = atSubResourceRanges[i + uRegionCount].baseMipLevel;
}


atCopyRegions[i].srcOffset.x = ptRegions[i].iSourceOffsetX;
atCopyRegions[i].srcOffset.y = ptRegions[i].iSourceOffsetY;
atCopyRegions[i].srcOffset.z = ptRegions[i].iSourceOffsetZ;

atCopyRegions[i].extent.width = ptRegions[i].uSourceExtentX;
atCopyRegions[i].extent.height = ptRegions[i].uSourceExtentY;
atCopyRegions[i].extent.depth = ptRegions[i].uSourceExtentZ;

atCopyRegions[i].srcSubresource.baseArrayLayer = ptRegions[i].uSourceBaseArrayLayer;
atCopyRegions[i].srcSubresource.layerCount = ptRegions[i].uSourceLayerCount;
atCopyRegions[i].srcSubresource.mipLevel = ptRegions[i].uSourceMipLevel;
atCopyRegions[i].srcSubresource.aspectMask = ptColdSrcTexture->tDesc.tUsage & PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;

atCopyRegions[i].dstOffset.x = ptRegions[i].iDestinationOffsetX;
atCopyRegions[i].dstOffset.y = ptRegions[i].iDestinationOffsetY;
atCopyRegions[i].dstOffset.z = ptRegions[i].iDestinationOffsetZ;

atCopyRegions[i].dstSubresource.baseArrayLayer = ptRegions[i].uDestinationBaseArrayLayer;
atCopyRegions[i].dstSubresource.layerCount = ptRegions[i].uDestinationLayerCount;
atCopyRegions[i].dstSubresource.mipLevel = ptRegions[i].uDestinationMipLevel;
atCopyRegions[i].dstSubresource.aspectMask = ptColdDstTexture->tDesc.tUsage & PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT;
}

vkCmdCopyImage(ptCmdBuffer->tCmdBuffer,
ptDevice->sbtTexturesHot[tSrcHandle.uIndex].tImage,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
ptDevice->sbtTexturesHot[tDstHandle.uIndex].tImage,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
uRegionCount,
atCopyRegions);

uCurrentSrcBaseArrayLevel = UINT32_MAX;
uCurrentSrcMipLevel = UINT32_MAX;
uCurrentDestBaseArrayLevel = UINT32_MAX;
uCurrentDestMipLevel = UINT32_MAX;

for (uint32_t i = 0; i < uRegionCount; i++)
{
if(uCurrentSrcBaseArrayLevel != atSubResourceRanges[i].baseArrayLayer || uCurrentSrcMipLevel != atSubResourceRanges[i].baseMipLevel)
{
pl__transition_image_layout(ptCmdBuffer->tCmdBuffer, ptDevice->sbtTexturesHot[tSrcHandle.uIndex].tImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, pl__vulkan_layout(ptRegions[i].tSourceImageUsage), atSubResourceRanges[i], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
uCurrentSrcBaseArrayLevel = atSubResourceRanges[i].baseArrayLayer;
uCurrentSrcMipLevel = atSubResourceRanges[i].baseMipLevel;
}
if(uCurrentDestBaseArrayLevel != atSubResourceRanges[i + uRegionCount].baseArrayLayer || uCurrentDestMipLevel != atSubResourceRanges[i + uRegionCount].baseMipLevel)
{
pl__transition_image_layout(ptCmdBuffer->tCmdBuffer, ptDevice->sbtTexturesHot[tDstHandle.uIndex].tImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, pl__vulkan_layout(ptRegions[i].tDestinationImageUsage), atSubResourceRanges[i + uRegionCount], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
uCurrentDestBaseArrayLevel = atSubResourceRanges[i + uRegionCount].baseArrayLayer;
uCurrentDestMipLevel = atSubResourceRanges[i + uRegionCount].baseMipLevel;
}
}

pl_temp_allocator_reset(&gptGraphics->tTempAllocator);
}

void
pl_generate_mipmaps(plBlitEncoder* ptEncoder, plTextureHandle tTexture)
{
Expand Down Expand Up @@ -946,7 +1055,7 @@ pl_update_bind_group(plDevice* ptDevice, plBindGroupHandle tHandle, const plBind
const plVulkanBuffer* ptVulkanBuffer = &ptDevice->sbtBuffersHot[ptData->atBufferBindings[i].tBuffer.uIndex];

sbtBufferDescInfos[i].buffer = ptVulkanBuffer->tBuffer;
sbtBufferDescInfos[i].offset = ptData->atBufferBindings[i].szOffset;
sbtBufferDescInfos[i].offset = ptData->atBufferBindings[i]._szOffset;
sbtBufferDescInfos[i].range = ptData->atBufferBindings[i].szBufferRange == 0 ? VK_WHOLE_SIZE : ptData->atBufferBindings[i].szBufferRange;

sbtWrites[uCurrentWrite].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
Expand Down
3 changes: 2 additions & 1 deletion extensions/pl_model_loader_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Index of this file:
#include "pl.h"
#include "pl_model_loader_ext.h"
#include "pl_stl.h"
#include "pl_ds.h"
#include "pl_string.h"
#define PL_MATH_INCLUDE_FUNCTIONS
#include "pl_math.h"
Expand Down Expand Up @@ -49,6 +48,8 @@ Index of this file:
static const plFileI* gptFile = NULL;
#endif

#include "pl_ds.h"

// misc
#include "cgltf.h"

Expand Down
Loading

0 comments on commit f8b1903

Please sign in to comment.