summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2022-12-21 14:46:09 -0300
committerFrederico Linhares <fred@linhares.blue>2022-12-23 11:08:59 -0300
commit1cd2d838bf1682e125d52d12ac6f2960df65c7e0 (patch)
tree6e2ae0881b826f94a99b90dc5478a96c85c6d043
parent993d033617eaeb582e0e5f0525fa51e1c83893da (diff)
refa Merge Rectangle into Vector4D
-rw-r--r--glsl/shader_2d_wired.frag.glsl11
-rw-r--r--glsl/shader_2d_wired.vert.glsl30
-rw-r--r--src/core.cpp2
-rw-r--r--src/rectangle.cpp300
-rw-r--r--src/sprite.cpp5
-rw-r--r--src/vector_4d.cpp151
-rw-r--r--src/vk/graphics_pipeline_2d_solid.cpp5
-rw-r--r--src/vk/graphics_pipeline_2d_solid_layout.hpp10
-rw-r--r--src/vk/graphics_pipeline_2d_wired.cpp222
-rw-r--r--src/vk/graphics_pipeline_2d_wired.hpp9
-rw-r--r--src/vk/graphics_pipeline_2d_wired_layout.cpp10
-rw-r--r--src/vk/graphics_pipeline_3d.cpp1
-rw-r--r--src/vk/graphics_pipeline_3d_layout.hpp22
-rw-r--r--src/vk/model.cpp1
-rw-r--r--src/vk/rectangle.cpp223
-rw-r--r--src/vk/rectangle.hpp32
-rw-r--r--src/vk/sprite.cpp5
-rw-r--r--src/vk/uniform_buffer_object.hpp (renamed from src/rectangle.hpp)50
-rw-r--r--src/vk/view_2d.cpp1
-rw-r--r--src/vk/view_2d.hpp2
-rw-r--r--src/vk/view_3d.cpp1
-rw-r--r--test/src/main.rb6
22 files changed, 431 insertions, 668 deletions
diff --git a/glsl/shader_2d_wired.frag.glsl b/glsl/shader_2d_wired.frag.glsl
index 0671223..569b950 100644
--- a/glsl/shader_2d_wired.frag.glsl
+++ b/glsl/shader_2d_wired.frag.glsl
@@ -15,16 +15,19 @@
*/
#version 450
+#extension GL_ARB_separate_shader_objects : enable
+
+layout(location = 0) in flat int in_instance_index;
layout(location = 0) out vec4 out_color;
-layout(set = 1, binding = 0) uniform UBOModel2D
+layout(set = 1, binding = 1) uniform UBOColors
{
- vec3 color;
-} ubo_model_2d;
+ vec3 colors[128];
+} ubo_colors;
void
main()
{
- out_color = vec4(ubo_model_2d.color, 1.0);
+ out_color = vec4(ubo_colors.colors[in_instance_index], 1.0);
}
diff --git a/glsl/shader_2d_wired.vert.glsl b/glsl/shader_2d_wired.vert.glsl
index 97330dc..28cc55f 100644
--- a/glsl/shader_2d_wired.vert.glsl
+++ b/glsl/shader_2d_wired.vert.glsl
@@ -15,16 +15,42 @@
*/
#version 450
+#extension GL_ARB_separate_shader_objects : enable
-layout(location = 0) in vec2 in_position;
+layout(location = 0) out int out_instance_index;
layout(set = 0, binding = 0) uniform UBOView2D
{
mat4 proj;
} ubo_view;
+layout(set = 1, binding = 0) uniform UBORectangles
+{
+ vec4 positions[128];
+} ubo_sprite_positions;
+
void
main()
{
- gl_Position = ubo_view.proj * vec4(in_position, 0.0, 1.0);
+ vec2 coordinate;
+ vec4 position = ubo_sprite_positions.positions[gl_InstanceIndex];
+
+ out_instance_index = gl_InstanceIndex;
+
+ switch(gl_VertexIndex)
+ {
+ case 0:
+ coordinate = vec2(position.x, position.y);
+ break;
+ case 1:
+ coordinate = vec2(position.x, position.w);
+ break;
+ case 2:
+ coordinate = vec2(position.z, position.y);
+ break;
+ case 3:
+ coordinate = vec2(position.z, position.w);
+ break;
+ }
+ gl_Position = ubo_view.proj * vec4(coordinate, 0.0, 1.0);
}
diff --git a/src/core.cpp b/src/core.cpp
index ae8db70..0de2dbd 100644
--- a/src/core.cpp
+++ b/src/core.cpp
@@ -21,7 +21,6 @@
#include "key.hpp"
#include "mesh.hpp"
#include "model.hpp"
-#include "rectangle.hpp"
#include "rotation_3d.hpp"
#include "sound.hpp"
#include "sprite.hpp"
@@ -673,7 +672,6 @@ load_mruby_interface(void *obj)
cg_key_init(cg_core.mrb);
cg_mesh_init(cg_core.mrb);
cg_model_init(cg_core.mrb);
- cg_rectangle_init(cg_core.mrb);
cg_rotation_3d_init(cg_core.mrb);
cg_sound_init(cg_core.mrb);
cg_sprite_init(cg_core.mrb);
diff --git a/src/rectangle.cpp b/src/rectangle.cpp
deleted file mode 100644
index f678cc3..0000000
--- a/src/rectangle.cpp
+++ /dev/null
@@ -1,300 +0,0 @@
-/*
- * Copyright 2022 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 "rectangle.hpp"
-
-#include <mruby/array.h>
-
-#include "vector_3d.hpp"
-#include "view_2d.hpp"
-#include "view_3d.hpp"
-
-void
-cg_free_rectangle(mrb_state *mrb, void* obj)
-{
- auto ptr = static_cast<std::shared_ptr<VK::Rectangle>*>(obj);
-
- ptr->~shared_ptr();
- mrb_free(mrb, ptr);
-}
-
-const struct mrb_data_type cg_rectangle_type = {
- "CG_Rectangle", cg_free_rectangle };
-
-static mrb_value
-cg_cRectangle_initialize(mrb_state *mrb, mrb_value self)
-{
- mrb_float x, y, width, height;
- std::shared_ptr<VK::Rectangle> *ptr;
-
- mrb_get_args(mrb, "ffff", &x, &y, &width, &height);
- ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
- if(ptr) mrb_free(mrb, ptr);
- ptr = (std::shared_ptr<VK::Rectangle>*)mrb_malloc(
- mrb, sizeof(std::shared_ptr<VK::Rectangle>));
-
- new(ptr)std::shared_ptr<VK::Rectangle>(
- std::make_shared<VK::Rectangle>(x, y, width, height));
-
- mrb_data_init(self, ptr, &cg_rectangle_type);
- return self;
-}
-
-static mrb_value
-cg_cRectangle_get_x(mrb_state *mrb, mrb_value self)
-{
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- return mrb_float_value(mrb, (*ptr)->x);
-}
-
-static mrb_value
-cg_cRectangle_set_x(mrb_state *mrb, mrb_value self)
-{
- mrb_float x;
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_get_args(mrb, "f", &x);
-
- (*ptr)->x = x;
-
- return mrb_float_value(mrb, (*ptr)->x);
-}
-
-static mrb_value
-cg_cRectangle_get_y(mrb_state *mrb, mrb_value self)
-{
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- return mrb_float_value(mrb, (*ptr)->y);
-}
-
-static mrb_value
-cg_cRectangle_set_y(mrb_state *mrb, mrb_value self)
-{
- mrb_float y;
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_get_args(mrb, "f", &y);
-
- (*ptr)->y = y;
-
- return mrb_float_value(mrb, (*ptr)->y);
-}
-
-static mrb_value
-cg_cRectangle_get_width(mrb_state *mrb, mrb_value self)
-{
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- return mrb_float_value(mrb, (*ptr)->width);
-}
-
-static mrb_value
-cg_cRectangle_set_width(mrb_state *mrb, mrb_value self)
-{
- mrb_float width;
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_get_args(mrb, "f", &width);
-
- (*ptr)->width = width;
-
- return mrb_float_value(mrb, (*ptr)->width);
-}
-
-static mrb_value
-cg_cRectangle_get_height(mrb_state *mrb, mrb_value self)
-{
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- return mrb_float_value(mrb, (*ptr)->height);
-}
-
-static mrb_value
-cg_cRectangle_set_height(mrb_state *mrb, mrb_value self)
-{
- mrb_float height;
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_get_args(mrb, "f", &height);
-
- (*ptr)->height = height;
-
- return mrb_float_value(mrb, (*ptr)->height);
-}
-
-static mrb_value
-cg_cRectangle_get_xy(mrb_state *mrb, mrb_value self)
-{
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_value array = mrb_ary_new_capa(mrb, 2);
- mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->x));
- mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->y));
-
- return array;
-}
-
-static mrb_value
-cg_cRectangle_get_width_height(mrb_state *mrb, mrb_value self)
-{
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_value array = mrb_ary_new_capa(mrb, 2);
- mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->width));
- mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->height));
-
- return array;
-}
-
-static mrb_value
-cg_cRectangle_set_xy(mrb_state *mrb, mrb_value self)
-{
- mrb_float x, y;
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_get_args(mrb, "ff", &x, &y);
-
- (*ptr)->x = x;
- (*ptr)->y = y;
-
- return self;
-}
-
-static mrb_value
-cg_cRectangle_set_width_height(mrb_state *mrb, mrb_value self)
-{
- mrb_float width, height;
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_get_args(mrb, "ff", &width, &height);
-
- (*ptr)->width = width;
- (*ptr)->height = height;
-
- return self;
-}
-
-static mrb_value
-cg_cRectangle_align_vertically(mrb_state *mrb, mrb_value self)
-{
- std::shared_ptr<VK::Rectangle> *ptr, *that;
-
- mrb_get_args(mrb, "d", &that, &cg_rectangle_type);
- ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- return mrb_bool_value((*ptr)->align_vertically(**that));
-}
-
-static mrb_value
-cg_cRectangle_align_horizontally(mrb_state *mrb, mrb_value self)
-{
- std::shared_ptr<VK::Rectangle> *ptr, *that;
-
- mrb_get_args(mrb, "d", &that, &cg_rectangle_type);
- ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- return mrb_bool_value((*ptr)->align_horizontally(**that));
-}
-
-static mrb_value
-cg_cRectangle_collide(mrb_state *mrb, mrb_value self)
-{
- std::shared_ptr<VK::Rectangle> *ptr, *that;
-
- mrb_get_args(mrb, "d", &that, &cg_rectangle_type);
- ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- return mrb_bool_value((*ptr)->collide(**that));
-}
-
-static mrb_value
-cg_cRectangle_draw(mrb_state *mrb, mrb_value self)
-{
- mrb_value view_value;
- VK::View2D *view_2d;
- std::shared_ptr<glm::vec3> *color;
- auto ptr = (std::shared_ptr<VK::Rectangle>*)DATA_PTR(self);
-
- mrb_get_args(mrb, "od", &view_value, &color, &cg_vector_3d_type);
-
- view_2d = cg_cView_to_view_2d(mrb, view_value);
-
- VK::UBOModel2D model;
- model.color = **color;
- (*ptr)->ub_rectangle[cg_core.vk_swapchain->current_frame].copy_data(&model);
- (*ptr)->update_vertex_buffer();
- auto &rectangles = view_2d->rectangles_to_draw[
- cg_core.vk_swapchain->current_frame];
- rectangles.push_back(*ptr);
-
- return self;
-}
-
-void
-cg_rectangle_init(mrb_state *mrb)
-{
- RClass *cg_m, *cg_cRectangle;
-
- cg_m = mrb_module_get(mrb, "CandyGear");
- cg_cRectangle = mrb_define_class_under(
- mrb, cg_m, "Rectangle", mrb->object_class);
- MRB_SET_INSTANCE_TT(cg_cRectangle, MRB_TT_DATA);
- mrb_define_method(
- mrb, cg_cRectangle, "initialize", cg_cRectangle_initialize,
- MRB_ARGS_REQ(4));
- mrb_define_method(
- mrb, cg_cRectangle, "x", cg_cRectangle_get_x, MRB_ARGS_NONE());
- mrb_define_method(
- mrb, cg_cRectangle, "y", cg_cRectangle_get_y, MRB_ARGS_NONE());
- mrb_define_method(
- mrb, cg_cRectangle, "width", cg_cRectangle_get_width, MRB_ARGS_NONE());
- mrb_define_method(
- mrb, cg_cRectangle, "height", cg_cRectangle_get_height, MRB_ARGS_NONE());
-
- mrb_define_method(
- mrb, cg_cRectangle, "x=", cg_cRectangle_set_x, MRB_ARGS_REQ(1));
- mrb_define_method(
- mrb, cg_cRectangle, "y=", cg_cRectangle_set_y, MRB_ARGS_REQ(1));
- mrb_define_method(
- mrb, cg_cRectangle, "width=", cg_cRectangle_set_width, MRB_ARGS_REQ(1));
- mrb_define_method(
- mrb, cg_cRectangle, "height=", cg_cRectangle_set_height, MRB_ARGS_REQ(1));
-
- mrb_define_method(
- mrb, cg_cRectangle, "xy", cg_cRectangle_get_xy, MRB_ARGS_NONE());
- mrb_define_method(
- mrb, cg_cRectangle, "width_height", cg_cRectangle_get_width_height,
- MRB_ARGS_NONE());
-
- mrb_define_method(
- mrb, cg_cRectangle, "set_xy", cg_cRectangle_set_xy, MRB_ARGS_REQ(2));
- mrb_define_method(
- mrb, cg_cRectangle, "set_width_height", cg_cRectangle_set_width_height,
- MRB_ARGS_REQ(2));
-
- mrb_define_method(
- mrb, cg_cRectangle, "align_vertically?", cg_cRectangle_align_vertically,
- MRB_ARGS_REQ(1));
- mrb_define_method(
- mrb, cg_cRectangle, "align_horizontally?",
- cg_cRectangle_align_horizontally, MRB_ARGS_REQ(1));
- mrb_define_method(
- mrb, cg_cRectangle, "collide?", cg_cRectangle_collide, MRB_ARGS_REQ(1));
- mrb_define_method(
- mrb, cg_cRectangle, "draw", cg_cRectangle_draw, MRB_ARGS_REQ(2));
-}
diff --git a/src/sprite.cpp b/src/sprite.cpp
index fa5e22f..c72f7c5 100644
--- a/src/sprite.cpp
+++ b/src/sprite.cpp
@@ -65,9 +65,12 @@ cg_cSprite_draw(mrb_state *mrb, mrb_value self)
view_2d = cg_cView_to_view_2d(mrb, view_value);
+ glm::vec4 rect(
+ (*position)->x, (*position)->y,
+ (*position)->x + (*position)->z, (*position)->y + (*position)->w);
auto &positions = view_2d->sprites_to_draw[
cg_core.vk_swapchain->current_frame][*ptr];
- positions.push_back(*position->get());
+ positions.push_back(rect);
return self;
}
diff --git a/src/vector_4d.cpp b/src/vector_4d.cpp
index 85a265e..7bdcbab 100644
--- a/src/vector_4d.cpp
+++ b/src/vector_4d.cpp
@@ -19,9 +19,30 @@
#include <memory>
#include <glm/vec4.hpp>
-
#include <mruby/array.h>
+#include "vector_3d.hpp"
+#include "view_2d.hpp"
+
+namespace
+{
+
+constexpr bool
+align_vertically(const float a_x, const float a_width,
+ const float b_x, const float b_width)
+{
+ return a_x <= b_x + b_width && a_x + a_width >= b_x;
+}
+
+constexpr bool
+align_horizontally(const float a_y, const float a_height,
+ const float b_y, const float b_height)
+{
+ return a_y <= b_y + b_height && a_y + a_height >= b_y;
+}
+
+}
+
void
cg_free_vector_4d(mrb_state *mrb, void* obj)
{
@@ -67,8 +88,7 @@ cg_cVector4D_get_x(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_y(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
return mrb_float_value(mrb, (*ptr)->y);
}
@@ -76,8 +96,7 @@ cg_cVector4D_get_y(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_w(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
return mrb_float_value(mrb, (*ptr)->z);
}
@@ -85,8 +104,7 @@ cg_cVector4D_get_w(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_h(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
return mrb_float_value(mrb, (*ptr)->w);
}
@@ -94,8 +112,7 @@ cg_cVector4D_get_h(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_xy(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto *ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_value array = mrb_ary_new_capa(mrb, 2);
mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->x));
@@ -107,8 +124,7 @@ cg_cVector4D_get_xy(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_xw(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_value array = mrb_ary_new_capa(mrb, 2);
mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->x));
@@ -120,8 +136,7 @@ cg_cVector4D_get_xw(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_xh(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_value array = mrb_ary_new_capa(mrb, 2);
mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->x));
@@ -133,8 +148,7 @@ cg_cVector4D_get_xh(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_yw(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_value array = mrb_ary_new_capa(mrb, 2);
mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->y));
@@ -146,8 +160,7 @@ cg_cVector4D_get_yw(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_yh(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_value array = mrb_ary_new_capa(mrb, 2);
mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->y));
@@ -159,8 +172,7 @@ cg_cVector4D_get_yh(mrb_state *mrb, mrb_value self)
mrb_value
cg_cVector4D_get_wh(mrb_state *mrb, mrb_value self)
{
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_value array = mrb_ary_new_capa(mrb, 2);
mrb_ary_push(mrb, array, mrb_float_value(mrb, (*ptr)->z));
@@ -173,8 +185,7 @@ static mrb_value
cg_cVector4D_set_x(mrb_state *mrb, mrb_value self)
{
mrb_float x;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "f", &x);
(*ptr)->x = x;
@@ -186,8 +197,7 @@ static mrb_value
cg_cVector4D_set_y(mrb_state *mrb, mrb_value self)
{
mrb_float y;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "f", &y);
(*ptr)->y = y;
@@ -199,8 +209,7 @@ static mrb_value
cg_cVector4D_set_w(mrb_state *mrb, mrb_value self)
{
mrb_float w;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "f", &w);
(*ptr)->z = w;
@@ -212,8 +221,7 @@ static mrb_value
cg_cVector4D_set_h(mrb_state *mrb, mrb_value self)
{
mrb_float h;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "f", &h);
(*ptr)->w = h;
@@ -225,8 +233,7 @@ static mrb_value
cg_cVector4D_set_xy(mrb_state *mrb, mrb_value self)
{
mrb_float x, y;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "ff", &x, &y);
(*ptr)->x = x;
@@ -239,8 +246,7 @@ static mrb_value
cg_cVector4D_set_xw(mrb_state *mrb, mrb_value self)
{
mrb_float x, w;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "ff", &x, &w);
(*ptr)->x = x;
@@ -253,8 +259,7 @@ static mrb_value
cg_cVector4D_set_xh(mrb_state *mrb, mrb_value self)
{
mrb_float x, h;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "ff", &x, &h);
(*ptr)->x = x;
@@ -267,8 +272,7 @@ static mrb_value
cg_cVector4D_set_yw(mrb_state *mrb, mrb_value self)
{
mrb_float y, w;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "ff", &y, &w);
(*ptr)->y = y;
@@ -281,8 +285,7 @@ static mrb_value
cg_cVector4D_set_yh(mrb_state *mrb, mrb_value self)
{
mrb_float y, h;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "ff", &y, &h);
(*ptr)->x = y;
@@ -295,8 +298,7 @@ static mrb_value
cg_cVector4D_set_wh(mrb_state *mrb, mrb_value self)
{
mrb_float w, h;
- std::shared_ptr<glm::vec4> *ptr =
- (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
mrb_get_args(mrb, "ff", &w, &h);
(*ptr)->z = w;
@@ -305,6 +307,65 @@ cg_cVector4D_set_wh(mrb_state *mrb, mrb_value self)
return self;
}
+static mrb_value
+cg_cVector4D_align_vertically(mrb_state *mrb, mrb_value self)
+{
+ std::shared_ptr<glm::vec4> *ptr, *that;
+
+ mrb_get_args(mrb, "d", &that, &cg_vector_4d_type);
+ ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ return mrb_bool_value(
+ align_vertically((*ptr)->x, (*ptr)->z, (*that)->x, (*that)->z));
+}
+
+static mrb_value
+cg_cVector4D_align_horizontally(mrb_state *mrb, mrb_value self)
+{
+ std::shared_ptr<glm::vec4> *ptr, *that;
+
+ mrb_get_args(mrb, "d", &that, &cg_vector_4d_type);
+ ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ return mrb_bool_value(
+ align_horizontally((*ptr)->y, (*ptr)->w, (*that)->y, (*that)->w));
+}
+
+static mrb_value
+cg_cVector4D_collide(mrb_state *mrb, mrb_value self)
+{
+ std::shared_ptr<glm::vec4> *ptr, *that;
+
+ mrb_get_args(mrb, "d", &that, &cg_vector_4d_type);
+ ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ return mrb_bool_value(
+ align_vertically((*ptr)->x, (*ptr)->z, (*that)->x, (*that)->z) &&
+ align_horizontally((*ptr)->y, (*ptr)->w, (*that)->y, (*that)->w));
+}
+
+static mrb_value
+cg_cVector4D_draw_rectangle(mrb_state *mrb, mrb_value self)
+{
+ mrb_value view_value;
+ VK::View2D *view_2d;
+ std::shared_ptr<glm::vec3> *color;
+ auto ptr = (std::shared_ptr<glm::vec4>*)DATA_PTR(self);
+
+ mrb_get_args(mrb, "od", &view_value, &color, &cg_vector_3d_type);
+
+ view_2d = cg_cView_to_view_2d(mrb, view_value);
+
+ glm::vec4 position(
+ (*ptr)->x, (*ptr)->y, (*ptr)->x + (*ptr)->z, (*ptr)->y + (*ptr)->w);
+ VK::Rectangle rect{position, (**color)};
+ auto &rectangles = view_2d->rectangles_to_draw[
+ cg_core.vk_swapchain->current_frame];
+ rectangles.push_back(rect);
+
+ return self;
+}
+
void
cg_vector_4d_init(mrb_state *mrb)
{
@@ -403,4 +464,16 @@ cg_vector_4d_init(mrb_state *mrb)
mrb, cg_cVector4D, "set_ga", cg_cVector4D_set_yh, MRB_ARGS_REQ(2));
mrb_define_method(
mrb, cg_cVector4D, "set_ba", cg_cVector4D_set_wh, MRB_ARGS_REQ(2));
+
+ mrb_define_method(
+ mrb, cg_cVector4D, "align_vertically?", cg_cVector4D_align_vertically,
+ MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "align_horizontally?",
+ cg_cVector4D_align_horizontally, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "collide?", cg_cVector4D_collide, MRB_ARGS_REQ(1));
+ mrb_define_method(
+ mrb, cg_cVector4D, "draw_rectangle", cg_cVector4D_draw_rectangle,
+ MRB_ARGS_REQ(2));
}
diff --git a/src/vk/graphics_pipeline_2d_solid.cpp b/src/vk/graphics_pipeline_2d_solid.cpp
index 3906cc7..b7caaa7 100644
--- a/src/vk/graphics_pipeline_2d_solid.cpp
+++ b/src/vk/graphics_pipeline_2d_solid.cpp
@@ -20,6 +20,7 @@
#include "../core.hpp"
#include "sprite.hpp"
+#include "uniform_buffer_object.hpp"
namespace
{
@@ -319,9 +320,9 @@ GraphicsPipeline2DSolid::draw(
draw_command_buffer, Sprite::vertex_count, positions.size(), 0, 0);
}
- VK::UBOSpritePositions ubo_sprite_positions;
+ UBOVectors4D ubo_sprite_positions;
for(auto i{0}; i < positions.size(); i++)
- ubo_sprite_positions.positions[i] = positions[i];
+ ubo_sprite_positions.vectors[i] = positions[i];
sprite->ub_sprite_positions[image_index].copy_data(
&ubo_sprite_positions);
}
diff --git a/src/vk/graphics_pipeline_2d_solid_layout.hpp b/src/vk/graphics_pipeline_2d_solid_layout.hpp
index 121ddaa..b04d239 100644
--- a/src/vk/graphics_pipeline_2d_solid_layout.hpp
+++ b/src/vk/graphics_pipeline_2d_solid_layout.hpp
@@ -22,16 +22,6 @@
namespace VK
{
-struct UBOView2D
-{
- glm::mat4 proj;
-};
-
-struct UBOSpritePositions
-{
- glm::vec4 positions[128];
-};
-
struct GraphicsPipeline2DSolidLayout
{
VkDescriptorSetLayout descriptor_set_view;
diff --git a/src/vk/graphics_pipeline_2d_wired.cpp b/src/vk/graphics_pipeline_2d_wired.cpp
index 2b416c2..34b6a1b 100644
--- a/src/vk/graphics_pipeline_2d_wired.cpp
+++ b/src/vk/graphics_pipeline_2d_wired.cpp
@@ -21,6 +21,7 @@
#include "../core.hpp"
#include "rectangle.hpp"
#include "sprite.hpp"
+#include "uniform_buffer_object.hpp"
namespace
{
@@ -97,27 +98,15 @@ load_pipeline(void *obj)
frag_shader_stage_info
};
- VkVertexInputBindingDescription vertex_input_binding{};
- vertex_input_binding.binding = 0;
- vertex_input_binding.stride = sizeof(glm::vec2);
- vertex_input_binding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
-
- std::array<VkVertexInputAttributeDescription, 1> vertex_attribute{};
- vertex_attribute[0].location = 0;
- vertex_attribute[0].binding = 0;
- vertex_attribute[0].format = VK_FORMAT_R32G32_SFLOAT;
- vertex_attribute[0].offset = 0;
-
VkPipelineVertexInputStateCreateInfo vertex_input_info = {};
vertex_input_info.sType =
VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
vertex_input_info.pNext = nullptr;
vertex_input_info.flags = 0;
- vertex_input_info.vertexBindingDescriptionCount = 1;
- vertex_input_info.pVertexBindingDescriptions = &vertex_input_binding;
- vertex_input_info.vertexAttributeDescriptionCount =
- static_cast<uint32_t>(vertex_attribute.size());
- vertex_input_info.pVertexAttributeDescriptions = vertex_attribute.data();
+ vertex_input_info.vertexBindingDescriptionCount = 0;
+ vertex_input_info.pVertexBindingDescriptions = nullptr;
+ vertex_input_info.vertexAttributeDescriptionCount = 0;
+ vertex_input_info.pVertexAttributeDescriptions = nullptr;
VkPipelineInputAssemblyStateCreateInfo input_assembly = {};
input_assembly.sType =
@@ -250,9 +239,182 @@ unload_pipeline(void *obj)
cg_core.vk_device_with_swapchain->device, self->graphic_pipeline, nullptr);
}
+void
+load_indexes(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ self->queue_family =
+ cg_core.vk_device_with_swapchain->get_queue_family_with_graphics();
+
+ std::array<uint32_t, 4> indexes{0, 1, 2, 3};
+ void *indexes_data{indexes.data()};
+ size_t indexes_size{sizeof(indexes[0]) * indexes.size()};
+ VK::SourceBuffer source_index_buffer{
+ self->queue_family->device, indexes_data, indexes_size};
+ self->index_buffer = new VK::DestinationBuffer{
+ self->queue_family, &source_index_buffer,
+ VK_BUFFER_USAGE_INDEX_BUFFER_BIT};
+}
+
+void
+unload_indexes(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ delete self->index_buffer;
+}
+
+void
+load_uniform_rectangles_buffers(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ self->ub_rectangles.reserve(
+ cg_core.vk_swapchain->images_count);
+ for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++)
+ self->ub_rectangles.emplace_back(
+ cg_core.vk_device_with_swapchain, sizeof(VK::UBOVectors4D));
+}
+
+void
+unload_uniform_rectangles_buffers(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ self->ub_rectangles.clear();
+}
+
+void
+load_uniform_rectangle_colors_buffers(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ self->ub_rectangle_colors.reserve(
+ cg_core.vk_swapchain->images_count);
+ for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++)
+ self->ub_rectangle_colors.emplace_back(
+ cg_core.vk_device_with_swapchain, sizeof(VK::UBOVectors3D));
+}
+
+void
+unload_uniform_rectangle_colors_buffers(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ self->ub_rectangle_colors.clear();
+}
+
+void
+load_descriptor_set_pool(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ std::array<VkDescriptorPoolSize, 1> descriptor_pool_sizes{};
+ descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
+ descriptor_pool_sizes[0].descriptorCount =
+ self->ub_rectangles.size() + self->ub_rectangle_colors.size();
+
+ VkDescriptorPoolCreateInfo pool_info{};
+ pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
+ pool_info.pNext = nullptr;
+ pool_info.flags = 0;
+ pool_info.maxSets =
+ self->ub_rectangles.size() + self->ub_rectangle_colors.size();
+ pool_info.poolSizeCount = descriptor_pool_sizes.size();
+ pool_info.pPoolSizes = descriptor_pool_sizes.data();
+
+ if(vkCreateDescriptorPool(
+ self->queue_family->device->device, &pool_info, nullptr,
+ &self->descriptor_pool) != VK_SUCCESS)
+ throw CommandError{"Failed to create a Vulkan descriptor pool."};
+}
+
+void
+unload_descriptor_set_pool(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ vkDestroyDescriptorPool(
+ self->queue_family->device->device, self->descriptor_pool, nullptr);
+}
+
+void
+load_descriptor_sets(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ std::vector<VkDescriptorSetLayout> layouts(
+ cg_core.vk_swapchain->images_count,
+ cg_core.vk_graphics_pipeline_2d_wired_layout->descriptor_set_instance);
+
+ VkDescriptorSetAllocateInfo alloc_info{};
+ alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
+ alloc_info.descriptorPool = self->descriptor_pool;
+ alloc_info.descriptorSetCount = layouts.size();
+ alloc_info.pSetLayouts = layouts.data();
+
+ self->descriptor_sets.resize(layouts.size());
+ if(vkAllocateDescriptorSets(
+ self->queue_family->device->device, &alloc_info,
+ self->descriptor_sets.data()) != VK_SUCCESS)
+ CommandError{"Failed to create Vulkan descriptor set."};
+}
+
+void
+load_data_to_descriptor_sets(void *obj)
+{
+ auto self = static_cast<VK::GraphicsPipeline2DWired*>(obj);
+
+ for(auto i{0}; i < self->ub_rectangles.size(); i++)
+ {
+ VkDescriptorBufferInfo rectangles_buffer_info{};
+ rectangles_buffer_info.buffer = self->ub_rectangles[i].buffer;
+ rectangles_buffer_info.offset = 0;
+ rectangles_buffer_info.range = sizeof(VK::UBOVectors4D);
+
+ VkDescriptorBufferInfo colors_buffer_info{};
+ colors_buffer_info.buffer = self->ub_rectangle_colors[i].buffer;
+ colors_buffer_info.offset = 0;
+ colors_buffer_info.range = sizeof(VK::UBOVectors3D);
+
+ std::array<VkWriteDescriptorSet, 2> write_descriptors{};
+ write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
+ write_descriptors[0].dstSet = self->descriptor_sets[i];
+ write_descriptors[0].dstBinding = 0;
+ write_descriptors[0].dstArrayElement = 0;
+ write_descriptors[0].descriptorCount = 1;
+ write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
+ write_descriptors[0].pBufferInfo = &rectangles_buffer_info;
+ write_descriptors[0].pImageInfo = nullptr;
+ write_descriptors[0].pTexelBufferView = nullptr;
+
+ write_descriptors[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
+ write_descriptors[1].dstSet = self->descriptor_sets[i];
+ write_descriptors[1].dstBinding = 1;
+ write_descriptors[1].dstArrayElement = 0;
+ write_descriptors[1].descriptorCount = 1;
+ write_descriptors[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
+ write_descriptors[1].pBufferInfo = &colors_buffer_info;
+ write_descriptors[1].pImageInfo = nullptr;
+ write_descriptors[1].pTexelBufferView = nullptr;
+
+ vkUpdateDescriptorSets(
+ cg_core.vk_device_with_swapchain->device, write_descriptors.size(),
+ write_descriptors.data(), 0, nullptr);
+ }
+}
+
const CommandChain loader{
+ {&load_indexes, &unload_indexes},
{&load_framebuffer, &unload_framebuffer},
- {&load_pipeline, &unload_pipeline}
+ {&load_pipeline, &unload_pipeline},
+ {&load_uniform_rectangles_buffers, &unload_uniform_rectangles_buffers},
+ {&load_uniform_rectangle_colors_buffers,
+ &unload_uniform_rectangle_colors_buffers},
+ {&load_descriptor_set_pool, &unload_descriptor_set_pool},
+ {&load_descriptor_sets, nullptr},
+ {&load_data_to_descriptor_sets, nullptr}
};
}
@@ -296,11 +458,10 @@ GraphicsPipeline2DWired::draw(
}
// Draw rectangles
- for(auto& rectangle: view->rectangles_to_draw[current_frame])
{
std::array<VkDescriptorSet, 2> vk_descriptor_sets{
view->descriptor_sets_2d[image_index],
- rectangle->descriptor_sets[image_index]};
+ this->descriptor_sets[image_index]};
VkDeviceSize offsets[]{0};
vkCmdBindDescriptorSets(
@@ -310,11 +471,24 @@ GraphicsPipeline2DWired::draw(
vkCmdBindPipeline(
draw_command_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
this->graphic_pipeline);
- vkCmdBindVertexBuffers(
- draw_command_buffer, 0, 1,
- &rectangle->vertex_buffers[image_index].buffer, offsets);
- vkCmdDraw(
- draw_command_buffer, Rectangle::vertex_count, 1, 0, 0);
+ vkCmdBindIndexBuffer(
+ draw_command_buffer, this->index_buffer->buffer, 0,
+ VK_INDEX_TYPE_UINT32);
+ vkCmdDrawIndexed(
+ draw_command_buffer, Rectangle::VertexCount,
+ view->rectangles_to_draw[current_frame].size(),
+ 0, 0, 0);
+
+ UBOVectors4D ubo_rectangle_positions;
+ UBOVectors3D ubo_rectangle_colors;
+ for(auto i{0}; i < view->rectangles_to_draw[current_frame].size(); i++)
+ {
+ auto &rect = view->rectangles_to_draw[current_frame][i];
+ ubo_rectangle_positions.vectors[i] = rect.position;
+ ubo_rectangle_colors.vectors[i] = rect.color;
+ }
+ this->ub_rectangles[image_index].copy_data(&ubo_rectangle_positions);
+ this->ub_rectangle_colors[image_index].copy_data(&ubo_rectangle_colors);
}
// Prepare for the next frame.
diff --git a/src/vk/graphics_pipeline_2d_wired.hpp b/src/vk/graphics_pipeline_2d_wired.hpp
index e6c40de..aa975a8 100644
--- a/src/vk/graphics_pipeline_2d_wired.hpp
+++ b/src/vk/graphics_pipeline_2d_wired.hpp
@@ -29,9 +29,18 @@ namespace VK
struct GraphicsPipeline2DWired
{
+ QueueFamily *queue_family;
+
std::vector<VkFramebuffer> swapchain_framebuffers;
VkPipeline graphic_pipeline;
+ DestinationBuffer *index_buffer;
+ std::vector<UniformBuffer> ub_rectangles;
+ std::vector<UniformBuffer> ub_rectangle_colors;
+
+ VkDescriptorPool descriptor_pool;
+ std::vector<VkDescriptorSet> descriptor_sets;
+
GraphicsPipeline2DWired();
~GraphicsPipeline2DWired();
diff --git a/src/vk/graphics_pipeline_2d_wired_layout.cpp b/src/vk/graphics_pipeline_2d_wired_layout.cpp
index c523bc7..ae58a2f 100644
--- a/src/vk/graphics_pipeline_2d_wired_layout.cpp
+++ b/src/vk/graphics_pipeline_2d_wired_layout.cpp
@@ -66,14 +66,20 @@ load_descriptor_set_instance(void *obj)
{
auto self = static_cast<VK::GraphicsPipeline2DWiredLayout*>(obj);
- std::array<VkDescriptorSetLayoutBinding, 1> layout_bindings{};
+ std::array<VkDescriptorSetLayoutBinding, 2> layout_bindings{};
layout_bindings[0].binding = 0;
layout_bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
layout_bindings[0].descriptorCount = 1;
- layout_bindings[0].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
+ layout_bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
layout_bindings[0].pImmutableSamplers = nullptr;
+ layout_bindings[1].binding = 1;
+ layout_bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
+ layout_bindings[1].descriptorCount = 1;
+ layout_bindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
+ layout_bindings[1].pImmutableSamplers = nullptr;
+
VkDescriptorSetLayoutCreateInfo layout_info{};
layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layout_info.pNext = nullptr;
diff --git a/src/vk/graphics_pipeline_3d.cpp b/src/vk/graphics_pipeline_3d.cpp
index f08e43a..790cad7 100644
--- a/src/vk/graphics_pipeline_3d.cpp
+++ b/src/vk/graphics_pipeline_3d.cpp
@@ -23,6 +23,7 @@
#include "core.hpp"
#include "image.hpp"
#include "vertex_3d.hpp"
+#include "uniform_buffer_object.hpp"
namespace
{
diff --git a/src/vk/graphics_pipeline_3d_layout.hpp b/src/vk/graphics_pipeline_3d_layout.hpp
index d45a778..10c8bd9 100644
--- a/src/vk/graphics_pipeline_3d_layout.hpp
+++ b/src/vk/graphics_pipeline_3d_layout.hpp
@@ -22,28 +22,6 @@
namespace VK
{
-struct UBOWorld3D_Vert
-{
- glm::vec4 ambient_light_color;
-};
-
-struct UBOWorld3D_Frag
-{
- glm::vec3 directional_light_direction;
- glm::vec4 directional_light_color;
-};
-
-struct UBOView3D
-{
- glm::mat4 view;
- glm::mat4 proj;
-};
-
-struct UBOModelInstance
-{
- glm::mat4 instances[128];
-};
-
struct GraphicsPipeline3DLayout
{
VkDescriptorSetLayout descriptor_set_world;
diff --git a/src/vk/model.cpp b/src/vk/model.cpp
index d38342d..2aca963 100644
--- a/src/vk/model.cpp
+++ b/src/vk/model.cpp
@@ -19,6 +19,7 @@
#include <array>
#include "../core.hpp"
+#include "uniform_buffer_object.hpp"
namespace
{
diff --git a/src/vk/rectangle.cpp b/src/vk/rectangle.cpp
index b0925f1..dc18c45 100644
--- a/src/vk/rectangle.cpp
+++ b/src/vk/rectangle.cpp
@@ -16,230 +16,15 @@
#include "rectangle.hpp"
-#include <array>
-
-#include "../core.hpp"
-#include "graphics_pipeline_2d_wired_layout.hpp"
-
-namespace
-{
-
-void
-load_mesh(void *obj)
-{
- auto self = static_cast<VK::Rectangle*>(obj);
-
- self->queue_family =
- cg_core.vk_device_with_swapchain->get_queue_family_with_graphics();
-
- glm::vec2 rect[4]{
- glm::vec2{self->x, self->y},
- glm::vec2{self->x, self->y + self->height},
- glm::vec2{self->x + self->width, self->y},
- glm::vec2{self->x + self->width, self->y + self->height}
- };
-
- void *vertexes_data{&rect};
- static const size_t vertexes_size =
- sizeof(glm::vec2) * VK::Rectangle::vertex_count;
- self->source_buffers.reserve(cg_core.vk_swapchain->images_count);
- self->vertex_buffers.reserve(cg_core.vk_swapchain->images_count);
- for(int i{0}; i < cg_core.vk_swapchain->images_count; i++)
- {
- self->source_buffers.emplace_back(
- self->queue_family->device, vertexes_data, vertexes_size);
- self->vertex_buffers.emplace_back(
- self->queue_family, &self->source_buffers[i],
- VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
- }
-}
-
-void
-unload_mesh(void *obj)
-{
- auto self = static_cast<VK::Rectangle*>(obj);
-
- self->vertex_buffers.clear();
- self->source_buffers.clear();
-}
-
-void
-load_uniform_buffer(void *obj)
-{
- auto self = static_cast<VK::Rectangle*>(obj);
-
- self->ub_rectangle.reserve(cg_core.vk_swapchain->images_count);
- for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++)
- self->ub_rectangle.emplace_back(
- cg_core.vk_device_with_swapchain, sizeof(VK::UBOModel2D));
-}
-
-void
-unload_uniform_buffer(void *obj)
-{
- auto self = static_cast<VK::Rectangle*>(obj);
-
- self->ub_rectangle.clear();
-}
-
-void
-load_descriptor_set_pool(void *obj)
-{
- auto self = static_cast<VK::Rectangle*>(obj);
-
- std::array<VkDescriptorPoolSize, 1> descriptor_pool_sizes{};
- descriptor_pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
- descriptor_pool_sizes[0].descriptorCount = self->ub_rectangle.size();
-
- VkDescriptorPoolCreateInfo pool_info{};
- pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
- pool_info.pNext = nullptr;
- pool_info.flags = 0;
- pool_info.maxSets = self->ub_rectangle.size();
- pool_info.poolSizeCount = descriptor_pool_sizes.size();
- pool_info.pPoolSizes = descriptor_pool_sizes.data();
-
- if(vkCreateDescriptorPool(
- self->queue_family->device->device, &pool_info, nullptr,
- &self->descriptor_pool) != VK_SUCCESS)
- throw CommandError{"Failed to create a Vulkan descriptor pool."};
-}
-
-void
-unload_descriptor_set_pool(void *obj)
-{
- auto self = static_cast<VK::Rectangle*>(obj);
-
- vkDestroyDescriptorPool(
- self->queue_family->device->device, self->descriptor_pool, nullptr);
-}
-
-void
-load_descriptor_sets(void *obj)
-{
- auto self = static_cast<VK::Rectangle*>(obj);
-
- std::vector<VkDescriptorSetLayout> layouts(
- cg_core.vk_swapchain->images_count,
- cg_core.vk_graphics_pipeline_2d_wired_layout->descriptor_set_instance);
-
- VkDescriptorSetAllocateInfo alloc_info{};
- alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
- alloc_info.descriptorPool = self->descriptor_pool;
- alloc_info.descriptorSetCount = layouts.size();
- alloc_info.pSetLayouts = layouts.data();
-
- self->descriptor_sets.resize(layouts.size());
- if(vkAllocateDescriptorSets(
- self->queue_family->device->device, &alloc_info,
- self->descriptor_sets.data()) != VK_SUCCESS)
- CommandError{"Failed to create Vulkan descriptor set."};
-}
-
-void
-load_data_to_descriptor_sets(void *obj)
-{
- auto self = static_cast<VK::Rectangle*>(obj);
-
- for(auto i{0}; i < self->ub_rectangle.size(); i++)
- {
- VkDescriptorBufferInfo buffer_info{};
- buffer_info.buffer = self->ub_rectangle[i].buffer;
- buffer_info.offset = 0;
- buffer_info.range = sizeof(VK::UBOModel2D);
-
- std::array<VkWriteDescriptorSet, 1> write_descriptors{};
- write_descriptors[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
- write_descriptors[0].dstSet = self->descriptor_sets[i];
- write_descriptors[0].dstBinding = 0;
- write_descriptors[0].dstArrayElement = 0;
- write_descriptors[0].descriptorCount = 1;
- write_descriptors[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
- write_descriptors[0].pBufferInfo = &buffer_info;
- write_descriptors[0].pImageInfo = nullptr;
- write_descriptors[0].pTexelBufferView = nullptr;
-
- vkUpdateDescriptorSets(
- cg_core.vk_device_with_swapchain->device, write_descriptors.size(),
- write_descriptors.data(), 0, nullptr);
- }
-}
-
-static const CommandChain loader{
- {&load_mesh, &unload_mesh},
- {&load_uniform_buffer, &unload_uniform_buffer},
- {&load_descriptor_set_pool, &unload_descriptor_set_pool},
- {&load_descriptor_sets, nullptr},
- {&load_data_to_descriptor_sets, nullptr},
-};
-
-constexpr bool
-_align_vertically(const float a_x, const float a_width,
- const float b_x, const float b_width)
-{
- return a_x <= b_x + b_width && a_x + a_width >= b_x;
-}
-
-constexpr bool
-_align_horizontally(const float a_y, const float a_height,
- const float b_y, const float b_height)
-{
- return a_y <= b_y + b_height && a_y + a_height >= b_y;
-}
-
-}
-
namespace VK
{
-Rectangle::Rectangle(float x, float y, float width, float height):
- x{x},
- y{y},
- width{width},
- height{height}
-{
- loader.execute(this);
-}
+const int Rectangle::VertexCount{4};
-Rectangle::~Rectangle()
+Rectangle::Rectangle(glm::vec4 position, glm::vec3 color):
+ position{position},
+ color{color}
{
- loader.revert(this);
-}
-
-bool
-Rectangle::align_vertically(const Rectangle &that) const
-{
- return _align_vertically(this->x, this->width, that.x, that.width);
-}
-
-bool
-Rectangle::align_horizontally(const Rectangle &that) const
-{
- return _align_horizontally(this->y, this->height, that.y, that.height);
-}
-
-bool
-Rectangle::collide(const Rectangle &that) const
-{
- return
- _align_vertically(this->x, this->width, that.x, that.width) &&
- _align_horizontally(this->y, this->height, that.y, that.height);
-}
-
-void
-Rectangle::update_vertex_buffer()
-{
- glm::vec2 rect[4]{
- glm::vec2{this->x, this->y},
- glm::vec2{this->x, this->y + this->height},
- glm::vec2{this->x + this->width, this->y},
- glm::vec2{this->x + this->width, this->y + this->height}
- };
-
- void *vertexes_data{&rect};
- this->source_buffers[cg_core.vk_swapchain->current_frame].copy_data(
- vertexes_data);
- this->vertex_buffers[cg_core.vk_swapchain->current_frame].copy_data();
}
}
diff --git a/src/vk/rectangle.hpp b/src/vk/rectangle.hpp
index fc21135..faee927 100644
--- a/src/vk/rectangle.hpp
+++ b/src/vk/rectangle.hpp
@@ -29,36 +29,12 @@ namespace VK
struct Rectangle
{
- static const uint32_t vertex_count{4};
+ static const int VertexCount;
- float x{0};
- float y{0};
- float width{1};
- float height{1};
+ glm::vec4 position;
+ glm::vec3 color;
- QueueFamily *queue_family;
-
- std::vector<SourceBuffer> source_buffers;
- std::vector<DestinationBuffer> vertex_buffers;
-
- std::vector<UniformBuffer> ub_rectangle;
-
- VkDescriptorPool descriptor_pool;
- std::vector<VkDescriptorSet> descriptor_sets;
-
- Rectangle() = default;
- Rectangle(float x, float y, float width, float height);
- ~Rectangle();
-
- bool
- align_vertically(const Rectangle &that) const;
- bool
- align_horizontally(const Rectangle &that) const;
- bool
- collide(const Rectangle &that) const;
-
- void
- update_vertex_buffer();
+ Rectangle(glm::vec4 position, glm::vec3 color);
};
}
diff --git a/src/vk/sprite.cpp b/src/vk/sprite.cpp
index a7b802a..c2761f3 100644
--- a/src/vk/sprite.cpp
+++ b/src/vk/sprite.cpp
@@ -20,6 +20,7 @@
#include "../core.hpp"
#include "sprite.hpp"
+#include "uniform_buffer_object.hpp"
namespace
{
@@ -81,7 +82,7 @@ load_uniform_buffers(void *obj)
cg_core.vk_swapchain->images_count);
for(auto i{0}; i < cg_core.vk_swapchain->images_count; i++)
self->sprite->ub_sprite_positions.emplace_back(
- cg_core.vk_device_with_swapchain, sizeof(VK::UBOSpritePositions));
+ cg_core.vk_device_with_swapchain, sizeof(VK::UBOVectors4D));
}
void
@@ -162,7 +163,7 @@ load_buffers_to_descriptor_sets(void *obj)
VkDescriptorBufferInfo buffer_info{};
buffer_info.buffer = self->sprite->ub_sprite_positions[i].buffer;
buffer_info.offset = 0;
- buffer_info.range = sizeof(VK::UBOSpritePositions);
+ buffer_info.range = sizeof(VK::UBOVectors4D);
VkDescriptorImageInfo image_info{};
image_info.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
diff --git a/src/rectangle.hpp b/src/vk/uniform_buffer_object.hpp
index 8ed1748..fa598ab 100644
--- a/src/rectangle.hpp
+++ b/src/vk/uniform_buffer_object.hpp
@@ -14,15 +14,51 @@
* limitations under the License.
*/
-#ifndef CANDY_GEAR_RECTANGLE_H
-#define CANDY_GEAR_RECTANGLE_H 1
+#ifndef CANDY_GEAR_VK_UNIFORM_BUFFER_OBJECT_H
+#define CANDY_GEAR_VK_UNIFORM_BUFFER_OBJECT_H 1
#include "core.hpp"
-#include "vk/rectangle.hpp"
-extern const struct mrb_data_type cg_rectangle_type;
+namespace VK
+{
-void
-cg_rectangle_init(mrb_state *mrb);
+struct UBOView2D
+{
+ glm::mat4 proj;
+};
-#endif /* CANDY_GEAR_RECTANGLE_H */
+struct UBOView3D
+{
+ glm::mat4 view;
+ glm::mat4 proj;
+};
+
+struct UBOWorld3D_Vert
+{
+ glm::vec4 ambient_light_color;
+};
+
+struct UBOWorld3D_Frag
+{
+ glm::vec3 directional_light_direction;
+ glm::vec4 directional_light_color;
+};
+
+struct UBOModelInstance
+{
+ glm::mat4 instances[128];
+};
+
+struct UBOVectors4D
+{
+ glm::vec4 vectors[128];
+};
+
+struct UBOVectors3D
+{
+ glm::vec3 vectors[128];
+};
+
+}
+
+#endif /* CANDY_GEAR_VK_UNIFORM_BUFFER_OBJECT_H */
diff --git a/src/vk/view_2d.cpp b/src/vk/view_2d.cpp
index 011fb1a..a80cc89 100644
--- a/src/vk/view_2d.cpp
+++ b/src/vk/view_2d.cpp
@@ -19,6 +19,7 @@
#include <array>
#include "../core.hpp"
+#include "uniform_buffer_object.hpp"
namespace
{
diff --git a/src/vk/view_2d.hpp b/src/vk/view_2d.hpp
index ec7225f..f49c3be 100644
--- a/src/vk/view_2d.hpp
+++ b/src/vk/view_2d.hpp
@@ -38,7 +38,7 @@ struct View2D
VkDescriptorPool descriptor_pool;
std::vector<VkDescriptorSet> descriptor_sets_2d;
- std::vector<std::vector<std::shared_ptr<Rectangle>>> rectangles_to_draw;
+ std::vector<std::vector<Rectangle>> rectangles_to_draw;
std::vector<
std::unordered_map<std::shared_ptr<Sprite>, std::vector<glm::vec4>>>
sprites_to_draw;
diff --git a/src/vk/view_3d.cpp b/src/vk/view_3d.cpp
index 9d124d9..39d1159 100644
--- a/src/vk/view_3d.cpp
+++ b/src/vk/view_3d.cpp
@@ -19,6 +19,7 @@
#include <array>
#include "../core.hpp"
+#include "uniform_buffer_object.hpp"
namespace
{
diff --git a/test/src/main.rb b/test/src/main.rb
index b7bbe85..84a47bd 100644
--- a/test/src/main.rb
+++ b/test/src/main.rb
@@ -30,8 +30,8 @@ def init()
$model = CandyGear::Model.new(mesh, texture);
$sprite = CandyGear::Sprite.new(
texture, CandyGear::Vector4D.new(0, 0, 1.0, 1.0));
- $rectangle = CandyGear::Rectangle.new(102.0, 1.0, 101.0, 101.0)
- $sprite_position = CandyGear::Vector4D.new(1.0, 1.0, 101.0, 101.0);
+ $rectangle = CandyGear::Vector4D.new(103.0, 1.0, 100.0, 100.0);
+ $sprite_position = CandyGear::Vector4D.new(1.0, 1.0, 100.0, 100.0);
$instances = [
CandyGear::Vector3D.new(5.0, 0.0, 0.0),
@@ -88,7 +88,7 @@ def quit() = CandyGear.quit();
def tick()
$sprite.draw($view1, $sprite_position);
$instances_rotation.rotate(0.0, BOX_ROTATION_SPEED);
- $rectangle.draw($view1, $color);
+ $rectangle.draw_rectangle($view1, $color);
$instances.each do |i|
$model.draw(i, $instances_rotation);
end