working on text render pipeline

This commit is contained in:
2023-04-08 04:09:25 +02:00
parent 77e8672754
commit 157a099ed2
11 changed files with 8202 additions and 29 deletions

View File

@@ -68,7 +68,7 @@ namespace mlx::core
void Application::stringPut(void* win, int x, int y, int color, char* str) void Application::stringPut(void* win, int x, int y, int color, char* str)
{ {
_graphics[*static_cast<int*>(win)]->stringPut(x, y, color, str);
} }
void Application::texturePut(void* win, void* img, int x, int y) void Application::texturePut(void* win, void* img, int x, int y)

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */ /* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
/* Updated: 2023/04/06 16:39:13 by maldavid ### ########.fr */ /* Updated: 2023/04/08 00:19:18 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -16,13 +16,13 @@ namespace mlx
{ {
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) : GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) :
_window(std::make_shared<MLX_Window>(w, h, std::move(title))), _window(std::make_shared<MLX_Window>(w, h, std::move(title))),
_renderer(std::make_unique<Renderer>()), _renderer(std::make_unique<Renderer>()), _text_put_pipeline(std::make_unique<TextPutPipeline>()),
_id(id) _id(id)
{ {
_renderer->setWindow(_window.get()); _renderer->setWindow(_window.get());
_renderer->init(); _renderer->init();
_pixel_put_pipeline.init(w, h, *_renderer); _pixel_put_pipeline.init(w, h, *_renderer);
_text_put_pipeline.init(*_renderer); _text_put_pipeline->init(_renderer.get());
} }
void GraphicsSupport::endRender() noexcept void GraphicsSupport::endRender() noexcept
@@ -49,6 +49,11 @@ namespace mlx
sets.push_back(_pixel_put_pipeline.getDescriptorSet()); sets.push_back(_pixel_put_pipeline.getDescriptorSet());
vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
_pixel_put_pipeline.render(*_renderer); _pixel_put_pipeline.render(*_renderer);
sets.pop_back();
sets.push_back(_text_put_pipeline->getDescriptorSet());
vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
_text_put_pipeline->render();
_renderer->endFrame(); _renderer->endFrame();
@@ -60,6 +65,7 @@ namespace mlx
{ {
vkDeviceWaitIdle(Render_Core::get().getDevice().get()); vkDeviceWaitIdle(Render_Core::get().getDevice().get());
_pixel_put_pipeline.destroy(); _pixel_put_pipeline.destroy();
_text_put_pipeline->destroy();
_renderer->destroy(); _renderer->destroy();
} }
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ /* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */
/* Updated: 2023/04/06 19:01:12 by maldavid ### ########.fr */ /* Updated: 2023/04/08 01:14:22 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -50,9 +50,9 @@ namespace mlx
private: private:
std::unordered_set<TextureRenderData> _textures_to_render; std::unordered_set<TextureRenderData> _textures_to_render;
PixelPutPipeline _pixel_put_pipeline; PixelPutPipeline _pixel_put_pipeline;
TextPutPipeline _text_put_pipeline;
glm::mat4 _proj = glm::mat4(1.0); glm::mat4 _proj = glm::mat4(1.0);
std::shared_ptr<MLX_Window> _window; std::shared_ptr<MLX_Window> _window;
std::unique_ptr<TextPutPipeline> _text_put_pipeline; // unique_ptr because of the size of the class
std::unique_ptr<Renderer> _renderer; std::unique_ptr<Renderer> _renderer;
int _id; int _id;
}; };

View File

@@ -29,6 +29,7 @@ namespace mlx
{ {
_textures_to_render.clear(); _textures_to_render.clear();
_pixel_put_pipeline.clear(); _pixel_put_pipeline.clear();
_text_put_pipeline.clear();
} }
void GraphicsSupport::pixelPut(int x, int y, int color) noexcept void GraphicsSupport::pixelPut(int x, int y, int color) noexcept
@@ -38,7 +39,7 @@ namespace mlx
void GraphicsSupport::stringPut(int x, int y, int color, std::string str) void GraphicsSupport::stringPut(int x, int y, int color, std::string str)
{ {
_text_put_pipeline.put(x, y, color, str); _text_put_pipeline->put(x, y, color, str);
} }
void GraphicsSupport::texturePut(std::shared_ptr<Texture> texture, int x, int y) void GraphicsSupport::texturePut(std::shared_ptr<Texture> texture, int x, int y)

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */ /* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */
/* Updated: 2023/04/07 17:07:46 by maldavid ### ########.fr */ /* Updated: 2023/04/08 00:29:39 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -14,7 +14,7 @@
namespace mlx namespace mlx
{ {
void TextureAtlas::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, uint32_t render_width, uint32_t rendre_height) void TextureAtlas::create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format)
{ {
Image::create(width, height, format, Image::create(width, height, format,
VK_IMAGE_TILING_OPTIMAL, VK_IMAGE_TILING_OPTIMAL,
@@ -25,16 +25,23 @@ namespace mlx
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT); Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
Image::createSampler(); Image::createSampler();
_color = 0xFFFFFFFF;
float red = (_color & 0x00FF0000) / 255;
float green = (_color & 0x0000FF00) / 255;
float blue = (_color & 0x000000FF) / 255;
std::vector<Vertex> vertexData = { std::vector<Vertex> vertexData = {
{{0, 0}, {1.f, 1.f, 1.f, 1.f}, {0.0f, 0.0f}}, {{0, 0}, {red, green, blue, 1.f}, {0.0f, 0.0f}},
{{render_width, 0}, {1.f, 1.f, 1.f, 1.f}, {1.0f, 0.0f}}, {{20, 0}, {red, green, blue, 1.f}, {1.0f, 0.0f}},
{{render_width, render_height}, {1.f, 1.f, 1.f, 1.f}, {1.0f, 1.0f}}, {{20, 20}, {red, green, blue, 1.f}, {1.0f, 1.0f}},
{{0, render_height}, {1.f, 1.f, 1.f, 1.f}, {0.0f, 1.0f}} {{0, 20}, {red, green, blue, 1.f}, {0.0f, 1.0f}}
}; };
std::vector<uint16_t> indexData = { 0, 1, 2, 2, 3, 0 }; std::vector<uint16_t> indexData = { 0, 1, 2, 2, 3, 0 };
_vbo.create(sizeof(Vertex) * vertexData.size(), vertexData.data()); _vbo.create(sizeof(Vertex) * vertexData.size());
_vbo.setData(sizeof(Vertex) * vertexData.size(), vertexData.data());
_ibo.create(sizeof(uint16_t) * indexData.size(), indexData.data()); _ibo.create(sizeof(uint16_t) * indexData.size(), indexData.data());
if(pixels != nullptr) if(pixels != nullptr)
@@ -47,15 +54,19 @@ namespace mlx
} }
} }
void TextureAtlas::render(Renderer& renderer, int x, int y, std::array<glm::vec2, 4> uv) void TextureAtlas::render(Renderer& renderer, int x, int y, std::array<glm::vec2, 4> pos, std::array<glm::vec2, 4> uv)
{ {
auto cmd = renderer.getActiveCmdBuffer().get(); auto cmd = renderer.getActiveCmdBuffer().get();
float red = (_color & 0x00FF0000) / 255;
float green = (_color & 0x0000FF00) / 255;
float blue = (_color & 0x000000FF) / 255;
std::vector<Vertex> vertexData = { std::vector<Vertex> vertexData = {
{{0, 0}, {1.f, 1.f, 1.f, 1.f}, uv[0]}, {pos[0], {red, green, blue, 1.f}, uv[0]},
{{render_width, 0}, {1.f, 1.f, 1.f, 1.f}, uv[1]}, {pos[1], {red, green, blue, 1.f}, uv[1]},
{{render_width, render_height}, {1.f, 1.f, 1.f, 1.f}, uv[2]}, {pos[2], {red, green, blue, 1.f}, uv[2]},
{{0, render_height}, {1.f, 1.f, 1.f, 1.f}, uv[3]} {pos[3], {red, green, blue, 1.f}, uv[3]}
}; };
_vbo.setData(sizeof(Vertex) * vertexData.size(), vertexData.data()); _vbo.setData(sizeof(Vertex) * vertexData.size(), vertexData.data());

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */ /* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
/* Updated: 2023/04/07 16:44:43 by maldavid ### ########.fr */ /* Updated: 2023/04/08 00:27:46 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -24,10 +24,12 @@ namespace mlx
public: public:
TextureAtlas() = default; TextureAtlas() = default;
void create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, uint32_t render_width, uint32_t rendre_height); void create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format);
void render(class Renderer& renderer, int x, int y, std::array<glm::vec2, 4> uv); void render(class Renderer& renderer, int x, int y, std::array<glm::vec2, 4> pos, std::array<glm::vec2, 4> uv);
void destroy() noexcept override; void destroy() noexcept override;
inline void setColor(int color) noexcept { _color = color; }
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); } inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; } inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); } inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); }
@@ -38,8 +40,7 @@ namespace mlx
VBO _vbo; VBO _vbo;
C_IBO _ibo; C_IBO _ibo;
DescriptorSet _set; DescriptorSet _set;
uint32_t _render_width; int _color;
uint32_t _render_height;
}; };
} }

View File

@@ -6,29 +6,66 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */ /* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
/* Updated: 2023/04/07 17:24:33 by maldavid ### ########.fr */ /* Updated: 2023/04/08 00:48:48 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <renderer/text_pipeline.h> #include <renderer/text_pipeline.h>
#include <fstream> #include <fstream>
#include <utils/opensans-regular.h>
#define STB_TRUETYPE_IMPLEMENTATION #define STB_TRUETYPE_IMPLEMENTATION
#include <stb_truetype.h> #include <stb_truetype.h>
namespace mlx namespace mlx
{ {
static uint8_t ttf_buffer[1 << 20];
static uint8_t tmp_bitmap[512 * 512]; static uint8_t tmp_bitmap[512 * 512];
void TextPutPipeline::init(Renderer* renderer) noexcept void TextPutPipeline::init(Renderer* renderer) noexcept
{ {
_renderer = renderer; _renderer = renderer;
stbtt_BakeFontBitmap(opensans_regular_ttf, 0, 32.0, tmp_bitmap, 512, 512, 32, 96, _cdata);
_atlas.create(reinterpret_cast<uint8_t*>(_cdata), 512, 512, VK_FORMAT_R8G8B8A8_UNORM);
_atlas.setDescriptor(renderer->getFragDescriptorSet().duplicate());
} }
void TextPutPipeline::put(int x, int y, int color, std::string str) void TextPutPipeline::put(int x, int y, int color, std::string str)
{ {
_drawlist.emplace_back(std::move(str), color, x, y);
}
void TextPutPipeline::render()
{
_atlas.updateSet(0);
for(auto& draw : _drawlist)
{
_atlas.setColor(draw.color);
for(char c : draw.text)
{
if(c < 32 && c > 127)
continue;
stbtt_aligned_quad q;
stbtt_GetBakedQuad(_cdata, 512, 512, c - 32, &draw.x, &draw.y, &q, 1);
std::array<glm::vec2, 4> pos = {
glm::vec2{q.x0, q.y0},
glm::vec2{q.x1, q.y0},
glm::vec2{q.x1, q.y1},
glm::vec2{q.x0, q.y1}
};
std::array<glm::vec2, 4> uv = {
glm::vec2{q.s0, q.t0},
glm::vec2{q.s1, q.t0},
glm::vec2{q.s1, q.t1},
glm::vec2{q.s0, q.t1}
};
_atlas.render(*_renderer, 0, 0, std::move(pos), std::move(uv));
}
}
} }
void TextPutPipeline::destroy() noexcept void TextPutPipeline::destroy() noexcept

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */ /* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
/* Updated: 2023/04/07 17:11:08 by maldavid ### ########.fr */ /* Updated: 2023/04/08 00:46:36 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -18,6 +18,7 @@
#include <string> #include <string>
#include <stb_truetype.h> #include <stb_truetype.h>
#include <cstdint> #include <cstdint>
#include <vector>
namespace mlx namespace mlx
{ {
@@ -28,13 +29,29 @@ namespace mlx
void init(Renderer* renderer) noexcept; void init(Renderer* renderer) noexcept;
void put(int x, int y, int color, std::string str); void put(int x, int y, int color, std::string str);
inline VkDescriptorSet getDescriptorSet() noexcept { return _atlas.getSet(); }
inline void clear() { _drawlist.clear(); }
void render();
void destroy() noexcept; void destroy() noexcept;
~TextPutPipeline() = default; ~TextPutPipeline() = default;
private: private:
struct DrawList
{
std::string text;
float x;
float y;
int color;
DrawList(std::string _text, int _color, int _x, int _y) :
text(std::move(_text)), color(_color), x(_x), y(_y)
{}
};
stbtt_bakedchar _cdata[96]; stbtt_bakedchar _cdata[96];
TextureAtlas _atlas; TextureAtlas _atlas;
std::vector<DrawList> _drawlist;
Renderer* _renderer = nullptr; Renderer* _renderer = nullptr;
}; };
} }

8099
src/utils/opensans-regular.h git.filemode.normal_file

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2023/04/06 15:26:57 by maldavid ### ########.fr */ /* Updated: 2023/04/08 00:21:38 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -78,6 +78,7 @@ int main(void)
mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 0xFFFF00FF); mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 0xFFFF00FF);
mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200); mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200);
img = create_image(&mlx); img = create_image(&mlx);
mlx_string_put(mlx.mlx, mlx.win, 0, 0, 0xFFFF0000, "this is a text");
mlx_put_image_to_window(mlx.mlx, mlx.win, img, 200, 20); mlx_put_image_to_window(mlx.mlx, mlx.win, img, 200, 20);
mlx_loop_hook(mlx.mlx, update, &mlx); mlx_loop_hook(mlx.mlx, update, &mlx);
mlx_loop(mlx.mlx); mlx_loop(mlx.mlx);