Skip to content

Instantly share code, notes, and snippets.

@floooh
Created May 29, 2020 17:12
Show Gist options
  • Save floooh/d117491d5e9c85543f433978be02cb97 to your computer and use it in GitHub Desktop.
Save floooh/d117491d5e9c85543f433978be02cb97 to your computer and use it in GitHub Desktop.
sokol-gfx with C++20 designated init :/
// dynamic vertex- and index-buffers for imgui-generated geometry
bind.vertex_buffers[0] = sg_make_buffer({
.size = MaxVertices * sizeof(ImDrawVert),
.usage = SG_USAGE_STREAM
});
bind.index_buffer = sg_make_buffer({
.size = MaxIndices * sizeof(ImDrawIdx),
.type = SG_BUFFERTYPE_INDEXBUFFER,
.usage = SG_USAGE_STREAM
});
// font texture for imgui's default font
unsigned char* font_pixels;
int font_width, font_height;
io.Fonts->GetTexDataAsRGBA32(&font_pixels, &font_width, &font_height);
bind.fs_images[0] = sg_make_image({
.width = font_width,
.height = font_height,
.pixel_format = SG_PIXELFORMAT_RGBA8,
.wrap_u = SG_WRAP_CLAMP_TO_EDGE,
.wrap_v = SG_WRAP_CLAMP_TO_EDGE,
.content = {
.subimage = {
{
{ .ptr = font_pixels, .size = font_width * font_height * 4 }
}
}
}
});
// shader object for imgui rendering
sg_shader shd = sg_make_shader({
.attrs = {
{ .name = "position" },
{ .name = "texcoord0" },
{ .name = "color0" }
},
.vs = {
.source =
"uniform vec2 disp_size;\n"
"attribute vec2 position;\n"
"attribute vec2 texcoord0;\n"
"attribute vec4 color0;\n"
"varying vec2 uv;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_Position = vec4(((position/disp_size)-0.5)*vec2(2.0,-2.0), 0.5, 1.0);\n"
" uv = texcoord0;\n"
" color = color0;\n"
"}\n",
.uniform_blocks = {
{
.size = sizeof(vs_params_t),
.uniforms = {
{ .name="disp_size", .type=SG_UNIFORMTYPE_FLOAT2}
}
}
}
},
.fs = {
.source =
"precision mediump float;"
"uniform sampler2D tex;\n"
"varying vec2 uv;\n"
"varying vec4 color;\n"
"void main() {\n"
" gl_FragColor = texture2D(tex, uv) * color;\n"
"}\n",
.images = {
{ .name="tex", .type=SG_IMAGETYPE_2D }
},
}
});
// pipeline object for imgui rendering
pip = sg_make_pipeline({
.layout = {
.buffers = {
{ .stride = sizeof(ImDrawVert) }
},
.attrs = {
{ .offset=offsetof(ImDrawVert,pos), .format=SG_VERTEXFORMAT_FLOAT2 },
{ .offset=offsetof(ImDrawVert,uv), .format=SG_VERTEXFORMAT_FLOAT2 },
{ .offset=offsetof(ImDrawVert,col), .format=SG_VERTEXFORMAT_UBYTE4N }
}
},
.shader = shd,
.index_type = SG_INDEXTYPE_UINT16,
.blend = {
.enabled = true,
.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA,
.dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
.color_write_mask = SG_COLORMASK_RGB
}
});
// initial clear color
pass_action = {
.colors = {
{ .action = SG_ACTION_CLEAR, .val = { 0.0f, 0.5f, 0.7f, 1.0f } }
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment