Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
WIP

WIP

WIP
  • Loading branch information
hoffstadt committed Dec 20, 2024
1 parent aba7453 commit 3105cf9
Show file tree
Hide file tree
Showing 21 changed files with 747 additions and 419 deletions.
2 changes: 1 addition & 1 deletion examples/example_8.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
.tStencilStoreOp = PL_STORE_OP_DONT_CARE,
.tCurrentUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT,
.tNextUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT,
.fClearZ = 0.0f
.fClearZ = 1.0f
},
.atColorTargets = {
{
Expand Down
2 changes: 1 addition & 1 deletion examples/example_9.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
.tStencilStoreOp = PL_STORE_OP_DONT_CARE,
.tCurrentUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT,
.tNextUsage = PL_TEXTURE_USAGE_DEPTH_STENCIL_ATTACHMENT,
.fClearZ = 0.0f
.fClearZ = 1.0f
},
.atColorTargets = {
{
Expand Down
4 changes: 2 additions & 2 deletions extensions/pl_draw_backend_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ pl__get_3d_pipeline(plRenderPassHandle tRenderPass, uint32_t uMSAASampleCount, p
.tVertexShader = gptShader->load_glsl("../shaders/draw_3d.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = tFlags & PL_DRAW_FLAG_DEPTH_WRITE ? 1 : 0,
.ulDepthMode = tFlags & PL_DRAW_FLAG_DEPTH_TEST ? PL_COMPARE_MODE_GREATER : PL_COMPARE_MODE_ALWAYS,
.ulDepthMode = tFlags & PL_DRAW_FLAG_DEPTH_TEST ? (tFlags & PL_DRAW_FLAG_REVERSE_Z_DEPTH ? PL_COMPARE_MODE_GREATER : PL_COMPARE_MODE_LESS) : PL_COMPARE_MODE_ALWAYS,
.ulCullMode = ulCullMode,
.ulWireframe = 0,
.ulStencilMode = PL_COMPARE_MODE_ALWAYS,
Expand Down Expand Up @@ -429,7 +429,7 @@ pl__get_3d_pipeline(plRenderPassHandle tRenderPass, uint32_t uMSAASampleCount, p
.tVertexShader = gptShader->load_glsl("../shaders/draw_3d_line.vert", "main", NULL, NULL),
.tGraphicsState = {
.ulDepthWriteEnabled = tFlags & PL_DRAW_FLAG_DEPTH_WRITE,
.ulDepthMode = tFlags & PL_DRAW_FLAG_DEPTH_TEST ? PL_COMPARE_MODE_GREATER : PL_COMPARE_MODE_ALWAYS,
.ulDepthMode = tFlags & PL_DRAW_FLAG_DEPTH_TEST ? (tFlags & PL_DRAW_FLAG_REVERSE_Z_DEPTH ? PL_COMPARE_MODE_GREATER : PL_COMPARE_MODE_LESS) : PL_COMPARE_MODE_ALWAYS,
.ulCullMode = ulCullMode,
.ulWireframe = 0,
.ulStencilMode = PL_COMPARE_MODE_ALWAYS,
Expand Down
13 changes: 7 additions & 6 deletions extensions/pl_draw_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ typedef struct _plDrawI

enum _plDrawFlags
{
PL_DRAW_FLAG_NONE = 0,
PL_DRAW_FLAG_DEPTH_TEST = 1 << 0,
PL_DRAW_FLAG_DEPTH_WRITE = 1 << 1,
PL_DRAW_FLAG_CULL_FRONT = 1 << 2,
PL_DRAW_FLAG_CULL_BACK = 1 << 3,
PL_DRAW_FLAG_FRONT_FACE_CW = 1 << 4,
PL_DRAW_FLAG_NONE = 0,
PL_DRAW_FLAG_DEPTH_TEST = 1 << 0,
PL_DRAW_FLAG_DEPTH_WRITE = 1 << 1,
PL_DRAW_FLAG_CULL_FRONT = 1 << 2,
PL_DRAW_FLAG_CULL_BACK = 1 << 3,
PL_DRAW_FLAG_FRONT_FACE_CW = 1 << 4,
PL_DRAW_FLAG_REVERSE_Z_DEPTH = 1 << 5,
};

enum _plDrawRectFlags
Expand Down
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
10 changes: 6 additions & 4 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 All @@ -47,7 +48,7 @@ Index of this file:
// [SECTION] apis
//-----------------------------------------------------------------------------

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

//-----------------------------------------------------------------------------
// [SECTION] includes
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
53 changes: 35 additions & 18 deletions extensions/pl_graphics_metal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@

// printf("%s\n", [tDevice.architecture.name UTF8String]);
atDeviceInfo[0].tType = PL_DEVICE_TYPE_INTEGRATED;
atDeviceInfo[0].tCapabilities = PL_DEVICE_CAPABILITY_BIND_GROUP_INDEXING | PL_DEVICE_CAPABILITY_SAMPLER_ANISOTROPY | PL_DEVICE_CAPABILITY_SWAPCHAIN;
atDeviceInfo[0].tCapabilities = PL_DEVICE_CAPABILITY_BIND_GROUP_INDEXING | PL_DEVICE_CAPABILITY_SAMPLER_ANISOTROPY | PL_DEVICE_CAPABILITY_SWAPCHAIN | PL_DEVICE_CAPABILITY_MULTIPLE_VIEWPORTS;

if(tDevice.hasUnifiedMemory)
{
Expand Down Expand Up @@ -2353,23 +2353,40 @@
plDrawArea* ptArea = &atAreas[i];
plDrawStream* ptStream = ptArea->ptDrawStream;

MTLScissorRect tScissorRect = {
.x = (NSUInteger)(ptArea->tScissor.iOffsetX),
.y = (NSUInteger)(ptArea->tScissor.iOffsetY),
.width = (NSUInteger)(ptArea->tScissor.uWidth),
.height = (NSUInteger)(ptArea->tScissor.uHeight)
};
[ptEncoder->tEncoder setScissorRect:tScissorRect];

MTLViewport tViewport = {
.originX = ptArea->tViewport.fX,
.originY = ptArea->tViewport.fY,
.width = ptArea->tViewport.fWidth,
.height = ptArea->tViewport.fHeight,
.znear = 0,
.zfar = 1.0
};
[ptEncoder->tEncoder setViewport:tViewport];
MTLScissorRect atScissors[PL_MAX_VIEWPORTS] = {0};
MTLViewport 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;
}

atScissors[j] = (MTLScissorRect){
.x = (NSUInteger)(ptArea->atScissors[j].iOffsetX),
.y = (NSUInteger)(ptArea->atScissors[j].iOffsetY),
.width = (NSUInteger)(ptArea->atScissors[j].uWidth),
.height = (NSUInteger)(ptArea->atScissors[j].uHeight)
};

atViewports[j] = (MTLViewport){
.originX = ptArea->atViewports[j].fX,
.originY = ptArea->atViewports[j].fY,
.width = ptArea->atViewports[j].fWidth,
.height = ptArea->atViewports[j].fHeight,
.znear = 0,
.zfar = 1.0
};

uViewportCount++;
}

[ptEncoder->tEncoder setViewports:atViewports count:uViewportCount];
[ptEncoder->tEncoder setScissorRects:atScissors count:uViewportCount];

const uint32_t uTokens = ptStream->_uStreamCount;
uint32_t uCurrentStreamIndex = 0;
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
Loading

0 comments on commit 3105cf9

Please sign in to comment.