/
Zamar_Terrier
/
TigorEngine
Обзор
Документация
Войти
/
Zamar_Terrier
/
TigorEngine
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/Core/graphicsObject.c
262 строки
9 KB
Zamar_Terrier
Офигенное обновление движка
31 июл 2026, 21:03
Верифицирован
31 июл 2026, 21:03
1682686
Код
Авторство
О чём код?
#include "Core/graphicsObject.h" #include <vulkan/vulkan.h> #include "Core/e_memory.h" #include "Core/e_device.h" #include "Core/pipeline.h" #include "Core/e_buffer.h" #include "Core/e_texture.h" #include "Core/e_descriptor.h" #include "Tools/shader_builder.h" #include "Data/e_resource_data.h" #include "Data/e_resource_descriptors.h" #include "Data/e_resource_engine.h" extern TEngine engine; void GraphicsObjectInit(GraphicsObject *graphObj, uint32_t type) { graphObj->BluePrints.num_blue_print_packs = 0; graphObj->BluePrints.isShadow = false; if (graphObj->num_shapes == 0) memset(&graphObj->shapes, 0, sizeof(Shape) * MAX_SHAPES); switch (type) { case TIGOR_VERTEX_TYPE_2D_OBJECT: graphObj->shapes[0].bindingDescription = &Bind2DDescription; graphObj->shapes[0].attr = planeAttributeDescription; graphObj->shapes[0].countAttr = 3; graphObj->shapes[0].countBind = 1; graphObj->shapes[0].type = type; break; case TIGOR_VERTEX_TYPE_3D_OBJECT: graphObj->shapes[0].bindingDescription = &Bind3DDescription; graphObj->shapes[0].attr = cubeAttributeDescription; graphObj->shapes[0].countAttr = 4; graphObj->shapes[0].countBind = 1; graphObj->shapes[0].type = type; break; case TIGOR_VERTEX_TYPE_3D_INSTANCE: graphObj->shapes[0].bindingDescription = AllocateMemory(2, sizeof(VkVertexInputBindingDescription)); graphObj->shapes[0].bindingDescription[0] = Bind3DDescription; graphObj->shapes[0].bindingDescription[1] = Bind3DInstanceDescription; graphObj->shapes[0].attr = instanceAttributeDescription; graphObj->shapes[0].countAttr = 7; graphObj->shapes[0].countBind = 2; graphObj->shapes[0].type = type; break; case TIGOR_VERTEX_TYPE_TREE_INSTANCE: graphObj->shapes[0].bindingDescription = AllocateMemory(2, sizeof(VkVertexInputBindingDescription)); graphObj->shapes[0].bindingDescription[0] = BindTree3DDescription; graphObj->shapes[0].bindingDescription[1] = Bind3DInstanceDescription; graphObj->shapes[0].attr = treeInstanceAttributeDescription; graphObj->shapes[0].countAttr = 7; graphObj->shapes[0].countBind = 2; graphObj->shapes[0].type = type; break; case TIGOR_VERTEX_TYPE_MODEL_OBJECT: graphObj->shapes[0].bindingDescription = &BindModel3DDescription; graphObj->shapes[0].attr = modelAttributeDescription; graphObj->shapes[0].countAttr = 7; graphObj->shapes[0].countBind = 1; graphObj->shapes[0].type = type; break; case TIGOR_VERTEX_TYPE_2D_PARTICLE: graphObj->shapes[0].bindingDescription = &BindParticle2DDescription; graphObj->shapes[0].attr = particle2DAttributeDescription; graphObj->shapes[0].countAttr = 3; graphObj->shapes[0].countBind = 1; graphObj->shapes[0].type = type; break; case TIGOR_VERTEX_TYPE_3D_PARTICLE: graphObj->shapes[0].bindingDescription = &BindParticle3DDescription; graphObj->shapes[0].attr = particle3DAttributeDescription; graphObj->shapes[0].countAttr = 3; graphObj->shapes[0].countBind = 1; graphObj->shapes[0].type = type; break; default: graphObj->shapes[0].type = 0; break; } } void GraphicsObjectSetVertex(GraphicsObject *graphObj, void *vert, uint32_t type_v_size, uint32_t *inx, VertextIterator *vi) { if (vi->v_index > MAX_VERTEX_COUNT || vi->i_index > MAX_INDEX_COUNT) { printf("Too many vertices/indices!\n"); return; } uint32_t num = graphObj->num_shapes; graphObj->shapes[num].vParam.typeSize = type_v_size; graphObj->shapes[num].iParam.typeSize = sizeof(uint32_t); int res = 0; if (vert != NULL) { graphObj->shapes[num].vParam.vertices = AllocateMemory(vi->v_index, graphObj->shapes[num].vParam.typeSize); memcpy(graphObj->shapes[num].vParam.vertices, vert, graphObj->shapes[num].vParam.typeSize * vi->v_index); graphObj->shapes[num].vParam.num_verts = vi->v_index; } if (inx != NULL) { graphObj->shapes[num].iParam.indices = AllocateMemory(vi->i_index, graphObj->shapes[num].iParam.typeSize); memcpy(graphObj->shapes[num].iParam.indices, inx, graphObj->shapes[num].iParam.typeSize * vi->i_index); graphObj->shapes[num].iParam.indexesSize = vi->i_index; } if (!graphObj->shapes[num].init) { res = BuffersCreateVertex((VertexParam *)&graphObj->shapes[num].vParam); if (res) return; res = BuffersCreateIndex((IndexParam *)&graphObj->shapes[num].iParam); if (res) return; graphObj->shapes[num].init = true; } if (graphObj->shapes[num].vParam.num_verts > 0) BuffersUpdateVertex((VertexParam *)&graphObj->shapes[num].vParam); if (graphObj->shapes[num].iParam.indexesSize > 0) BuffersUpdateIndex((IndexParam *)&graphObj->shapes[num].iParam); graphObj->num_shapes++; } void GraphicsObjectSetShaderWithUniform(GraphicsObject *graphObj, ShaderObject *shader, uint32_t pack_indx) { ShaderBuilder temp = {0}; ShaderBuilderMakeUniformsFromShader(&temp, (uint32_t *)shader->code, shader->size, &graphObj->BluePrints, pack_indx); PipelineSetting *setting = (PipelineSetting *)&graphObj->BluePrints.blue_print_packs[pack_indx].setting; uint32_t type = 0; switch (temp.type) { case SHADER_TYPE_VERTEX: type = VK_SHADER_STAGE_VERTEX_BIT; break; case SHADER_TYPE_FRAGMENT: type = VK_SHADER_STAGE_FRAGMENT_BIT; break; case SHADER_TYPE_COMPUTED: type = VK_SHADER_STAGE_COMPUTE_BIT; break; case SHADER_TYPE_TESELLATION_CONTROL: type = VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; break; case SHADER_TYPE_TESELLATION_EVALUATION: type = VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; break; } PipelineSettingSetShader(setting, shader, type); } void GraphicsObjectSetShader(GraphicsObject *graphObj, ShaderObject *shader, uint32_t pack_indx, uint32_t shader_type) { PipelineSetting *setting = (PipelineSetting *)&graphObj->BluePrints.blue_print_packs[pack_indx].setting; PipelineSettingSetShader(setting, shader, shader_type); } void GraphicsObjectCreateDrawItems(GraphicsObject *graphObj) { memset(graphObj->gItems.shader_packs, 0, sizeof(ShaderPack) * MAX_BLUE_PRINTS); for (int i = 0; i < graphObj->BluePrints.num_blue_print_packs; i++) { BluePrintPack *pack = &graphObj->BluePrints.blue_print_packs[i]; DescriptorCreate(&graphObj->gItems.shader_packs[i].descriptor, pack->descriptors, &graphObj->BluePrints, pack->num_descriptors, engine.imagesCount); } } void GraphicsObjectClean(GraphicsObject *graphObj) { for (int i = 0; i < graphObj->gItems.num_shader_packs; i++) { PipelineDestroyer(&graphObj->gItems.shader_packs[i].pipeline); DescriptorDestroyer(&graphObj->gItems.shader_packs[i].descriptor); } for (int i = 0; i < graphObj->BluePrints.num_blue_print_packs; i++) { BluePrintPack *pack = &graphObj->BluePrints.blue_print_packs[i]; for (int j = 0; j < pack->num_descriptors; j++) { BluePrintDescriptor *descriptor = &pack->descriptors[j]; if (descriptor->descrType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) { BuffersDestroyContainer(&descriptor->uniform); } } } } void GraphicsObjectDestroy(GraphicsObject *graphObj) { for (int i = 0; i < graphObj->gItems.num_shader_packs; i++) { PipelineDestroyer(&graphObj->gItems.shader_packs[i].pipeline); DescriptorDestroyer(&graphObj->gItems.shader_packs[i].descriptor); } BluePrintClearAll(&graphObj->BluePrints); for (int i = 0; i < graphObj->num_shapes; i++) { if (graphObj->shapes[i].iParam.indexesSize > 0) { BuffersDestroyer(&graphObj->shapes[i].iParam.buffer); } BuffersDestroyer(&graphObj->shapes[i].vParam.buffer); if (graphObj->shapes[i].vParam.vertices != NULL) { FreeMemory(graphObj->shapes[i].vParam.vertices); } if (graphObj->shapes[i].iParam.indices != NULL) { FreeMemory(graphObj->shapes[i].iParam.indices); } if (graphObj->shapes[i].type == TIGOR_VERTEX_TYPE_3D_INSTANCE || graphObj->shapes[i].type == TIGOR_VERTEX_TYPE_TREE_INSTANCE) { FreeMemory(graphObj->shapes[i].bindingDescription); } } } void GraphicsObjectSetShadersPath(GraphicsObject *graphObj, const char *vert, const char *frag) { int len = strlen(vert); memset(graphObj->aShader.vertShader, 0, 256); memcpy(graphObj->aShader.vertShader, vert, len); len = strlen(frag); memset(graphObj->aShader.fragShader, 0, 256); memcpy(graphObj->aShader.fragShader, frag, len); }