diff options
author | Frederico Linhares <fred@linhares.blue> | 2023-06-14 17:16:37 -0300 |
---|---|---|
committer | Frederico Linhares <fred@linhares.blue> | 2023-06-17 16:41:47 -0300 |
commit | c4c96f51d36f47a2c7eaefa9f2537cf7ff2c827c (patch) | |
tree | 5aecec86dca4bd69160093d2a969c4a1371113d0 /src/vk | |
parent | 482d53c89c90ee2a74d14c1a3e6c2fdab4ce0004 (diff) |
feat Add z index when rendering sprites
* src/sprite.cpp: Add a new parameter to sprites to define the z index
of the image being rendered.
* src/vk/graphics_pipeline_2d_solid.cpp: Sort may not be the most
efficient algorithm, but it is easier to implement. If this code becomes
slow, it can be optimized with little or no changes to the Ruby
interface.
Diffstat (limited to 'src/vk')
-rw-r--r-- | src/vk/graphics_pipeline_2d_solid.cpp | 38 | ||||
-rw-r--r-- | src/vk/sprite_to_draw.cpp | 40 | ||||
-rw-r--r-- | src/vk/sprite_to_draw.hpp | 44 | ||||
-rw-r--r-- | src/vk/view_2d.hpp | 6 |
4 files changed, 107 insertions, 21 deletions
diff --git a/src/vk/graphics_pipeline_2d_solid.cpp b/src/vk/graphics_pipeline_2d_solid.cpp index 5d210be..380bb65 100644 --- a/src/vk/graphics_pipeline_2d_solid.cpp +++ b/src/vk/graphics_pipeline_2d_solid.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2022 Frederico de Oliveira Linhares + * Copyright 2022-2023 Frederico de Oliveira Linhares * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -299,33 +299,37 @@ GraphicsPipeline2DSolid::draw( vkCmdSetScissor(draw_command_buffer, 0, 1, &vk_scissor); } + vkCmdBindPipeline( + draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, + this->graphic_pipeline); + + // FIXME: I know sorting is expensive, but I need to figure out better what + // to do here. + std::sort(view->sprites_to_draw[current_frame].rbegin(), + view->sprites_to_draw[current_frame].rend()); + // Draw sprites - for(auto& [sprite, positions]: view->sprites_to_draw[current_frame]) + for(auto& sprite_to_draw: view->sprites_to_draw[current_frame]) { std::array<VkDescriptorSet, 2> vk_descriptor_sets{ view->descriptor_sets_2d[image_index], - sprite->descriptor_sets[image_index]}; + sprite_to_draw.sprite->descriptor_sets[image_index]}; VkDeviceSize offsets[]{0}; vkCmdBindDescriptorSets( draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, cg_core.vk_graphics_pipeline_2d_solid_layout->pipeline, 0, vk_descriptor_sets.size(), vk_descriptor_sets.data(), 0, nullptr); - vkCmdBindPipeline( - draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, - this->graphic_pipeline); vkCmdBindVertexBuffers( - draw_command_buffer, 0, 1, &sprite->vertex_buffer->buffer, offsets); - - for(auto i{0}; i < positions.size(); i++) - { - ODOVector4D position{positions[i]}; - vkCmdPushConstants( - draw_command_buffer, - cg_core.vk_graphics_pipeline_2d_solid_layout->pipeline, - VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(ODOVector4D), &position); - vkCmdDraw(draw_command_buffer, Sprite::vertex_count, 1, 0, 0); - } + draw_command_buffer, 0, 1, &sprite_to_draw.sprite->vertex_buffer->buffer, + offsets); + + ODOVector4D position{sprite_to_draw.position}; + vkCmdPushConstants( + draw_command_buffer, + cg_core.vk_graphics_pipeline_2d_solid_layout->pipeline, + VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(ODOVector4D), &position); + vkCmdDraw(draw_command_buffer, Sprite::vertex_count, 1, 0, 0); } // Prepare for the next frame. diff --git a/src/vk/sprite_to_draw.cpp b/src/vk/sprite_to_draw.cpp new file mode 100644 index 0000000..76ee847 --- /dev/null +++ b/src/vk/sprite_to_draw.cpp @@ -0,0 +1,40 @@ +/* + * Copyright 2022-2023 Frederico de Oliveira Linhares + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sprite_to_draw.hpp" + +namespace VK +{ + SpriteToDraw::SpriteToDraw( + std::shared_ptr<Sprite> sprite, const glm::vec4 &position, float z_index): + sprite{sprite}, + position{position}, + z_index{z_index} + { + } + + bool + operator<(const SpriteToDraw& a, const SpriteToDraw& b) + { + return a.z_index < b.z_index; + } + + bool + operator>(const SpriteToDraw& a, const SpriteToDraw& b) + { + return a.z_index > b.z_index; + } +} diff --git a/src/vk/sprite_to_draw.hpp b/src/vk/sprite_to_draw.hpp new file mode 100644 index 0000000..3bd6af3 --- /dev/null +++ b/src/vk/sprite_to_draw.hpp @@ -0,0 +1,44 @@ +/* + * Copyright 2022-2023 Frederico de Oliveira Linhares + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CANDY_GEAR_VK_SPRITES_TO_DRAW_2D_H +#define CANDY_GEAR_VK_SPRITES_TO_DRAW_2D_H 1 + +#include <memory> + +#include "core.hpp" +#include "sprite.hpp" + +namespace VK +{ + struct SpriteToDraw + { + std::shared_ptr<Sprite> sprite; + glm::vec4 position; + float z_index; + + SpriteToDraw(std::shared_ptr<Sprite> sprite, const glm::vec4 &position, + float z_index); + }; + + bool + operator<(const SpriteToDraw &a, const SpriteToDraw &b); + + bool + operator<(const SpriteToDraw &a, const SpriteToDraw &b); +} + +#endif /* CANDY_GEAR_VK_SPRITES_TO_DRAW_2D_H */ diff --git a/src/vk/view_2d.hpp b/src/vk/view_2d.hpp index caa3d0f..ead21e2 100644 --- a/src/vk/view_2d.hpp +++ b/src/vk/view_2d.hpp @@ -22,7 +22,7 @@ #include <vector> #include "core.hpp" -#include "sprite.hpp" +#include "sprite_to_draw.hpp" #include "rectangle.hpp" namespace VK @@ -40,9 +40,7 @@ struct View2D std::vector<VkDescriptorSet> descriptor_sets_2d; std::vector<std::vector<Rectangle>> rectangles_to_draw; - std::vector< - std::unordered_map<std::shared_ptr<Sprite>, std::vector<glm::vec4>>> - sprites_to_draw; + std::vector<std::vector<SpriteToDraw>> sprites_to_draw; View2D(glm::vec4 region, float projection_width, float projection_height); virtual ~View2D(); |