diff options
-rw-r--r-- | src/sprite.cpp | 15 | ||||
-rw-r--r-- | src/view_2d.hpp | 2 | ||||
-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 |
6 files changed, 116 insertions, 29 deletions
diff --git a/src/sprite.cpp b/src/sprite.cpp index 8f25f03..dba9e22 100644 --- a/src/sprite.cpp +++ b/src/sprite.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. @@ -63,17 +63,17 @@ cg_cSprite_draw(mrb_state *mrb, mrb_value self) { mrb_value view_value; VK::View2D *view_2d; - mrb_float x, y, w, h; + mrb_float x, y, w, h, z_index{0.0}; auto ptr = (std::shared_ptr<VK::Sprite>*)DATA_PTR(self); - mrb_get_args(mrb, "offff", &view_value, &x, &y, &w, &h); + mrb_get_args(mrb, "offff|f", &view_value, &x, &y, &w, &h, &z_index); view_2d = cg_cView_to_view_2d(mrb, view_value); glm::vec4 rect(x, y, x + w, y + h); - auto &positions = view_2d->sprites_to_draw[ - cg_core.vk_swapchain->current_frame][*ptr]; - positions.push_back(rect); + auto &sprites_to_draw = view_2d->sprites_to_draw[ + cg_core.vk_swapchain->current_frame]; + sprites_to_draw.emplace_back(*ptr, rect, z_index); return self; } @@ -88,5 +88,6 @@ cg_sprite_init(mrb_state *mrb) MRB_SET_INSTANCE_TT(cg_cSprite, MRB_TT_DATA); mrb_define_method( mrb, cg_cSprite, "initialize", cg_cSprite_initialize, MRB_ARGS_REQ(5)); - mrb_define_method(mrb, cg_cSprite, "draw", cg_cSprite_draw, MRB_ARGS_REQ(5)); + mrb_define_method(mrb, cg_cSprite, "draw", cg_cSprite_draw, MRB_ARGS_REQ(5)| + MRB_ARGS_OPT(1)); } diff --git a/src/view_2d.hpp b/src/view_2d.hpp index c2b1701..f2ff5b6 100644 --- a/src/view_2d.hpp +++ b/src/view_2d.hpp @@ -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. 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(); |