From c7efea5258f7674cdb0ecf2a52accf4cfdccf508 Mon Sep 17 00:00:00 2001 From: redstrate <54911369+redstrate@users.noreply.github.com> Date: Thu, 18 Feb 2021 08:31:36 -0500 Subject: [PATCH] Add more windowing stuff to Linux backend, fix some other clang-analyze bugs --- engine/asset/src/asset.cpp | 9 ++------ engine/core/src/engine.cpp | 2 +- engine/gfx/vulkan/src/gfx_vulkan.cpp | 33 +++++++++++++++++----------- engine/renderer/src/renderer.cpp | 4 ++-- platforms/linux/main.cpp.in | 12 ++++++++-- tools/shadercompiler/main.cpp | 2 +- 6 files changed, 36 insertions(+), 26 deletions(-) diff --git a/engine/asset/src/asset.cpp b/engine/asset/src/asset.cpp index 622d5b6..8ab47e6 100644 --- a/engine/asset/src/asset.cpp +++ b/engine/asset/src/asset.cpp @@ -107,12 +107,8 @@ std::unique_ptr load_mesh(const file::Path path) { Vector3 scl; file->read(&scl); - - int finalBoneIndex = 0; - - if(boneMapping.count(bone)) { - finalBoneIndex = boneMapping[bone]; - } else { + + if(!boneMapping.count(bone)) { Bone b; b.index = mesh->bones.size(); b.name = bone; @@ -127,7 +123,6 @@ std::unique_ptr load_mesh(const file::Path path) { mesh->bones.push_back(b); boneMapping[bone] = b.index; - finalBoneIndex = b.index; } } diff --git a/engine/core/src/engine.cpp b/engine/core/src/engine.cpp index 56a73f4..5aa4cb6 100755 --- a/engine/core/src/engine.cpp +++ b/engine/core/src/engine.cpp @@ -414,7 +414,7 @@ void Engine::add_window(void* native_handle, const int identifier, const prism:: _gfx->initialize_view(native_handle, identifier, drawable_extent.width, drawable_extent.height); - _windows.push_back(std::move(std::make_unique())); + _windows.push_back(std::make_unique()); Window* window = _windows.back().get(); window->identifier = identifier; diff --git a/engine/gfx/vulkan/src/gfx_vulkan.cpp b/engine/gfx/vulkan/src/gfx_vulkan.cpp index 2b9db9f..546d41c 100755 --- a/engine/gfx/vulkan/src/gfx_vulkan.cpp +++ b/engine/gfx/vulkan/src/gfx_vulkan.cpp @@ -226,11 +226,13 @@ void GFXVulkan::recreate_view(const int identifier, const uint32_t width, const if(surface->identifier == identifier) found_surface = surface; } - - found_surface->surfaceWidth = width; - found_surface->surfaceHeight = height; - - createSwapchain(found_surface, found_surface->swapchain); + + if(found_surface != nullptr) { + found_surface->surfaceWidth = width; + found_surface->surfaceHeight = height; + + createSwapchain(found_surface, found_surface->swapchain); + } } GFXBuffer* GFXVulkan::create_buffer(void *data, const GFXSize size, const bool dynamic_data, const GFXBufferUsage usage) { @@ -294,7 +296,8 @@ void GFXVulkan::copy_buffer(GFXBuffer* buffer, void* data, GFXSize offset, GFXSi VkMappedMemoryRange range = {}; range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; range.memory = vulkanBuffer->memory; - range.size = VK_WHOLE_SIZE; + range.size = size; + range.offset = offset; vkFlushMappedMemoryRanges(device, 1, &range); vkUnmapMemory(device, vulkanBuffer->memory); @@ -1029,8 +1032,11 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) { GFXVulkanCommandBuffer* cmdbuf = (GFXVulkanCommandBuffer*)command_buffer; if(cmdbuf->handle != VK_NULL_HANDLE) cmd = cmdbuf->handle; - else + else if(current_surface != nullptr) cmd = commandBuffers[current_surface-> currentFrame]; + + if(cmd == nullptr) + return; VkCommandBufferBeginInfo beginInfo = {}; beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; @@ -1041,9 +1047,11 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) { VkRenderPass currentRenderPass = VK_NULL_HANDLE; GFXVulkanPipeline* currentPipeline = nullptr; uint64_t lastDescriptorHash = 0; - VkIndexType currentIndexType = VK_INDEX_TYPE_UINT32; const auto try_bind_descriptor = [cmd, this, ¤tPipeline, &lastDescriptorHash]() -> bool { + if(currentPipeline == nullptr) + return false; + if (lastDescriptorHash != getDescriptorHash(currentPipeline)) { if (!currentPipeline->cachedDescriptorSets.count(getDescriptorHash(currentPipeline))) cacheDescriptorState(currentPipeline, currentPipeline->descriptorLayout); @@ -1060,7 +1068,7 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) { return true; }; - for (auto command : command_buffer->commands) { + for (const auto& command : command_buffer->commands) { switch (command.type) { case GFXCommandType::SetRenderPass: { @@ -1100,7 +1108,7 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) { vkCmdSetScissor(cmd, 0, 1, &scissor); } - else { + else if(current_surface != nullptr) { renderPassInfo.framebuffer = current_surface->swapchainFramebuffers[imageIndex]; VkViewport viewport = {}; @@ -1170,8 +1178,6 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) { indexType = VK_INDEX_TYPE_UINT16; vkCmdBindIndexBuffer(cmd, ((GFXVulkanBuffer*)command.data.set_index_buffer.buffer)->handle, 0, indexType); - - currentIndexType = indexType; } break; case GFXCommandType::SetPushConstant: @@ -1267,7 +1273,7 @@ void GFXVulkan::submit(GFXCommandBuffer* command_buffer, const int identifier) { submitInfo.pCommandBuffers = &cmd; vkQueueSubmit(graphicsQueue, 1, &submitInfo, VK_NULL_HANDLE); - } else { + } else if(current_surface != nullptr) { // submit VkSubmitInfo submitInfo = {}; submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; @@ -1445,6 +1451,7 @@ void GFXVulkan::createSwapchain(NativeSurface* native_surface, VkSwapchainKHR ol #else if(native_surface->surface == VK_NULL_HANDLE) { struct WindowConnection { + int identifier = 0; xcb_connection_t* connection; xcb_window_t window; }; diff --git a/engine/renderer/src/renderer.cpp b/engine/renderer/src/renderer.cpp index 33b7337..d724856 100755 --- a/engine/renderer/src/renderer.cpp +++ b/engine/renderer/src/renderer.cpp @@ -300,7 +300,7 @@ void Renderer::render(GFXCommandBuffer* commandbuffer, Scene* scene, RenderTarge commandbuffer->pop_group(); - if(render_options.enable_depth_of_field) + if(render_options.enable_depth_of_field && dofPass != nullptr) dofPass->render(commandbuffer, *scene); beginInfo.framebuffer = nullptr; @@ -1064,7 +1064,7 @@ ShaderSource Renderer::register_shader(const std::string_view shader_file) { file::Path shader_path = file::Path(shader_file); - ShaderStage stage; + ShaderStage stage = ShaderStage::Vertex; if(shader_path.extension() == ".vert") stage = ShaderStage::Vertex; else if(shader_path.extension() == ".frag") diff --git a/platforms/linux/main.cpp.in b/platforms/linux/main.cpp.in index 8b1eee7..230200c 100755 --- a/platforms/linux/main.cpp.in +++ b/platforms/linux/main.cpp.in @@ -27,6 +27,7 @@ xcb_connection_t* connection = nullptr; xcb_screen_t* screen = nullptr; struct WindowConnection { + int identifier = 0; xcb_connection_t* connection; xcb_window_t window; }; @@ -67,6 +68,7 @@ static inline xcb_intern_atom_reply_t* intern_atom_helper(xcb_connection_t *conn int platform::open_window(const std::string_view title, const prism::Rectangle rect, const WindowFlags flags) { auto& win = window_connections.emplace_back(); win.connection = connection; + win.identifier = window_connections.size() - 1; uint32_t mask; uint32_t values[32]; @@ -115,7 +117,7 @@ int platform::open_window(const std::string_view title, const prism::Rectangle r engine->add_window((void*)&win, 0, rect.extent); app->initialize_render(); - return window_connections.size() - 1; + return win.identifier; } void platform::close_window(const int index) { @@ -362,10 +364,16 @@ int main(int argc, char* argv[]) { { const xcb_configure_notify_event_t *cfgEvent = (const xcb_configure_notify_event_t *)event; + int identifier = -1; + for(auto window : window_connections) { + if(window.window == cfgEvent->window) + identifier = window.identifier; + } + window_width = cfgEvent->width; window_height = cfgEvent->height; - engine->resize(0, {cfgEvent->width, cfgEvent->height}); + engine->resize(identifier, {cfgEvent->width, cfgEvent->height}); } break; case XCB_KEY_PRESS: diff --git a/tools/shadercompiler/main.cpp b/tools/shadercompiler/main.cpp index 816fecd..5a04605 100755 --- a/tools/shadercompiler/main.cpp +++ b/tools/shadercompiler/main.cpp @@ -38,7 +38,7 @@ int main(int argc, char* argv[]) { return 0; } else { - ShaderStage stage; + ShaderStage stage = ShaderStage::Vertex; if(has_extension(source_path, ".vert")) stage = ShaderStage::Vertex; else if(has_extension(source_path, ".frag"))