render fix
This commit is contained in:
@@ -34,7 +34,6 @@ typedef struct rn_state_t rn_state_t;
|
||||
struct rn_state_t {
|
||||
rn_font_t main_font;
|
||||
rn_shader_t shader2d;
|
||||
v2f32_t window_size;
|
||||
|
||||
rn_cmd_t *first_cmd;
|
||||
rn_cmd_t *last_cmd;
|
||||
@@ -81,102 +80,6 @@ void gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, G
|
||||
}
|
||||
}
|
||||
|
||||
void rn_begin(v2f32_t window_size) {
|
||||
rn_state.window_size = window_size;
|
||||
}
|
||||
|
||||
void rn_init(ma_arena_t *perm) {
|
||||
rn_state.cap = 1024*64;
|
||||
rn_state.vertices = ma_push_array(perm, rn_vertex_t, rn_state.cap);
|
||||
|
||||
{
|
||||
ma_temp_t scratch = ma_begin_scratch1(perm);
|
||||
s8_t font_data = rn_get_default_font(tcx.temp);
|
||||
rn_atlas_t *atlas = rn_create_atlas(scratch.arena, (v2i32_t){2048, 2048});
|
||||
|
||||
u32 font_size = 100;
|
||||
rn_state.main_font = rn_create_font(perm, font_data, atlas, font_size);
|
||||
|
||||
GLint filter = GL_NEAREST;
|
||||
// if (StyleFontFilter == 1) filter = GL_LINEAR;
|
||||
glCreateTextures(GL_TEXTURE_2D, 1, &atlas->texture_id);
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_MIN_FILTER, filter);
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_MAG_FILTER, filter);
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTextureStorage2D(atlas->texture_id, 1, GL_R8, (GLsizei)atlas->size.x, (GLsizei)atlas->size.y);
|
||||
glTextureSubImage2D(atlas->texture_id, 0, 0, 0, (GLsizei)atlas->size.x, (GLsizei)atlas->size.y, GL_RED, GL_UNSIGNED_BYTE, atlas->bitmap);
|
||||
rn_state.main_font.texture_id = atlas->texture_id;
|
||||
ma_end_scratch(scratch);
|
||||
}
|
||||
|
||||
|
||||
glDebugMessageCallback(&gl_debug_callback, NULL);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
|
||||
glCreateBuffers(1, &rn_state.vbo);
|
||||
glNamedBufferStorage(rn_state.vbo, rn_state.cap * sizeof(rn_vertex_t), 0, GL_DYNAMIC_STORAGE_BIT);
|
||||
|
||||
glCreateVertexArrays(1, &rn_state.vao);
|
||||
|
||||
GLint vbuf_index = 0;
|
||||
glVertexArrayVertexBuffer(rn_state.vao, vbuf_index, rn_state.vbo, 0, sizeof(struct rn_vertex_t));
|
||||
|
||||
GLint a_pos = 0;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_pos, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, pos));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_pos, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_pos);
|
||||
|
||||
GLint a_tex = 1;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_tex, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, tex));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_tex, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_tex);
|
||||
|
||||
GLint a_color = 2;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_color, 4, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, color));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_color, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_color);
|
||||
|
||||
char *glsl_vshader =
|
||||
"#version 450 core\n"
|
||||
"layout(location=0) uniform vec2 U_InvHalfScreenSize;\n"
|
||||
"layout(location=0) in vec2 VPos;\n"
|
||||
"layout(location=1) in vec2 VTex;\n"
|
||||
"layout(location=2) in vec4 VColor;\n"
|
||||
"\n"
|
||||
"out gl_PerVertex { vec4 gl_Position; }; // required because of ARB_separate_shader_objects\n"
|
||||
"out vec2 FUV;\n"
|
||||
"out vec4 FColor;\n"
|
||||
"\n"
|
||||
"void main() {\n"
|
||||
" vec2 pos = VPos * U_InvHalfScreenSize;\n"
|
||||
" pos.y = 2 - pos.y; // invert y axis\n"
|
||||
" pos -= vec2(1, 1); // convert to 0,1 range\n"
|
||||
"\n"
|
||||
" gl_Position = vec4(pos, 0, 1);\n"
|
||||
" FUV = VTex;\n"
|
||||
"\n"
|
||||
" FColor = VColor;\n"
|
||||
"}\n";
|
||||
|
||||
char *glsl_fshader =
|
||||
"#version 450 core\n"
|
||||
"\n"
|
||||
"in vec2 FUV;\n"
|
||||
"in vec4 FColor;\n"
|
||||
"\n"
|
||||
"layout (binding=0) uniform sampler2D S_Texture;\n"
|
||||
"layout (location=0) out vec4 OutColor;\n"
|
||||
"\n"
|
||||
"void main() {\n"
|
||||
" vec4 c = FColor;\n"
|
||||
" c.a *= texture(S_Texture, FUV).r;\n"
|
||||
" OutColor = c;\n"
|
||||
"}\n";
|
||||
|
||||
rn_state.shader2d = rn_create_shader(glsl_vshader, glsl_fshader);
|
||||
}
|
||||
|
||||
rn_cmd_t *rn_get_cmd(rn_cmd_kind_t kind) {
|
||||
b32 alloc_new = false;
|
||||
if (rn_state.last_cmd == NULL) {
|
||||
@@ -274,6 +177,109 @@ v2f32_t rn_base_draw_string(rn_font_t *font, s8_t string, v2f32_t pos, v4f32_t c
|
||||
return result;
|
||||
}
|
||||
|
||||
v2f32_t rn_draw_string(rn_font_t *font, v2f32_t pos, v4f32_t color, s8_t string) {
|
||||
return rn_base_draw_string(font, string, pos, color, true);
|
||||
}
|
||||
|
||||
v2f32_t rn_measure_string(rn_font_t *font, s8_t string) {
|
||||
return rn_base_draw_string(font, string, v2f32(0,0), v4f32(0,0,0,0), false);
|
||||
}
|
||||
|
||||
void rn_init(ma_arena_t *perm) {
|
||||
rn_state.cap = 1024*64;
|
||||
rn_state.vertices = ma_push_array(perm, rn_vertex_t, rn_state.cap);
|
||||
|
||||
{
|
||||
ma_temp_t scratch = ma_begin_scratch1(perm);
|
||||
s8_t font_data = rn_get_default_font(tcx.temp);
|
||||
rn_atlas_t *atlas = rn_create_atlas(scratch.arena, (v2i32_t){2048, 2048});
|
||||
|
||||
u32 font_size = 100;
|
||||
rn_state.main_font = rn_create_font(perm, font_data, atlas, font_size);
|
||||
|
||||
GLint filter = GL_NEAREST;
|
||||
// if (StyleFontFilter == 1) filter = GL_LINEAR;
|
||||
glCreateTextures(GL_TEXTURE_2D, 1, &atlas->texture_id);
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_MIN_FILTER, filter);
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_MAG_FILTER, filter);
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTextureParameteri(atlas->texture_id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTextureStorage2D(atlas->texture_id, 1, GL_R8, (GLsizei)atlas->size.x, (GLsizei)atlas->size.y);
|
||||
glTextureSubImage2D(atlas->texture_id, 0, 0, 0, (GLsizei)atlas->size.x, (GLsizei)atlas->size.y, GL_RED, GL_UNSIGNED_BYTE, atlas->bitmap);
|
||||
rn_state.main_font.texture_id = atlas->texture_id;
|
||||
ma_end_scratch(scratch);
|
||||
}
|
||||
|
||||
|
||||
glDebugMessageCallback(&gl_debug_callback, NULL);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
|
||||
glCreateBuffers(1, &rn_state.vbo);
|
||||
glNamedBufferStorage(rn_state.vbo, rn_state.cap * sizeof(rn_vertex_t), 0, GL_DYNAMIC_STORAGE_BIT);
|
||||
|
||||
glCreateVertexArrays(1, &rn_state.vao);
|
||||
|
||||
GLint vbuf_index = 0;
|
||||
glVertexArrayVertexBuffer(rn_state.vao, vbuf_index, rn_state.vbo, 0, sizeof(struct rn_vertex_t));
|
||||
|
||||
GLint a_pos = 0;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_pos, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, pos));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_pos, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_pos);
|
||||
|
||||
GLint a_tex = 1;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_tex, 2, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, tex));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_tex, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_tex);
|
||||
|
||||
GLint a_color = 2;
|
||||
glVertexArrayAttribFormat(rn_state.vao, a_color, 4, GL_FLOAT, GL_FALSE, offsetof(struct rn_vertex_t, color));
|
||||
glVertexArrayAttribBinding(rn_state.vao, a_color, vbuf_index);
|
||||
glEnableVertexArrayAttrib(rn_state.vao, a_color);
|
||||
|
||||
char *glsl_vshader =
|
||||
"#version 450 core\n"
|
||||
"layout(location=0) uniform vec2 U_InvHalfScreenSize;\n"
|
||||
"layout(location=0) in vec2 VPos;\n"
|
||||
"layout(location=1) in vec2 VTex;\n"
|
||||
"layout(location=2) in vec4 VColor;\n"
|
||||
"\n"
|
||||
"out gl_PerVertex { vec4 gl_Position; }; // required because of ARB_separate_shader_objects\n"
|
||||
"out vec2 FUV;\n"
|
||||
"out vec4 FColor;\n"
|
||||
"\n"
|
||||
"void main() {\n"
|
||||
" vec2 pos = VPos * U_InvHalfScreenSize;\n"
|
||||
" pos.y = 2 - pos.y; // invert y axis\n"
|
||||
" pos -= vec2(1, 1); // convert to 0,1 range\n"
|
||||
"\n"
|
||||
" gl_Position = vec4(pos, 0, 1);\n"
|
||||
" FUV = VTex;\n"
|
||||
"\n"
|
||||
" FColor = VColor;\n"
|
||||
"}\n";
|
||||
|
||||
char *glsl_fshader =
|
||||
"#version 450 core\n"
|
||||
"\n"
|
||||
"in vec2 FUV;\n"
|
||||
"in vec4 FColor;\n"
|
||||
"\n"
|
||||
"layout (binding=0) uniform sampler2D S_Texture;\n"
|
||||
"layout (location=0) out vec4 OutColor;\n"
|
||||
"\n"
|
||||
"void main() {\n"
|
||||
" vec4 c = FColor;\n"
|
||||
" c.a *= texture(S_Texture, FUV).r;\n"
|
||||
" OutColor = c;\n"
|
||||
"}\n";
|
||||
|
||||
rn_state.shader2d = rn_create_shader(glsl_vshader, glsl_fshader);
|
||||
}
|
||||
|
||||
void rn_begin(void) {
|
||||
}
|
||||
|
||||
void rn_end(v2f32_t window_size, v4f32_t color) {
|
||||
f32 wx = window_size.x;
|
||||
f32 wy = window_size.y;
|
||||
@@ -303,4 +309,8 @@ void rn_end(v2f32_t window_size, v4f32_t color) {
|
||||
glDrawArrays(GL_TRIANGLES, 0, it->len);
|
||||
} else_is_invalid;
|
||||
}
|
||||
|
||||
rn_state.first_cmd = NULL;
|
||||
rn_state.last_cmd = NULL;
|
||||
rn_state.len = 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user