diff --git a/desktop/graphics/vulkan_adapter.cpp b/desktop/graphics/vulkan_adapter.cpp index 9cbe5d7..dbff074 100644 --- a/desktop/graphics/vulkan_adapter.cpp +++ b/desktop/graphics/vulkan_adapter.cpp @@ -50,7 +50,7 @@ auto getVertexFieldProperties() { namespace { std::vector tmp_readFile(const std::string &path) { - auto resource = __embedded_resources::getEmbeddedResource(path.c_str()); + auto resource = __embedded_resources::getEmbeddedResource(path); if (resource.data == nullptr) { // REPORT_ERROR @@ -59,7 +59,7 @@ std::vector tmp_readFile(const std::string &path) { exit(1); } - return std::vector(resource.data, resource.data + resource.length); + return {resource.data, resource.data + resource.length}; } } // namespace @@ -82,25 +82,26 @@ Adapter::Adapter(Vulkan &vulkan) VK_ATTACHMENT_LOAD_OP_CLEAR, VK_ATTACHMENT_STORE_OP_DONT_CARE, - {1.0f, 0}, + {1.0F, 0}, nullptr}); } -Adapter::~Adapter() { - // Do nothing -} +Adapter::~Adapter() = default; std::vector &Adapter::getAttachments() { return attachments; } +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing std::vector Adapter::loadVertexShader() { return tmp_readFile("shader.vert.spv"); } +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing std::vector Adapter::loadFragmentShader() { return tmp_readFile("shader.frag.spv"); } +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing VkVertexInputBindingDescription Adapter::getVertexInputBindingDescription() { VkVertexInputBindingDescription bindingDescription{}; bindingDescription.binding = 0; @@ -111,6 +112,7 @@ VkVertexInputBindingDescription Adapter::getVertexInputBindingDescription() { } std::vector +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing Adapter::getVertexInputAttributeDescriptions() { std::vector attributeDescriptions; @@ -163,122 +165,125 @@ struct DrawRequest { glm::mat4 modelTransform; }; +// NOLINTNEXTLINE: TODO std::vector pendingDrawCommands; +constexpr std::size_t PENDING_DRAW_COMMANDS_MAX_SIZE = 100000; + +// NOLINTNEXTLINE: TODO glm::mat4 currentModelTransform; + } // namespace -progressia::main::Texture::Texture(Backend backend) : backend(backend) {} +struct progressia::main::Texture::Backend { + progressia::desktop::Texture texture; +}; -progressia::main::Texture::~Texture() { - delete static_cast(this->backend); -} +progressia::main::Texture::Texture(std::unique_ptr backend) + : backend(std::move(backend)) {} -namespace { -struct PrimitiveBackend { +progressia::main::Texture::~Texture() = default; +struct Primitive::Backend { IndexedBuffer buf; progressia::main::Texture *tex; }; -} // namespace -Primitive::Primitive(Backend backend) : backend(backend) {} +Primitive::Primitive(std::unique_ptr backend) + : backend(std::move(backend)) {} -Primitive::~Primitive() { - delete static_cast(this->backend); -} +Primitive::~Primitive() = default; void Primitive::draw() { - auto backend = static_cast(this->backend); - - if (pendingDrawCommands.size() > 100000) { + if (pendingDrawCommands.size() > PENDING_DRAW_COMMANDS_MAX_SIZE) { backend->buf.getVulkan().getGint().flush(); } - pendingDrawCommands.push_back( - {static_cast(backend->tex->backend), - &backend->buf, currentModelTransform}); + pendingDrawCommands.push_back({&backend->tex->backend->texture, + &backend->buf, currentModelTransform}); } const progressia::main::Texture *Primitive::getTexture() const { - return static_cast(this->backend)->tex; + return backend->tex; } -View::View(Backend backend) : backend(backend) {} +struct View::Backend { + Adapter::ViewUniform::State state; +}; -View::~View() { - delete static_cast(this->backend); -} +View::View(std::unique_ptr backend) : backend(std::move(backend)) {} + +View::~View() = default; void View::configure(const glm::mat4 &proj, const glm::mat4 &view) { - - static_cast(this->backend) - ->update(proj, view); + backend->state.update(proj, view); } void View::use() { - auto backend = static_cast(this->backend); - backend->uniform->getVulkan().getGint().flush(); - backend->bind(); + backend->state.uniform->getVulkan().getGint().flush(); + backend->state.bind(); } -Light::Light(Backend backend) : backend(backend) {} +struct Light::Backend { + Adapter::LightUniform::State state; +}; -Light::~Light() { - delete static_cast(this->backend); -} +Light::Light(std::unique_ptr backend) : backend(std::move(backend)) {} +Light::~Light() = default; void Light::configure(const glm::vec3 &color, const glm::vec3 &from, float contrast, float softness) { - static_cast(this->backend) - ->update(Adapter::Light{glm::vec4(color, 1.0f), - glm::vec4(glm::normalize(from), 1.0f), contrast, - softness}); + backend->state.update(Adapter::Light{glm::vec4(color, 1.0F), + glm::vec4(glm::normalize(from), 1.0F), + contrast, softness}); } void Light::use() { - auto backend = static_cast(this->backend); - backend->uniform->getVulkan().getGint().flush(); - backend->bind(); + backend->state.uniform->getVulkan().getGint().flush(); + backend->state.bind(); } GraphicsInterface::GraphicsInterface(Backend backend) : backend(backend) {} -GraphicsInterface::~GraphicsInterface() { - // Do nothing -} +GraphicsInterface::~GraphicsInterface() = default; -progressia::main::Texture * +std::unique_ptr GraphicsInterface::newTexture(const progressia::main::Image &src) { - auto backend = new progressia::desktop::Texture( - src, *static_cast(this->backend)); + using Backend = progressia::main::Texture::Backend; - return new Texture(backend); + return std::make_unique( + std::unique_ptr(new Backend{progressia::desktop::Texture( + src, *static_cast(this->backend))})); } -Primitive * +std::unique_ptr GraphicsInterface::newPrimitive(const std::vector &vertices, const std::vector &indices, progressia::main::Texture *texture) { - auto backend = new PrimitiveBackend{ - IndexedBuffer(vertices.size(), indices.size(), - *static_cast(this->backend)), - texture}; + auto primitive = std::make_unique( + std::unique_ptr(new Primitive::Backend{ + IndexedBuffer(vertices.size(), indices.size(), + *static_cast(this->backend)), + texture})); - backend->buf.load(vertices.data(), indices.data()); + primitive->backend->buf.load(vertices.data(), indices.data()); - return new Primitive(backend); + return primitive; } -View *GraphicsInterface::newView() { - return new View(new Adapter::ViewUniform::State( - static_cast(this->backend)->getAdapter().createView())); +std::unique_ptr GraphicsInterface::newView() { + return std::make_unique(std::unique_ptr( + new View::Backend{Adapter::ViewUniform::State( + static_cast(this->backend)->getAdapter().createView())})); } -Light *GraphicsInterface::newLight() { - return new Light(new Adapter::LightUniform::State( - static_cast(this->backend)->getAdapter().createLight())); +std::unique_ptr GraphicsInterface::newLight() { + return std::make_unique( + std::unique_ptr(new Light::Backend{ + Adapter::LightUniform::State(static_cast(this->backend) + ->getAdapter() + .createLight())})); } glm::vec2 GraphicsInterface::getViewport() const { @@ -287,16 +292,17 @@ glm::vec2 GraphicsInterface::getViewport() const { return {extent.width, extent.height}; } +// NOLINTNEXTLINE(readability-convert-member-functions-to-static): future-proofing void GraphicsInterface::setModelTransform(const glm::mat4 &m) { currentModelTransform = m; } void GraphicsInterface::flush() { - auto commandBuffer = static_cast(this->backend) - ->getCurrentFrame() - ->getCommandBuffer(); - auto pipelineLayout = + auto *commandBuffer = static_cast(this->backend) + ->getCurrentFrame() + ->getCommandBuffer(); + auto *pipelineLayout = static_cast(this->backend)->getPipeline().getLayout(); progressia::desktop::Texture *lastTexture = nullptr; @@ -328,6 +334,7 @@ void GraphicsInterface::flush() { pendingDrawCommands.clear(); } +// NOLINTNEXTLINE: TODO float GraphicsInterface::tmp_getTime() { return glfwGetTime(); } uint64_t GraphicsInterface::getLastStartedFrame() { diff --git a/main/game.cpp b/main/game.cpp index 6d6ec90..5415c83 100644 --- a/main/game.cpp +++ b/main/game.cpp @@ -62,10 +62,10 @@ void initialize(GraphicsInterface &gintp) { debug("game init begin"); gint = &gintp; - texture1.reset( - gint->newTexture(progressia::main::loadImage("assets/texture.png"))); - texture2.reset( - gint->newTexture(progressia::main::loadImage("assets/texture2.png"))); + texture1 = + gint->newTexture(progressia::main::loadImage("assets/texture.png")); + texture2 = + gint->newTexture(progressia::main::loadImage("assets/texture2.png")); // Cube 1 { @@ -91,7 +91,7 @@ void initialize(GraphicsInterface &gintp) { c.normal = normal; } - cube1.reset(gint->newPrimitive(vertices, indices, &*texture1)); + cube1 = gint->newPrimitive(vertices, indices, &*texture1); } // Cube 2 @@ -118,11 +118,11 @@ void initialize(GraphicsInterface &gintp) { c.normal = normal; } - cube2.reset(gint->newPrimitive(vertices, indices, &*texture2)); + cube2 = gint->newPrimitive(vertices, indices, &*texture2); } - perspective.reset(gint->newView()); - light.reset(gint->newLight()); + perspective = gint->newView(); + light = gint->newLight(); debug("game init complete"); } diff --git a/main/rendering/graphics_interface.h b/main/rendering/graphics_interface.h index aff4242..c1b13d5 100644 --- a/main/rendering/graphics_interface.h +++ b/main/rendering/graphics_interface.h @@ -25,30 +25,26 @@ struct Vertex { }; class Texture : private progressia::main::NonCopyable { - public: - using Backend = void *; - private: - Backend backend; + struct Backend; + std::unique_ptr backend; + friend class GraphicsInterface; friend class Primitive; public: - Texture(Backend); + Texture(std::unique_ptr); ~Texture(); }; class Primitive : private progressia::main::NonCopyable { - public: - using Backend = void *; - private: - Backend backend; - + struct Backend; + std::unique_ptr backend; friend class GraphicsInterface; public: - Primitive(Backend); + Primitive(std::unique_ptr); ~Primitive(); void draw(); @@ -57,14 +53,13 @@ class Primitive : private progressia::main::NonCopyable { }; class View : private progressia::main::NonCopyable { - public: - using Backend = void *; - private: - Backend backend; + struct Backend; + std::unique_ptr backend; + friend class GraphicsInterface; public: - View(Backend); + View(std::unique_ptr); ~View(); void configure(const glm::mat4 &proj, const glm::mat4 &view); @@ -72,14 +67,13 @@ class View : private progressia::main::NonCopyable { }; class Light : private progressia::main::NonCopyable { - public: - using Backend = void *; - private: - Backend backend; + struct Backend; + std::unique_ptr backend; + friend class GraphicsInterface; public: - Light(Backend); + Light(std::unique_ptr); ~Light(); void configure(const glm::vec3 &color, const glm::vec3 &from, @@ -98,18 +92,18 @@ class GraphicsInterface : private progressia::main::NonCopyable { GraphicsInterface(Backend); ~GraphicsInterface(); - Texture *newTexture(const Image &); + std::unique_ptr newTexture(const Image &); - Primitive *newPrimitive(const std::vector &, - const std::vector &, - Texture *texture); + std::unique_ptr newPrimitive(const std::vector &, + const std::vector &, + Texture *texture); glm::vec2 getViewport() const; void setModelTransform(const glm::mat4 &); - View *newView(); - Light *newLight(); + std::unique_ptr newView(); + std::unique_ptr newLight(); void flush(); void startNextLayer(); diff --git a/tools/clang-tidy/clang-tidy.yml b/tools/clang-tidy/clang-tidy.yml index c0ec6c8..81fc730 100644 --- a/tools/clang-tidy/clang-tidy.yml +++ b/tools/clang-tidy/clang-tidy.yml @@ -14,7 +14,9 @@ Checks: "-*,\ -readability-else-after-return,\ -readability-named-parameter,\ -readability-use-anyofallof,\ - -cppcoreguidelines-pro-bounds-pointer-arithmetic" + -cppcoreguidelines-pro-bounds-pointer-arithmetic,\ + -performance-trivially-destructible,\ + -modernize-make-unique" # modernize-use-trailing-return-type # ignore reason: reduces readability @@ -47,3 +49,13 @@ Checks: "-*,\ # cppcoreguidelines-pro-bounds-pointer-arithmetic # ignore reason: infeasible to avoid in C++17 + +# performance-trivially-destructible +# ignore reason: breaks incapsulation too often + +# modernize-make-unique +# ignore reason: impossible with brace-init lists: +# struct S { int a; int b; }; +# std::unique_ptr(new S { 1, 2 }) // works +# std::make_unique(1, 2) // error +# std::make_unique({1, 2}) // error