mirror of
https://github.com/seekrs/MacroLibX.git
synced 2026-01-11 06:33:35 +00:00
working on text render pipeline
This commit is contained in:
@@ -68,7 +68,7 @@ namespace mlx::core
|
||||
|
||||
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)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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) :
|
||||
_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)
|
||||
{
|
||||
_renderer->setWindow(_window.get());
|
||||
_renderer->init();
|
||||
_pixel_put_pipeline.init(w, h, *_renderer);
|
||||
_text_put_pipeline.init(*_renderer);
|
||||
_text_put_pipeline->init(_renderer.get());
|
||||
}
|
||||
|
||||
void GraphicsSupport::endRender() noexcept
|
||||
@@ -49,6 +49,11 @@ namespace mlx
|
||||
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);
|
||||
_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();
|
||||
|
||||
@@ -60,6 +65,7 @@ namespace mlx
|
||||
{
|
||||
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
|
||||
_pixel_put_pipeline.destroy();
|
||||
_text_put_pipeline->destroy();
|
||||
_renderer->destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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:
|
||||
std::unordered_set<TextureRenderData> _textures_to_render;
|
||||
PixelPutPipeline _pixel_put_pipeline;
|
||||
TextPutPipeline _text_put_pipeline;
|
||||
glm::mat4 _proj = glm::mat4(1.0);
|
||||
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;
|
||||
int _id;
|
||||
};
|
||||
|
||||
@@ -29,6 +29,7 @@ namespace mlx
|
||||
{
|
||||
_textures_to_render.clear();
|
||||
_pixel_put_pipeline.clear();
|
||||
_text_put_pipeline.clear();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
_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)
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
{
|
||||
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,
|
||||
VK_IMAGE_TILING_OPTIMAL,
|
||||
@@ -25,16 +25,23 @@ namespace mlx
|
||||
Image::createImageView(VK_IMAGE_VIEW_TYPE_2D, VK_IMAGE_ASPECT_COLOR_BIT);
|
||||
Image::createSampler();
|
||||
|
||||
_color = 0xFFFFFFFF;
|
||||
|
||||
float red = (_color & 0x00FF0000) / 255;
|
||||
float green = (_color & 0x0000FF00) / 255;
|
||||
float blue = (_color & 0x000000FF) / 255;
|
||||
|
||||
std::vector<Vertex> vertexData = {
|
||||
{{0, 0}, {1.f, 1.f, 1.f, 1.f}, {0.0f, 0.0f}},
|
||||
{{render_width, 0}, {1.f, 1.f, 1.f, 1.f}, {1.0f, 0.0f}},
|
||||
{{render_width, render_height}, {1.f, 1.f, 1.f, 1.f}, {1.0f, 1.0f}},
|
||||
{{0, render_height}, {1.f, 1.f, 1.f, 1.f}, {0.0f, 1.0f}}
|
||||
{{0, 0}, {red, green, blue, 1.f}, {0.0f, 0.0f}},
|
||||
{{20, 0}, {red, green, blue, 1.f}, {1.0f, 0.0f}},
|
||||
{{20, 20}, {red, green, blue, 1.f}, {1.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 };
|
||||
|
||||
_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());
|
||||
|
||||
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();
|
||||
|
||||
float red = (_color & 0x00FF0000) / 255;
|
||||
float green = (_color & 0x0000FF00) / 255;
|
||||
float blue = (_color & 0x000000FF) / 255;
|
||||
|
||||
std::vector<Vertex> vertexData = {
|
||||
{{0, 0}, {1.f, 1.f, 1.f, 1.f}, uv[0]},
|
||||
{{render_width, 0}, {1.f, 1.f, 1.f, 1.f}, uv[1]},
|
||||
{{render_width, render_height}, {1.f, 1.f, 1.f, 1.f}, uv[2]},
|
||||
{{0, render_height}, {1.f, 1.f, 1.f, 1.f}, uv[3]}
|
||||
{pos[0], {red, green, blue, 1.f}, uv[0]},
|
||||
{pos[1], {red, green, blue, 1.f}, uv[1]},
|
||||
{pos[2], {red, green, blue, 1.f}, uv[2]},
|
||||
{pos[3], {red, green, blue, 1.f}, uv[3]}
|
||||
};
|
||||
_vbo.setData(sizeof(Vertex) * vertexData.size(), vertexData.data());
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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:
|
||||
TextureAtlas() = default;
|
||||
|
||||
void create(uint8_t* pixels, uint32_t width, uint32_t height, VkFormat format, uint32_t render_width, uint32_t rendre_height);
|
||||
void render(class Renderer& renderer, int x, int y, std::array<glm::vec2, 4> uv);
|
||||
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> pos, std::array<glm::vec2, 4> uv);
|
||||
void destroy() noexcept override;
|
||||
|
||||
inline void setColor(int color) noexcept { _color = color; }
|
||||
|
||||
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
|
||||
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
|
||||
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); }
|
||||
@@ -38,8 +40,7 @@ namespace mlx
|
||||
VBO _vbo;
|
||||
C_IBO _ibo;
|
||||
DescriptorSet _set;
|
||||
uint32_t _render_width;
|
||||
uint32_t _render_height;
|
||||
int _color;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,29 +6,66 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <fstream>
|
||||
|
||||
#include <utils/opensans-regular.h>
|
||||
|
||||
#define STB_TRUETYPE_IMPLEMENTATION
|
||||
#include <stb_truetype.h>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
static uint8_t ttf_buffer[1 << 20];
|
||||
static uint8_t tmp_bitmap[512 * 512];
|
||||
|
||||
void TextPutPipeline::init(Renderer* renderer) noexcept
|
||||
{
|
||||
_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)
|
||||
{
|
||||
|
||||
_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
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <stb_truetype.h>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
namespace mlx
|
||||
{
|
||||
@@ -28,13 +29,29 @@ namespace mlx
|
||||
|
||||
void init(Renderer* renderer) noexcept;
|
||||
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;
|
||||
|
||||
~TextPutPipeline() = default;
|
||||
|
||||
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];
|
||||
TextureAtlas _atlas;
|
||||
std::vector<DrawList> _drawlist;
|
||||
Renderer* _renderer = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
8099
src/utils/opensans-regular.h
git.filemode.normal_file
8099
src/utils/opensans-regular.h
git.filemode.normal_file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -6,7 +6,7 @@
|
||||
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200);
|
||||
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_loop_hook(mlx.mlx, update, &mlx);
|
||||
mlx_loop(mlx.mlx);
|
||||
|
||||
Reference in New Issue
Block a user