WIP: lunasvg implementation, things stopped working
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "renderer/renderer.h"
|
||||
#include "ui/ui_core.h"
|
||||
#include "ui/ui_icons.h"
|
||||
|
||||
#import <Metal/Metal.h>
|
||||
#import <QuartzCore/CAMetalLayer.h>
|
||||
@@ -162,6 +163,9 @@ struct Renderer {
|
||||
F32 font_atlas_size;
|
||||
F32 font_line_height;
|
||||
|
||||
// Icon atlas
|
||||
id<MTLTexture> icon_texture;
|
||||
|
||||
// Text measurement (Core Text)
|
||||
CTFontRef measure_font;
|
||||
F32 measure_font_size;
|
||||
@@ -694,6 +698,9 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) {
|
||||
batch.vertex_count = 0;
|
||||
batch.index_count = 0;
|
||||
|
||||
// Track which texture is currently bound (0 = font, 1 = icon)
|
||||
int bound_texture = 0;
|
||||
|
||||
auto flush_batch = [&]() {
|
||||
if (batch.index_count == 0) return;
|
||||
|
||||
@@ -708,6 +715,22 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) {
|
||||
batch.index_count = 0;
|
||||
};
|
||||
|
||||
auto bind_font_texture = [&]() {
|
||||
if (bound_texture != 0) {
|
||||
flush_batch();
|
||||
[encoder setFragmentTexture:r->font_texture atIndex:0];
|
||||
bound_texture = 0;
|
||||
}
|
||||
};
|
||||
|
||||
auto bind_icon_texture = [&]() {
|
||||
if (bound_texture != 1 && r->icon_texture) {
|
||||
flush_batch();
|
||||
[encoder setFragmentTexture:r->icon_texture atIndex:0];
|
||||
bound_texture = 1;
|
||||
}
|
||||
};
|
||||
|
||||
for (int32_t i = 0; i < render_commands.length; i++) {
|
||||
Clay_RenderCommand *cmd = Clay_RenderCommandArray_Get(&render_commands, i);
|
||||
Clay_BoundingBox bb = cmd->boundingBox;
|
||||
@@ -751,6 +774,7 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) {
|
||||
} break;
|
||||
|
||||
case CLAY_RENDER_COMMAND_TYPE_TEXT: {
|
||||
bind_font_texture();
|
||||
Clay_TextRenderData *text = &cmd->renderData.text;
|
||||
emit_text_glyphs(&batch, r, bb, text->textColor,
|
||||
text->stringContents.chars, text->stringContents.length,
|
||||
@@ -779,6 +803,7 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) {
|
||||
if (custom->customData) {
|
||||
CustomRenderType type = *(CustomRenderType *)custom->customData;
|
||||
if (type == CUSTOM_RENDER_VGRADIENT) {
|
||||
bind_font_texture();
|
||||
CustomGradientData *grad = (CustomGradientData *)custom->customData;
|
||||
Clay_Color tc = grad->top_color;
|
||||
Clay_Color bc = grad->bottom_color;
|
||||
@@ -789,6 +814,20 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) {
|
||||
custom->cornerRadius.topLeft, custom->cornerRadius.topRight,
|
||||
custom->cornerRadius.bottomRight, custom->cornerRadius.bottomLeft,
|
||||
1.0f);
|
||||
} else if (type == CUSTOM_RENDER_ICON) {
|
||||
bind_icon_texture();
|
||||
CustomIconData *icon = (CustomIconData *)custom->customData;
|
||||
Clay_Color c = icon->color;
|
||||
float cr = c.r / 255.f, cg = c.g / 255.f;
|
||||
float cb = c.b / 255.f, ca = c.a / 255.f;
|
||||
UI_IconInfo *info = &g_icons[icon->icon_id];
|
||||
emit_quad(&batch,
|
||||
bb.x, bb.y, bb.x + bb.width, bb.y + bb.height,
|
||||
info->u0, info->v0, info->u1, info->v1,
|
||||
cr, cg, cb, ca,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 0, 0,
|
||||
0, 0, 1.0f);
|
||||
}
|
||||
}
|
||||
} break;
|
||||
@@ -816,6 +855,20 @@ void renderer_end_frame(Renderer *r, Clay_RenderCommandArray render_commands) {
|
||||
r->frame_index++;
|
||||
}
|
||||
|
||||
void renderer_create_icon_atlas(Renderer *r, const uint8_t *data, int32_t w, int32_t h) {
|
||||
MTLTextureDescriptor *tex_desc = [[MTLTextureDescriptor alloc] init];
|
||||
tex_desc.pixelFormat = MTLPixelFormatR8Unorm;
|
||||
tex_desc.width = w;
|
||||
tex_desc.height = h;
|
||||
tex_desc.usage = MTLTextureUsageShaderRead;
|
||||
|
||||
r->icon_texture = [r->device newTextureWithDescriptor:tex_desc];
|
||||
[r->icon_texture replaceRegion:MTLRegionMake2D(0, 0, w, h)
|
||||
mipmapLevel:0
|
||||
withBytes:data
|
||||
bytesPerRow:w];
|
||||
}
|
||||
|
||||
void renderer_set_clear_color(Renderer *r, float cr, float cg, float cb) {
|
||||
r->clear_r = cr;
|
||||
r->clear_g = cg;
|
||||
|
||||
Reference in New Issue
Block a user