diff options
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(); |