Commit 9a4f739
Changed files (6)
libgogvk/gog_pipeline.c
@@ -0,0 +1,132 @@
+#include "gog_pipeline.h"
+#include <stdlib.h>
+
+gog_result gog_create_pipeline_core(gog_pipeline_core * core, gog_pipeline_ci ci) {
+ core->vk_pipeline_device = ci.device;
+ core->type = ci.type;
+
+ GOG_LOG_INFO("Initializing Pipeline Core [Type: %d]", ci.type);
+
+ /* 1. Layout Creation (Linking Atomic Resources) */
+ VkPipelineLayoutCreateInfo layout_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
+ .pNext = NULL,
+ .setLayoutCount = (ci.descriptor_set) ? 1 : 0,
+ .pSetLayouts = (ci.descriptor_set) ? &ci.descriptor_set->layout.vk_layout : NULL,
+ .pushConstantRangeCount = (ci.push_constants) ? 1 : 0,
+ .pPushConstantRanges = (ci.push_constants) ? &ci.push_constants->vk_range : NULL
+ };
+
+ VkResult res = vkCreatePipelineLayout(ci.device, &layout_ci, NULL, &core->vk_pipeline_layout);
+ GOG_CHECK_VK(res, "Failed to create VkPipelineLayout");
+
+ /* 2. Shader Stage Setup */
+ VkPipelineShaderStageCreateInfo *stages = alloca(sizeof(VkPipelineShaderStageCreateInfo) * ci.shader_c);
+ for (uint32_t j = 0; j < ci.shader_c; j++) {
+ stages[j] = (VkPipelineShaderStageCreateInfo){
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
+ .stage = ci.shaders_p[j].stage,
+ .module = ci.shaders_p[j].mod,
+ .pName = "main"
+ };
+ }
+
+ /* 3. Pipeline Type-Specific Logic */
+ if (ci.type == GOG_GRAPHICS_PIPELINE) {
+ core->vk_bind_point = VK_PIPELINE_BIND_POINT_GRAPHICS;
+
+ /* Pull Vertex Input from Atomic gog_vertex_buffer */
+ VkVertexInputBindingDescription binding = {
+ .binding = 0,
+ .stride = (uint32_t)ci.graphics.v_buff->layout.stride,
+ .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
+ };
+
+ VkPipelineVertexInputStateCreateInfo v_input_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
+ .vertexBindingDescriptionCount = 1,
+ .pVertexBindingDescriptions = &binding,
+ .vertexAttributeDescriptionCount = (uint32_t)ci.graphics.v_buff->layout.attr_c,
+ .pVertexAttributeDescriptions = ci.graphics.v_buff->layout.vk_attrs
+ };
+
+ /* Setup Dynamic Rendering (Modern Vulkan style) */
+ VkPipelineRenderingCreateInfo dynamic_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
+ .colorAttachmentCount = 1,
+ .pColorAttachmentFormats = &ci.graphics.canvas->color_format,
+ .depthAttachmentFormat = ci.graphics.canvas->depth_format
+ };
+
+ /* Fixed Function States from Atomic Raster/Canvas objects */
+ VkPipelineInputAssemblyStateCreateInfo ia_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
+ .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST
+ };
+
+ VkPipelineRasterizationStateCreateInfo rs_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
+ .lineWidth = 1.0f,
+ .polygonMode = VK_POLYGON_MODE_FILL,
+ .cullMode = ci.graphics.raster->cull_mode
+ };
+
+ VkPipelineDepthStencilStateCreateInfo ds_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
+ .depthTestEnable = ci.graphics.raster->depth_test,
+ .depthWriteEnable = ci.graphics.raster->depth_test,
+ .depthCompareOp = VK_COMPARE_OP_LESS
+ };
+
+ VkPipelineMultisampleStateCreateInfo ms_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
+ .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT
+ };
+
+ VkPipelineViewportStateCreateInfo vp_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
+ .viewportCount = 1,
+ .scissorCount = 1
+ };
+
+ VkDynamicState dyn_states[] = { VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
+ VkPipelineDynamicStateCreateInfo dyn_ci = {
+ .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
+ .dynamicStateCount = 2,
+ .pDynamicStates = dyn_states
+ };
+
+ VkGraphicsPipelineCreateInfo gfx_ci = {
+ .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
+ .pNext = &dynamic_ci,
+ .stageCount = ci.shader_c,
+ .pStages = stages,
+ .pVertexInputState = &v_input_ci,
+ .pInputAssemblyState = &ia_ci,
+ .pRasterizationState = &rs_ci,
+ .pMultisampleState = &ms_ci,
+ .pDepthStencilState = &ds_ci,
+ .pViewportState = &vp_ci,
+ .pDynamicState = &dyn_ci,
+ .layout = core->vk_pipeline_layout
+ };
+
+ res = vkCreateGraphicsPipelines(ci.device, VK_NULL_HANDLE, 1, &gfx_ci, NULL, &core->vk_pipeline);
+ GOG_CHECK_VK(res, "Failed to create Graphics VkPipeline");
+
+ } else {
+ core->vk_bind_point = VK_PIPELINE_BIND_POINT_COMPUTE;
+
+ VkComputePipelineCreateInfo comp_ci = {
+ .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
+ .stage = stages[0],
+ .layout = core->vk_pipeline_layout
+ };
+
+ res = vkCreateComputePipelines(ci.device, VK_NULL_HANDLE, 1, &comp_ci, NULL, &core->vk_pipeline);
+ GOG_CHECK_VK(res, "Failed to create Compute VkPipeline");
+ }
+
+ GOG_LOG_INFO("Pipeline core created at address: %p", (void*)core->vk_pipeline);
+ return GET_GOG_RESULT(GOG_SUCCES, 0);
+}
libgogvk/gog_pipeline.h
@@ -1,3 +1,6 @@
+#ifndef GOG_PIPELINE_H
+#define GOG_PIPELINE_H
+
#include <vulkan/vulkan_core.h>
#include "gogerr.h"
@@ -8,10 +11,78 @@ typedef enum{
GOG_COMPUTE_PIPELINE
} gog_pipeline_type;
+typedef struct {
+ VkDescriptorSet vk_set;
+
+ struct {
+ VkDescriptorSetLayout vk_layout;
+ size_t binding_c;
+ } layout;
+} gog_descriptor_set;
+
+typedef struct {
+ VkPushConstantRange vk_range;
+ void* p_data;
+ size_t size;
+} gog_push_constants;
+
+typedef struct {
+ VkBuffer vk_buffer;
+ VkBuffer vk_index_buffer;
+
+ struct {
+ VkVertexInputAttributeDescription* vk_attrs;
+ size_t attr_c;
+ size_t stride;
+ } layout;
+ struct
+ {
+ uint32_t index_c;
+ uint32_t instance_c;
+ uint32_t first_index;
+ int32_t v_offset;
+ }draw_i;
+} gog_vertex_buffer;
+
+typedef struct {
+ VkFormat color_format;
+ VkFormat depth_format;
+} gog_canvas_state;
+
+typedef struct {
+ VkCullModeFlags cull_mode;
+ VkBool32 depth_test;
+} gog_raster_state;
+
+typedef struct {
+ gog_pipeline_type type;
+ VkDevice device;
+
+ gog_descriptor_set* descriptor_set;
+ gog_push_constants* push_constants;
+
+ uint32_t shader_c;
+ struct {
+ VkShaderModule mod;
+ VkShaderStageFlagBits stage;
+ } * shaders_p;
+
+ union {
+ struct {
+ gog_vertex_buffer* v_buff;
+ gog_canvas_state* canvas;
+ gog_raster_state* raster;
+ } graphics;
+ struct {
+ size_t local_x, local_y, local_z;
+ } compute;
+ };
+} gog_pipeline_ci;
typedef struct{
gog_pipeline_type type;
+ VkDevice vk_pipeline_device;
VkPipelineBindPoint vk_bind_point;
VkPipeline vk_pipeline;
VkPipelineLayout vk_pipeline_layout;
@@ -19,39 +90,33 @@ typedef struct{
} gog_pipeline_core;
-typedef struct
-{
+typedef struct {
gog_pipeline_type type;
- uint32_t de_set_c;
- VkDescriptorSet * vk_de_set;
+ gog_descriptor_set* resource;
+ gog_push_constants* push_constants;
+
+ VkViewport viewport;
+ VkRect2D scissor;
- union
- {
- struct {
- VkBuffer vertex_buffer;
- VkBuffer index_buffer;
- uint32_t index_c;
- uint32_t instance_c;
- } draw;
-
+ union {
+ gog_vertex_buffer* v_buff;
struct {
- struct
- {
- uint32_t x,y,z;
- } group_c;
+ struct { uint32_t x, y, z; } group_c;
} dispatch;
};
-
-
-} gog_pipeline_io;
+} gog_pipeline_i;
typedef struct
{
gog_pipeline_core * core;
- gog_pipeline_io * io;
+ gog_pipeline_i * info;
} gog_pipeline;
+gog_result gog_create_pipeline_core(gog_pipeline_core * core, gog_pipeline_ci ci);
+
+#endif
+
libgogvk/gogerr.h
@@ -1,18 +1,44 @@
+#ifndef GOG_ERR_H
+#define GOG_ERR_H
+
+#include <stdio.h>
+#include <vulkan/vulkan_core.h>
+
+#define GOG_DEBUG
+
+#ifdef GOG_DEBUG
+ #define GOG_LOG_INFO( fmt, ...) { printf("[GOG INFO] " fmt "\n", __VA_ARGS__); }
+ #define GOG_LOG_WARN( fmt, ...) { printf("[GOG WARN] " fmt "\n", __VA_ARGS__); }
+ #define GOG_LOG_ERR(res, msg) printf("[GOG ERROR] %s (VkResult: %d)\n", msg, res)
+#else
+ #define GOG_LOG(...)
+ #define GOG_LOG_ERR(res, msg)
+#endif
-#define GET_GOG_RESULT(status_,code_)\
- ((gog_result){.status = status_, .err_code =code_})
-#define GOG_ERR_MISMATCH 0x2
typedef enum
{
- GOG_SUCCES=1,
- GOG_FAIL=0
-}gog_err;
-
+ GOG_SUCCES = 1,
+ GOG_FAIL = 0
+} gog_err;
typedef struct
{
- gog_err status;
- int err_code;
-}gog_result;
+ gog_err status;
+ int err_code;
+} gog_result;
+
+#define GET_GOG_RESULT(status_, code_) \
+ ((gog_result){.status = status_, .err_code = code_})
+
+#define GOG_CHECK_VK(result, msg) \
+ if (result != VK_SUCCESS) { \
+ GOG_LOG_ERR(result, msg); \
+ return GET_GOG_RESULT(GOG_FAIL, result); \
+ }
+
+#define GOG_ERR_MISMATCH 0x2
+#define GOG_ERR_PIPELINE_CREATION 0x3
+
+#endif
libgogvk.a
Binary file
main.c
@@ -5,6 +5,7 @@
#include <vulkan/vulkan_core.h>
#include <SDL2/SDL.h>
#include "libgogvk/gogvid.h"
+#include "libgogvk/gogerr.h"
int main()
{
makefile
@@ -2,7 +2,7 @@ CC=gcc
CFLAGS=-Wall -Wextra -O2 -I./libgogvk `sdl2-config --cflags`
LIBS=-lvulkan `sdl2-config --libs`
-LIB_SRC=./libgogvk/gogvid.c
+LIB_SRC=./libgogvk/gogvid.c ./libgogvk/gog_pipeline.c
LIB_OBJ=$(LIB_SRC:.c=.o)
MAIN_SRC=main.c