Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed Dec 20, 2024
1 parent 02e7ac4 commit 384c355
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 119 deletions.
60 changes: 42 additions & 18 deletions extensions/pl_ecs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static plEntity pl_ecs_create_material (plComponentLibrary*, const cha
static plEntity pl_ecs_create_skin (plComponentLibrary*, const char* pcName, plSkinComponent**);
static plEntity pl_ecs_create_animation (plComponentLibrary*, const char* pcName, plAnimationComponent**);
static plEntity pl_ecs_create_animation_data (plComponentLibrary*, const char* pcName, plAnimationDataComponent**);
static plEntity pl_ecs_create_perspective_camera (plComponentLibrary*, const char* pcName, plVec3 tPos, float fYFov, float fAspect, float fNearZ, float fFarZ, plCameraComponent**);
static plEntity pl_ecs_create_perspective_camera (plComponentLibrary*, const char* pcName, plVec3 tPos, float fYFov, float fAspect, float fNearZ, float fFarZ, bool bReverseZ, plCameraComponent**);
static plEntity pl_ecs_create_orthographic_camera(plComponentLibrary*, const char* pcName, plVec3 tPos, float fWidth, float fHeight, float fNearZ, float fFarZ, plCameraComponent**);
static plEntity pl_ecs_create_directional_light (plComponentLibrary*, const char* pcName, plVec3 tDirection, plLightComponent**);
static plEntity pl_ecs_create_point_light (plComponentLibrary*, const char* pcName, plVec3 tPosition, plLightComponent**);
Expand Down Expand Up @@ -903,14 +903,14 @@ pl_ecs_create_animation_data(plComponentLibrary* ptLibrary, const char* pcName,
}

static plEntity
pl_ecs_create_perspective_camera(plComponentLibrary* ptLibrary, const char* pcName, plVec3 tPos, float fYFov, float fAspect, float fNearZ, float fFarZ, plCameraComponent** pptCompOut)
pl_ecs_create_perspective_camera(plComponentLibrary* ptLibrary, const char* pcName, plVec3 tPos, float fYFov, float fAspect, float fNearZ, float fFarZ, bool bReverseZ, plCameraComponent** pptCompOut)
{
pcName = pcName ? pcName : "unnamed camera";
pl_log_debug_f(gptLog, uLogChannelEcs, "created camera: '%s'", pcName);
plEntity tNewEntity = pl_ecs_create_tag(ptLibrary, pcName);

const plCameraComponent tCamera = {
.tType = PL_CAMERA_TYPE_PERSPECTIVE,
.tType = bReverseZ ? PL_CAMERA_TYPE_PERSPECTIVE_REVERSE_Z : PL_CAMERA_TYPE_PERSPECTIVE,
.tPos = tPos,
.fNearZ = fNearZ,
.fFarZ = fFarZ,
Expand Down Expand Up @@ -1584,22 +1584,46 @@ pl_camera_update(plCameraComponent* ptCamera)
ptCamera->tViewMat = pl_mul_mat4t(&tFlipXY, &ptCamera->tViewMat);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~update projection~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if(ptCamera->tType == PL_CAMERA_TYPE_PERSPECTIVE)
switch(ptCamera->tType)
{
const float fInvtanHalfFovy = 1.0f / tanf(ptCamera->fFieldOfView / 2.0f);
ptCamera->tProjMat.col[0].x = fInvtanHalfFovy / ptCamera->fAspectRatio;
ptCamera->tProjMat.col[1].y = fInvtanHalfFovy;
ptCamera->tProjMat.col[2].z = ptCamera->fNearZ / (ptCamera->fNearZ - ptCamera->fFarZ);
ptCamera->tProjMat.col[2].w = 1.0f;
ptCamera->tProjMat.col[3].z = -ptCamera->fNearZ * ptCamera->fFarZ / (ptCamera->fNearZ - ptCamera->fFarZ);
ptCamera->tProjMat.col[3].w = 0.0f;
}
else if(ptCamera->tType == PL_CAMERA_TYPE_ORTHOGRAPHIC)
{
ptCamera->tProjMat.col[0].x = 2.0f / ptCamera->fWidth;
ptCamera->tProjMat.col[1].y = 2.0f / ptCamera->fHeight;
ptCamera->tProjMat.col[2].z = 1 / (ptCamera->fFarZ - ptCamera->fNearZ);
ptCamera->tProjMat.col[3].w = 1.0f;
case PL_CAMERA_TYPE_PERSPECTIVE:
{
const float fInvtanHalfFovy = 1.0f / tanf(ptCamera->fFieldOfView / 2.0f);
ptCamera->tProjMat.col[0].x = fInvtanHalfFovy / ptCamera->fAspectRatio;
ptCamera->tProjMat.col[1].y = fInvtanHalfFovy;
ptCamera->tProjMat.col[2].z = ptCamera->fFarZ / (ptCamera->fFarZ - ptCamera->fNearZ);
ptCamera->tProjMat.col[2].w = 1.0f;
ptCamera->tProjMat.col[3].z = -ptCamera->fNearZ * ptCamera->fFarZ / (ptCamera->fFarZ - ptCamera->fNearZ);
ptCamera->tProjMat.col[3].w = 0.0f;
break;
}

case PL_CAMERA_TYPE_PERSPECTIVE_REVERSE_Z:
{
const float fInvtanHalfFovy = 1.0f / tanf(ptCamera->fFieldOfView / 2.0f);
ptCamera->tProjMat.col[0].x = fInvtanHalfFovy / ptCamera->fAspectRatio;
ptCamera->tProjMat.col[1].y = fInvtanHalfFovy;
ptCamera->tProjMat.col[2].z = ptCamera->fNearZ / (ptCamera->fNearZ - ptCamera->fFarZ);
ptCamera->tProjMat.col[2].w = 1.0f;
ptCamera->tProjMat.col[3].z = -ptCamera->fNearZ * ptCamera->fFarZ / (ptCamera->fNearZ - ptCamera->fFarZ);
ptCamera->tProjMat.col[3].w = 0.0f;
break;
}

case PL_CAMERA_TYPE_ORTHOGRAPHIC:
{
ptCamera->tProjMat.col[0].x = 2.0f / ptCamera->fWidth;
ptCamera->tProjMat.col[1].y = 2.0f / ptCamera->fHeight;
ptCamera->tProjMat.col[2].z = 1 / (ptCamera->fFarZ - ptCamera->fNearZ);
ptCamera->tProjMat.col[3].w = 1.0f;
break;
}

default:
{
PL_ASSERT(false && "Unknown camera component type");
break;
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions extensions/pl_ecs_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ typedef struct _plEcsI
plEntity (*create_skin) (plComponentLibrary*, const char* pcName, plSkinComponent**);
plEntity (*create_animation) (plComponentLibrary*, const char* pcName, plAnimationComponent**);
plEntity (*create_animation_data) (plComponentLibrary*, const char* pcName, plAnimationDataComponent**);
plEntity (*create_perspective_camera) (plComponentLibrary*, const char* pcName, plVec3 tPos, float fYFov, float fAspect, float fNearZ, float fFarZ, plCameraComponent**);
plEntity (*create_perspective_camera) (plComponentLibrary*, const char* pcName, plVec3 tPos, float fYFov, float fAspect, float fNearZ, float fFarZ, bool bReverseZ, plCameraComponent**);
plEntity (*create_orthographic_camera)(plComponentLibrary*, const char* pcName, plVec3 tPos, float fWidth, float fHeight, float fNearZ, float fFarZ, plCameraComponent**);
plEntity (*create_directional_light) (plComponentLibrary*, const char* pcName, plVec3 tDirection, plLightComponent**);
plEntity (*create_point_light) (plComponentLibrary*, const char* pcName, plVec3 tPosition, plLightComponent**);
Expand Down Expand Up @@ -231,7 +231,10 @@ enum _plBlendMode
enum _plCameraType
{
PL_CAMERA_TYPE_PERSPECTIVE,
PL_CAMERA_TYPE_ORTHOGRAPHIC
PL_CAMERA_TYPE_PERSPECTIVE_REVERSE_Z,
PL_CAMERA_TYPE_ORTHOGRAPHIC,

PL_CAMERA_TYPE_COUNT
};

enum _plAnimationMode
Expand Down Expand Up @@ -459,6 +462,7 @@ typedef struct _plLightComponent
float fRange;
plVec3 tPosition;
plVec3 tDirection;
uint32_t uShadowResolution; // 0 -> automatic
float afCascadeSplits[PL_MAX_SHADOW_CASCADES];
uint32_t uCascadeCount;
} plLightComponent;
Expand Down
8 changes: 5 additions & 3 deletions extensions/pl_graphics_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Index of this file:
#define PL_MAX_SUBPASSES 8
#define PL_MAX_TIMELINE_SEMAPHORES 8
#define PL_MAX_VERTEX_ATTRIBUTES 8
#define PL_MAX_VIEWPORTS 16

// macros
#define PL_DEFINE_HANDLE(x) \
Expand Down Expand Up @@ -885,8 +886,8 @@ typedef struct _plBufferImageCopy

typedef struct _plDrawArea
{
plRenderViewport tViewport;
plScissor tScissor;
plRenderViewport atViewports[PL_MAX_VIEWPORTS];
plScissor atScissors[PL_MAX_VIEWPORTS];
plDrawStream* ptDrawStream;
} plDrawArea;

Expand Down Expand Up @@ -1108,7 +1109,8 @@ enum _plDeviceCapability
PL_DEVICE_CAPABILITY_NONE = 0,
PL_DEVICE_CAPABILITY_SWAPCHAIN = 1 << 0,
PL_DEVICE_CAPABILITY_BIND_GROUP_INDEXING = 1 << 1,
PL_DEVICE_CAPABILITY_SAMPLER_ANISOTROPY = 1 << 2
PL_DEVICE_CAPABILITY_SAMPLER_ANISOTROPY = 1 << 2,
PL_DEVICE_CAPABILITY_MULTIPLE_VIEWPORTS = 1 << 3
};

enum _plCommandPoolResetFlags
Expand Down
63 changes: 42 additions & 21 deletions extensions/pl_graphics_vulkan.c
Original file line number Diff line number Diff line change
Expand Up @@ -1955,28 +1955,44 @@ pl_draw_stream(plRenderEncoder* ptEncoder, uint32_t uAreaCount, plDrawArea *atAr
{
plDrawArea* ptArea = &atAreas[i];

const VkRect2D tScissor = {
.offset = {
.x = ptArea->tScissor.iOffsetX,
.y = ptArea->tScissor.iOffsetY
},
.extent = {
.width = ptArea->tScissor.uWidth,
.height = ptArea->tScissor.uHeight
VkRect2D atScissors[PL_MAX_VIEWPORTS] = {0};
VkViewport atViewports[PL_MAX_VIEWPORTS] = {0};

uint32_t uViewportCount = 0;

for(uint32_t j = 0; j < PL_MAX_VIEWPORTS; j++)
{

if(ptArea->atViewports[j].fWidth == 0.0f)
{
break;
}
};

const VkViewport tViewport = {
.x = ptArea->tViewport.fX,
.y = ptArea->tViewport.fY,
.width = ptArea->tViewport.fWidth,
.height = ptArea->tViewport.fHeight,
.minDepth = ptArea->tViewport.fMinDepth,
.maxDepth = ptArea->tViewport.fMaxDepth
};
atScissors[j] = (VkRect2D){
.offset = {
.x = ptArea->atScissors[j].iOffsetX,
.y = ptArea->atScissors[j].iOffsetY
},
.extent = {
.width = ptArea->atScissors[j].uWidth,
.height = ptArea->atScissors[j].uHeight
}
};

atViewports[j] = (VkViewport){
.x = ptArea->atViewports[j].fX,
.y = ptArea->atViewports[j].fY,
.width = ptArea->atViewports[j].fWidth,
.height = ptArea->atViewports[j].fHeight,
.minDepth = ptArea->atViewports[j].fMinDepth,
.maxDepth = ptArea->atViewports[j].fMaxDepth
};

uViewportCount++;
}

vkCmdSetViewport(ptCmdBuffer->tCmdBuffer, 0, 1, &tViewport);
vkCmdSetScissor(ptCmdBuffer->tCmdBuffer, 0, 1, &tScissor);
vkCmdSetViewport(ptCmdBuffer->tCmdBuffer, 0, uViewportCount, atViewports);
vkCmdSetScissor(ptCmdBuffer->tCmdBuffer, 0, uViewportCount, atScissors);

plDrawStream* ptStream = ptArea->ptDrawStream;

Expand Down Expand Up @@ -2120,7 +2136,7 @@ void
pl_set_depth_bias(plRenderEncoder* ptEncoder, float fDepthBiasConstantFactor, float fDepthBiasClamp, float fDepthBiasSlopeFactor)
{
plCommandBuffer* ptCmdBuffer = ptEncoder->ptCommandBuffer;
vkCmdSetDepthBias(ptCmdBuffer->tCmdBuffer, fDepthBiasConstantFactor, 0.0f, fDepthBiasSlopeFactor);
vkCmdSetDepthBias(ptCmdBuffer->tCmdBuffer, fDepthBiasConstantFactor, fDepthBiasClamp, fDepthBiasSlopeFactor);
}

void
Expand Down Expand Up @@ -2273,7 +2289,7 @@ pl_initialize_graphics(const plGraphicsInit* ptDesc)
if (ptDesc->tFlags & PL_GRAPHICS_INIT_FLAGS_SWAPCHAIN_ENABLED)
{
apcExtensions[uExtensionCount++] = VK_KHR_SURFACE_EXTENSION_NAME;

#ifdef _WIN32
apcExtensions[uExtensionCount++] = VK_KHR_WIN32_SURFACE_EXTENSION_NAME;
#elif defined(__ANDROID__)
Expand Down Expand Up @@ -2580,6 +2596,9 @@ pl_enumerate_devices(plDeviceInfo *atDeviceInfo, uint32_t* puDeviceCount)

if (tDeviceFeatures.samplerAnisotropy)
atDeviceInfo[i].tCapabilities |= PL_DEVICE_CAPABILITY_SAMPLER_ANISOTROPY;

if (tDeviceFeatures.multiViewport)
atDeviceInfo[i].tCapabilities |= PL_DEVICE_CAPABILITY_MULTIPLE_VIEWPORTS;
}
}

Expand Down Expand Up @@ -2693,6 +2712,8 @@ pl_create_device(const plDeviceInit* ptInit)
apcDeviceExts[uDeviceExtensionCount++] = "VK_KHR_portability_subset";
#endif

apcDeviceExts[uDeviceExtensionCount++] = VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME;

// get device features
VkPhysicalDeviceFeatures tDeviceFeatures = {0};
VkPhysicalDeviceFeatures2 tDeviceFeatures2 = {0};
Expand Down
38 changes: 22 additions & 16 deletions extensions/pl_renderer_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ pl_refr_initialize(plWindow* ptWindow)
gptData->tDefaultSampler = gptGfx->create_sampler(gptData->ptDevice, &tSamplerDesc);

const plSamplerDesc tShadowSamplerDesc = {
.tMagFilter = PL_FILTER_LINEAR,
.tMinFilter = PL_FILTER_LINEAR,
.tMagFilter = PL_FILTER_NEAREST,
.tMinFilter = PL_FILTER_NEAREST,
.fMinMip = 0.0f,
.fMaxMip = 1.0f,
.fMaxAnisotropy = 1.0f,
Expand Down Expand Up @@ -2425,7 +2425,7 @@ pl_refr_reload_scene_shaders(uint32_t uSceneHandle)
.pTempConstantData = aiConstantData0,
.tGraphicsState = {
.ulDepthWriteEnabled = 1,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
.ulDepthMode = PL_COMPARE_MODE_GREATER_OR_EQUAL,
.ulCullMode = PL_CULL_MODE_NONE,
.ulWireframe = 0,
.ulStencilMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -2645,7 +2645,7 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)
.pTempConstantData = aiConstantData0,
.tGraphicsState = {
.ulDepthWriteEnabled = 1,
.ulDepthMode = PL_COMPARE_MODE_LESS_OR_EQUAL,
.ulDepthMode = PL_COMPARE_MODE_GREATER_OR_EQUAL,
.ulCullMode = PL_CULL_MODE_NONE,
.ulWireframe = 0,
.ulStencilMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -2908,11 +2908,10 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)

gptGfx->update_bind_group(gptData->ptDevice, ptScene->tGlobalBindGroup, &tGlobalBindGroupData);

gptData->tShadowAtlasResolution.x = 1024.0f * 8.0f;
gptData->tShadowAtlasResolution.y = 1024.0f * 8.0f;
gptData->uShadowAtlasResolution = 1024 * 8;

const plTextureDesc tShadowDepthTextureDesc = {
.tDimensions = {gptData->tShadowAtlasResolution.x, gptData->tShadowAtlasResolution.y, 1},
.tDimensions = {(float)gptData->uShadowAtlasResolution, (float)gptData->uShadowAtlasResolution, 1},
.tFormat = PL_FORMAT_D32_FLOAT,
.uLayers = 1,
.uMips = 1,
Expand All @@ -2930,9 +2929,9 @@ pl_refr_finalize_scene(uint32_t uSceneHandle)
.tStencilStoreOp = PL_STORE_OP_DONT_CARE,
.tCurrentUsage = PL_TEXTURE_USAGE_SAMPLED,
.tNextUsage = PL_TEXTURE_USAGE_SAMPLED,
.fClearZ = 1.0f
.fClearZ = 0.0f
},
.tDimensions = {.x = gptData->tShadowAtlasResolution.x, .y = gptData->tShadowAtlasResolution.y}
.tDimensions = {.x = (float)gptData->uShadowAtlasResolution, .y = (float)gptData->uShadowAtlasResolution}
};

plRenderPassAttachments atShadowAttachmentSets[PL_MAX_FRAMES_IN_FLIGHT] = {0};
Expand Down Expand Up @@ -3221,14 +3220,20 @@ pl_refr_render_scene(uint32_t uSceneHandle, uint32_t uViewHandle, plViewOptions

plDrawArea tArea = {
.ptDrawStream = ptStream,
.tScissor = {
.uWidth = (uint32_t)tDimensions.x,
.uHeight = (uint32_t)tDimensions.y,
.atScissors =
{
{
.uWidth = (uint32_t)tDimensions.x,
.uHeight = (uint32_t)tDimensions.y,
}
},
.tViewport = {
.fWidth = tDimensions.x,
.fHeight = tDimensions.y,
.fMaxDepth = 1.0f
.atViewports =
{
{
.fWidth = tDimensions.x,
.fHeight = tDimensions.y,
.fMaxDepth = 1.0f
}
}
};

Expand Down Expand Up @@ -4282,6 +4287,7 @@ pl_load_renderer_ext(plApiRegistryI* ptApiRegistry, bool bReload)
gptProfile = pl_get_api_latest(ptApiRegistry, plProfileI);
gptLog = pl_get_api_latest(ptApiRegistry, plLogI);

gptRect = pl_get_api_latest(ptApiRegistry, plRectPackI);
gptECS = pl_get_api_latest(ptApiRegistry, plEcsI);
gptCamera = pl_get_api_latest(ptApiRegistry, plCameraI);
gptDraw = pl_get_api_latest(ptApiRegistry, plDrawI);
Expand Down
Loading

0 comments on commit 384c355

Please sign in to comment.