From 21ec1138247937f9552471186464b6b4b357e6f1 Mon Sep 17 00:00:00 2001 From: Greg Wells Date: Wed, 28 May 2025 11:53:06 -0400 Subject: [PATCH] gryphn graphics pipelines (create baisc objects) --- include/gryphn/gryphn.h | 1 + .../vulkan_graphics_pipeline.c | 155 ++++++++++++++++++ .../vulkan_graphics_pipeline.h | 17 ++ src/core/gryphn_platform_functions.h | 4 + src/core/instance/init/gryphn_init.c | 2 + .../gryphn_graphics_pipeline.c | 11 ++ .../gryphn_graphics_pipeline.h | 89 ++++++++++ 7 files changed, 279 insertions(+) create mode 100644 rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.c create mode 100644 rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.h create mode 100644 src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.c create mode 100644 src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h diff --git a/include/gryphn/gryphn.h b/include/gryphn/gryphn.h index 3473383..1d03abd 100644 --- a/include/gryphn/gryphn.h +++ b/include/gryphn/gryphn.h @@ -9,3 +9,4 @@ #include #include #include +#include diff --git a/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.c b/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.c new file mode 100644 index 0000000..323f479 --- /dev/null +++ b/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.c @@ -0,0 +1,155 @@ +#include "vulkan_graphics_pipeline.h" + +VkDynamicState vkGryphnDynamicStateToVulkanDynamicState(enum gnDynamicState_e state) { + switch (state) { + case GN_DYNAMIC_VIEWPORT: return VK_DYNAMIC_STATE_SCISSOR; + case GN_DYNAMIC_SCISSOR: return VK_DYNAMIC_STATE_SCISSOR; + + case GN_DYNAMIC_STATE_MAX: return VK_DYNAMIC_STATE_MAX_ENUM; + } +} + +VkPrimitiveTopology vkGryphnPrimitiveType(enum gnPrimitiveType_e primitiveType) { + switch (primitiveType) { + case GN_PRIMITIVE_POINTS: return VK_PRIMITIVE_TOPOLOGY_POINT_LIST; + case GN_PRIMITIVE_LINES: return VK_PRIMITIVE_TOPOLOGY_LINE_LIST; + case GN_PRIMITIVE_LINE_STRIP: return VK_PRIMITIVE_TOPOLOGY_LINE_STRIP; + case GN_PRIMITIVE_TRIANGLES: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + case GN_PRIMITIVE_TRIANGLE_STRIP: return VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP; + } +} + +VkPolygonMode vkGryphnPolygonMode(enum gnFillMode_e fillMode) { + switch (fillMode) { + case GN_FILL_MODE_FILL: return VK_POLYGON_MODE_FILL; + case GN_FILL_MODE_LINE: return VK_POLYGON_MODE_LINE; + case GN_FILL_MODE_POINT: return VK_POLYGON_MODE_POINT; + } +} + +VkCullModeFlags vkGryphnCullMode(enum gnCullFace_e face) { + switch (face) { + case GN_CULL_FACE_NONE: return VK_CULL_MODE_NONE; + case GN_CULL_FACE_BACK: return VK_CULL_MODE_BACK_BIT; + case GN_CULL_FACE_FRONT: return VK_CULL_MODE_FRONT_BIT; + } +} + +VkBlendFactor vkGryphnBlendFactor(enum gnBlendFactor_e factor) { + switch (factor) { + case GN_BLEND_FACTOR_ZERO: return VK_BLEND_FACTOR_ZERO; + case GN_BLEND_FACTOR_ONE: return VK_BLEND_FACTOR_ONE; + case GN_BLEND_FACTOR_SRC_ALPHA: return VK_BLEND_FACTOR_SRC_ALPHA; + case GN_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA: return VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA; + } +} + +VkBlendOp vkGryphnBlendOperation(enum gnBlendOperation_e operation) { + switch(operation) { + case GN_OPERATION_ADD: return VK_BLEND_OP_ADD; + } +} + +gnReturnCode gnCreateGraphicsPipelineFn(struct gnGraphicsPipeline_t* graphicsPipeline, struct gnOutputDevice_t* device, struct gnGraphicsPipelineInfo_t info) { + graphicsPipeline->graphicsPipeline = malloc(sizeof(gnPlatformGraphicsPipeline)); + + for (int i = 0; i < GN_DYNAMIC_STATE_MAX; i++) graphicsPipeline->graphicsPipeline->isDynamic[i] = gnFalse; + + VkDynamicState* dynamicStates = malloc(sizeof(VkDynamicState) * info.dynamicState.dynamicStateCount); + for (int i = 0; i < info.dynamicState.dynamicStateCount; i++) { + graphicsPipeline->graphicsPipeline->isDynamic[info.dynamicState.dynamicStates[i]] = gnTrue; + dynamicStates[i] = vkGryphnDynamicStateToVulkanDynamicState(info.dynamicState.dynamicStates[i]); + } + + graphicsPipeline->graphicsPipeline->dynamicState = (VkPipelineDynamicStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO, + .dynamicStateCount = info.dynamicState.dynamicStateCount, + .pDynamicStates = dynamicStates + }; + + graphicsPipeline->graphicsPipeline->vertexInfo = (VkPipelineVertexInputStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO, + .vertexBindingDescriptionCount = 0, + .vertexAttributeDescriptionCount = 0 + }; + + graphicsPipeline->graphicsPipeline->inputAssembly = (VkPipelineInputAssemblyStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO, + .topology = vkGryphnPrimitiveType(info.primitiveType), + .primitiveRestartEnable = VK_FALSE + }; + + graphicsPipeline->graphicsPipeline->viewportState = (VkPipelineViewportStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO, + .viewportCount = 1, + .scissorCount = 1 + }; + + if (!graphicsPipeline->graphicsPipeline->isDynamic[GN_DYNAMIC_VIEWPORT]) { + graphicsPipeline->graphicsPipeline->viewport = (VkViewport) { + .x = info.viewport.position.x, + .y = info.viewport.position.y, + .width = info.viewport.size.x, + .height = info.viewport.size.y, + .minDepth = info.viewport.minDepth, + .maxDepth = info.viewport.maxDepth + }; + graphicsPipeline->graphicsPipeline->viewportState.pViewports = &graphicsPipeline->graphicsPipeline->viewport; + } + + if (!graphicsPipeline->graphicsPipeline->isDynamic[GN_DYNAMIC_SCISSOR]) { + graphicsPipeline->graphicsPipeline->scissor = (VkRect2D){ + .offset = { info.scissor.position.x, info.scissor.position.y }, + .extent = { info.scissor.size.x, info.scissor.size.y } + }; + graphicsPipeline->graphicsPipeline->viewportState.pScissors = &graphicsPipeline->graphicsPipeline->scissor; + } + + graphicsPipeline->graphicsPipeline->rasterizer = (VkPipelineRasterizationStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO, + .polygonMode = vkGryphnPolygonMode(info.fillMode), + .lineWidth = 1.0f, + .frontFace = ( info.cullMode.direction == GN_DIRECTION_CLOCK_WISE ) ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE, + .cullMode = vkGryphnCullMode(info.cullMode.face), + .depthBiasEnable = VK_FALSE, + .depthBiasConstantFactor = 0.0f, + .depthBiasClamp = 0.0f, + .depthBiasSlopeFactor = 0.0f + }; + + VkPipelineMultisampleStateCreateInfo multisampling = { + .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO, + .sampleShadingEnable = VK_FALSE, + .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT + }; + + graphicsPipeline->graphicsPipeline->colorBlendAttachment = (VkPipelineColorBlendAttachmentState){ + .colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT, + .blendEnable = ( info.colorBlending.enable == gnTrue ) ? VK_TRUE : VK_FALSE, + .srcColorBlendFactor = vkGryphnBlendFactor(info.colorBlending.sourceColorBlendFactor), + .dstColorBlendFactor = vkGryphnBlendFactor(info.colorBlending.destinationColorBlendFactor), + .colorBlendOp = vkGryphnBlendOperation(info.colorBlending.colorBlendOperation), + .srcAlphaBlendFactor = vkGryphnBlendFactor(info.colorBlending.sourceAlphaBlendFactor), + .dstAlphaBlendFactor = vkGryphnBlendFactor(info.colorBlending.destinationAlphaBlendFactor), + .alphaBlendOp = vkGryphnBlendOperation(info.colorBlending.alphaBlendOperation), + }; + + graphicsPipeline->graphicsPipeline->colorBlending = (VkPipelineColorBlendStateCreateInfo){ + .sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO, + .logicOpEnable = VK_FALSE, + .logicOp = VK_LOGIC_OP_COPY, + .attachmentCount = 1, + .pAttachments = &graphicsPipeline->graphicsPipeline->colorBlendAttachment, + .blendConstants[0] = 0.0f, + .blendConstants[1] = 0.0f, + .blendConstants[2] = 0.0f, + .blendConstants[3] = 0.0f + }; + + free(dynamicStates); + return GN_SUCCESS; +} + +void gnDestroyGraphicsPipelineFn(struct gnGraphicsPipeline_t *graphicsPipeline) { + +} diff --git a/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.h b/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.h new file mode 100644 index 0000000..1ce3f20 --- /dev/null +++ b/rendering_api/vulkan/src/pipelines/graphics_pipeline/vulkan_graphics_pipeline.h @@ -0,0 +1,17 @@ +#pragma once +#include +#include + +typedef struct gnPlatformGraphicsPipeline_t { + VkPipelineDynamicStateCreateInfo dynamicState; + gnBool isDynamic[GN_DYNAMIC_STATE_MAX]; + + VkPipelineVertexInputStateCreateInfo vertexInfo; + VkPipelineInputAssemblyStateCreateInfo inputAssembly; + VkViewport viewport; + VkRect2D scissor; + VkPipelineViewportStateCreateInfo viewportState; + VkPipelineRasterizationStateCreateInfo rasterizer; + VkPipelineColorBlendAttachmentState colorBlendAttachment; + VkPipelineColorBlendStateCreateInfo colorBlending; +} gnPlatformGraphicsPipeline; diff --git a/src/core/gryphn_platform_functions.h b/src/core/gryphn_platform_functions.h index 94a099b..6be3505 100644 --- a/src/core/gryphn_platform_functions.h +++ b/src/core/gryphn_platform_functions.h @@ -8,6 +8,7 @@ #include "window_surface/gryphn_surface.h" #include "window_surface/gryphn_surface_create_functions.h" #include "shader_module/gryphn_shader_module.h" +#include "pipelines/graphics_pipeline/gryphn_graphics_pipeline.h" typedef struct gnFunctions_t { gnReturnCode (*_gnCreateInstance)(gnInstance* instance, struct gnInstanceInfo_t info); @@ -54,4 +55,7 @@ typedef struct gnDeviceFunctions_t { gnReturnCode (*_gnCreateShaderModule)(struct gnShaderModule_t* module, struct gnOutputDevice_t* device, struct gnShaderModuleInfo_t shaderModuleInfo); void (*_gnDestroyShaderModule)(struct gnShaderModule_t* module); + + gnReturnCode (*_gnCreateGraphicsPipeline)(struct gnGraphicsPipeline_t* pipeline, struct gnOutputDevice_t* device, struct gnGraphicsPipelineInfo_t pipelineInfo); + void (*_gnDestroyGraphicsPipeline)(struct gnGraphicsPipeline_t* pipeline); } gnDeviceFunctions; diff --git a/src/core/instance/init/gryphn_init.c b/src/core/instance/init/gryphn_init.c index 5a27d1b..b72f266 100644 --- a/src/core/instance/init/gryphn_init.c +++ b/src/core/instance/init/gryphn_init.c @@ -73,4 +73,6 @@ void gnLoadDeviceFunctions(struct gnDynamicLibrary_t* lib, struct gnDeviceFuncti gnLoadDLLFunction(lib, functions->_gnDestroyPresentationQueue, "gnDestroyPresentationQueueFn"); gnLoadDLLFunction(lib, functions->_gnCreateShaderModule, "gnCreateShaderModuleFn"); gnLoadDLLFunction(lib, functions->_gnDestroyShaderModule, "gnDestroyShaderModuleFn"); + gnLoadDLLFunction(lib, functions->_gnCreateGraphicsPipeline, "gnCreateGraphicsPipelineFn"); + gnLoadDLLFunction(lib, functions->_gnDestroyGraphicsPipeline, "gnDestroyGraphicsPipelineFn"); } diff --git a/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.c b/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.c new file mode 100644 index 0000000..34377be --- /dev/null +++ b/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.c @@ -0,0 +1,11 @@ +#include "gryphn_graphics_pipeline.h" +#include "core/gryphn_platform_functions.h" + +gnReturnCode gnCreateGraphicsPipeline(struct gnGraphicsPipeline_t* graphicsPipeline, struct gnOutputDevice_t* device, struct gnGraphicsPipelineInfo_t info) { + graphicsPipeline->device = device; + return graphicsPipeline->device->deviceFunctions->_gnCreateGraphicsPipeline(graphicsPipeline, device, info); +} + +void gnDestroyGraphicsPipeline(struct gnGraphicsPipeline_t* graphicsPipeline) { + graphicsPipeline->device->deviceFunctions->_gnDestroyGraphicsPipeline(graphicsPipeline); +} diff --git a/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h b/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h new file mode 100644 index 0000000..c5cc48f --- /dev/null +++ b/src/core/pipelines/graphics_pipeline/gryphn_graphics_pipeline.h @@ -0,0 +1,89 @@ +#pragma once +#include + +typedef enum gnDynamicState_e { + GN_DYNAMIC_VIEWPORT, + GN_DYNAMIC_SCISSOR, + + GN_DYNAMIC_STATE_MAX +} gnDynamicState; + +typedef struct gnDynamicStateInfo_t { + uint32_t dynamicStateCount; + enum gnDynamicState_e* dynamicStates; +} gnDynamicStateInfo; + +typedef enum gnPrimitiveType_e { + GN_PRIMITIVE_POINTS, GN_PRIMITIVE_LINES, GN_PRIMITIVE_LINE_STRIP, GN_PRIMITIVE_TRIANGLES, GN_PRIMITIVE_TRIANGLE_STRIP +} gnPrimativeType; + +typedef struct gnViewport_t { + gnVec2 position; + gnVec2 size; + float minDepth; + float maxDepth; +} gnViewport; + +typedef struct gnScissor_t { + gnInt2 position; + gnUInt2 size; +} gnScissor; + +typedef enum gnFillMode_e { + GN_FILL_MODE_FILL, GN_FILL_MODE_LINE, GN_FILL_MODE_POINT +} gnFillMode; + +typedef enum gnCullFace_e { + GN_CULL_FACE_NONE, GN_CULL_FACE_BACK, GN_CULL_FACE_FRONT +} gnCullFace; + +typedef enum gnCullDirection_e { + GN_DIRECTION_CLOCK_WISE, GN_DIRECTION_COUNTER_CLOCK_WISE +} gnCullDirection; + +typedef struct gnCullMode_t { + enum gnCullFace_e face; + enum gnCullDirection_e direction; +} gnCullMode; + +typedef enum gnBlendFactor_e { + GN_BLEND_FACTOR_ZERO, + GN_BLEND_FACTOR_ONE, + GN_BLEND_FACTOR_SRC_ALPHA, + GN_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA +} gnBlendFactor; + +typedef enum gnBlendOperation_e { + GN_OPERATION_ADD +} gnBlendOperation; + +typedef struct gnColorBlending_t { + gnBool enable; + enum gnBlendFactor_e sourceColorBlendFactor; + enum gnBlendFactor_e sourceAlphaBlendFactor; + enum gnBlendFactor_e destinationColorBlendFactor; + enum gnBlendFactor_e destinationAlphaBlendFactor; + + enum gnBlendOperation_e colorBlendOperation; + enum gnBlendOperation_e alphaBlendOperation; +} gnColorBlending; + +typedef struct gnGraphicsPipelineInfo_t { + struct gnDynamicStateInfo_t dynamicState; + enum gnPrimitiveType_e primitiveType; + struct gnViewport_t viewport; + struct gnScissor_t scissor; + enum gnFillMode_e fillMode; + struct gnCullMode_t cullMode; + struct gnColorBlending_t colorBlending; +} gnGraphicsPipelineInfo; + +struct gnPlatformGraphicsPipeline_t; + +typedef struct gnGraphicsPipeline_t { + struct gnPlatformGraphicsPipeline_t* graphicsPipeline; + struct gnOutputDevice_t* device; +} gnGraphicsPipeline; + +gnReturnCode gnCreateGraphicsPipeline(struct gnGraphicsPipeline_t* graphicsPipeline, struct gnOutputDevice_t* device, struct gnGraphicsPipelineInfo_t info); +void gnDestroyGraphicsPipeline(struct gnGraphicsPipeline_t* graphicsPipeline);