Skip to content

Commit

Permalink
[imgui] use precompiled shaders for imgui
Browse files Browse the repository at this point in the history
this makes le_imgui self-contained, as it embeds both its default
shaders, and its default font file.

[le_window] reformat

[le_core] update comment
  • Loading branch information
tgfrerer committed Jan 19, 2024
1 parent feadbcc commit ee28fca
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 15 deletions.
4 changes: 2 additions & 2 deletions modules/le_core/le_api_loader_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ LE_MODULE_REGISTER_IMPL( le_module_loader, p_api ) {
// LINUX: these methods are to audit runtime dyanmic library linking and loading.
//
// To enable, start app with environment variable `LD_AUDIT` set to path of
// lible_module_loader.so:
// lible_core.so:
//
// EXPORT LD_AUDIT=./modules/lible_module_loader.so
// EXPORT LD_AUDIT=./modules/lible_core.so

extern "C" unsigned int
la_version( unsigned int version ) {
Expand Down
12 changes: 10 additions & 2 deletions modules/le_imgui/le_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
#include <algorithm>
#include <iterator>

namespace {
// We use an anonymous namespace here because we don't want to export our shader data to other compilation units
// the code for these files is auto-generated via glslang, and you can recompile from source by issueing
// `./compile_shaders.sh` in the shaders directory.
#include "shaders/imgui_frag.h"
#include "shaders/imgui_vert.h"
} // namespace

static le_img_resource_handle IMGUI_IMG_HANDLE = LE_IMG_RESOURCE( "ImguiDefaultFontImage" );

namespace {
Expand Down Expand Up @@ -226,13 +234,13 @@ static void le_imgui_draw_gui( le_imgui_o* self, le_renderpass_o* p_rp ) {
.addShaderStage(
LeShaderModuleBuilder( pipelineManager )
.setShaderStage( le::ShaderStage::eVertex )
.setSourceFilePath( "./resources/shaders/imgui.vert" )
.setSpirvCode( SPIRV_SOURCE_IMGUI_VERT, sizeof( SPIRV_SOURCE_IMGUI_VERT ) / sizeof( uint32_t ) )
.setHandle( LE_SHADER_MODULE_HANDLE( "imgui_vert_shader" ) )
.build() )
.addShaderStage(
LeShaderModuleBuilder( pipelineManager )
.setShaderStage( le::ShaderStage::eFragment )
.setSourceFilePath( "./resources/shaders/imgui.frag" )
.setSpirvCode( SPIRV_SOURCE_IMGUI_FRAG, sizeof( SPIRV_SOURCE_IMGUI_FRAG ) / sizeof( uint32_t ) )
.setHandle( LE_SHADER_MODULE_HANDLE( "imgui_frag_shader" ) )
.build() )
.withAttributeBindingState()
Expand Down
19 changes: 19 additions & 0 deletions modules/le_imgui/shaders/compile_shaders.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Generates .h files holding SPIR-V (encoded as uint32_t array)
# from .vert/.frag/.comp glsl code.

for FILE in *.{comp,vert,frag}; do

# In case there is no match, $FILE may still contain something like "*.comp"
# in which case we want to skip this iteration of the loop.
if [ ! -e "$FILE" ]; then
continue
fi

FILENAME="${FILE%.*}_${FILE#*.}"
VARNAME=$(echo "SPIRV_SOURCE_${FILENAME}" | tr '[:lower:]' '[:upper:]')

echo "$FILE${FILENAME}.h"
glslang -o "${FILENAME}.h" -V --vn "$VARNAME" "$FILE"
done
18 changes: 18 additions & 0 deletions modules/le_imgui/shaders/imgui.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 450 core

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (set = 0, binding = 1) uniform sampler2D tex_unit_0;

// inputs
layout (location = 0) in vec4 inColor;
layout (location = 1) in vec2 inTexCoord;

// outputs
layout (location = 0) out vec4 outFragColor;

void main()
{
outFragColor = inColor * texture( tex_unit_0, inTexCoord.st);
}
33 changes: 33 additions & 0 deletions modules/le_imgui/shaders/imgui.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#version 450 core

#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

// uniforms (resources)
layout (set = 0, binding = 0) uniform Mvp
{
mat4 modelViewProjectionMatrix;
};

// inputs (vertex attributes)
layout (location = 0) in vec2 inPos;
layout (location = 1) in vec2 inTexCoord;
layout (location = 2) in vec4 inColor;

// outputs
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec2 outTexCoord;

// we override the built-in fixed function outputs
// to have more control over the SPIR-V code created.
out gl_PerVertex
{
vec4 gl_Position;
};

void main()
{
outTexCoord = inTexCoord;
outColor = inColor;
gl_Position = modelViewProjectionMatrix * vec4(inPos,0,1);
}
28 changes: 28 additions & 0 deletions modules/le_imgui/shaders/imgui_frag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 1113.1.1
#pragma once
const uint32_t SPIRV_SOURCE_IMGUI_FRAG[] = {
0x07230203,0x00010000,0x0008000b,0x00000018,0x00000000,0x00020011,0x00000001,0x0006000b,
0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
0x0008000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000b,0x00000014,
0x00030010,0x00000004,0x00000007,0x00030003,0x00000002,0x000001c2,0x00090004,0x415f4c47,
0x735f4252,0x72617065,0x5f657461,0x64616873,0x6f5f7265,0x63656a62,0x00007374,0x00090004,
0x415f4c47,0x735f4252,0x69646168,0x6c5f676e,0x75676e61,0x5f656761,0x70303234,0x006b6361,
0x00040005,0x00000004,0x6e69616d,0x00000000,0x00060005,0x00000009,0x4674756f,0x43676172,
0x726f6c6f,0x00000000,0x00040005,0x0000000b,0x6f436e69,0x00726f6c,0x00050005,0x00000010,
0x5f786574,0x74696e75,0x0000305f,0x00050005,0x00000014,0x65546e69,0x6f6f4378,0x00006472,
0x00040047,0x00000009,0x0000001e,0x00000000,0x00040047,0x0000000b,0x0000001e,0x00000000,
0x00040047,0x00000010,0x00000022,0x00000000,0x00040047,0x00000010,0x00000021,0x00000001,
0x00040047,0x00000014,0x0000001e,0x00000001,0x00020013,0x00000002,0x00030021,0x00000003,
0x00000002,0x00030016,0x00000006,0x00000020,0x00040017,0x00000007,0x00000006,0x00000004,
0x00040020,0x00000008,0x00000003,0x00000007,0x0004003b,0x00000008,0x00000009,0x00000003,
0x00040020,0x0000000a,0x00000001,0x00000007,0x0004003b,0x0000000a,0x0000000b,0x00000001,
0x00090019,0x0000000d,0x00000006,0x00000001,0x00000000,0x00000000,0x00000000,0x00000001,
0x00000000,0x0003001b,0x0000000e,0x0000000d,0x00040020,0x0000000f,0x00000000,0x0000000e,
0x0004003b,0x0000000f,0x00000010,0x00000000,0x00040017,0x00000012,0x00000006,0x00000002,
0x00040020,0x00000013,0x00000001,0x00000012,0x0004003b,0x00000013,0x00000014,0x00000001,
0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,0x00000005,0x0004003d,
0x00000007,0x0000000c,0x0000000b,0x0004003d,0x0000000e,0x00000011,0x00000010,0x0004003d,
0x00000012,0x00000015,0x00000014,0x00050057,0x00000007,0x00000016,0x00000011,0x00000015,
0x00050085,0x00000007,0x00000017,0x0000000c,0x00000016,0x0003003e,0x00000009,0x00000017,
0x000100fd,0x00010038
};
45 changes: 45 additions & 0 deletions modules/le_imgui/shaders/imgui_vert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 1113.1.1
#pragma once
const uint32_t SPIRV_SOURCE_IMGUI_VERT[] = {
0x07230203,0x00010000,0x0008000b,0x00000028,0x00000000,0x00020011,0x00000001,0x0006000b,
0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
0x000b000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x00000009,0x0000000b,0x0000000f,
0x00000011,0x00000015,0x0000001f,0x00030003,0x00000002,0x000001c2,0x00090004,0x415f4c47,
0x735f4252,0x72617065,0x5f657461,0x64616873,0x6f5f7265,0x63656a62,0x00007374,0x00090004,
0x415f4c47,0x735f4252,0x69646168,0x6c5f676e,0x75676e61,0x5f656761,0x70303234,0x006b6361,
0x00040005,0x00000004,0x6e69616d,0x00000000,0x00050005,0x00000009,0x5474756f,0x6f437865,
0x0064726f,0x00050005,0x0000000b,0x65546e69,0x6f6f4378,0x00006472,0x00050005,0x0000000f,
0x4374756f,0x726f6c6f,0x00000000,0x00040005,0x00000011,0x6f436e69,0x00726f6c,0x00060005,
0x00000013,0x505f6c67,0x65567265,0x78657472,0x00000000,0x00060006,0x00000013,0x00000000,
0x505f6c67,0x7469736f,0x006e6f69,0x00030005,0x00000015,0x00000000,0x00030005,0x00000019,
0x0070764d,0x000a0006,0x00000019,0x00000000,0x65646f6d,0x6569566c,0x6f725077,0x7463656a,
0x4d6e6f69,0x69727461,0x00000078,0x00030005,0x0000001b,0x00000000,0x00040005,0x0000001f,
0x6f506e69,0x00000073,0x00040047,0x00000009,0x0000001e,0x00000001,0x00040047,0x0000000b,
0x0000001e,0x00000001,0x00040047,0x0000000f,0x0000001e,0x00000000,0x00040047,0x00000011,
0x0000001e,0x00000002,0x00050048,0x00000013,0x00000000,0x0000000b,0x00000000,0x00030047,
0x00000013,0x00000002,0x00040048,0x00000019,0x00000000,0x00000005,0x00050048,0x00000019,
0x00000000,0x00000023,0x00000000,0x00050048,0x00000019,0x00000000,0x00000007,0x00000010,
0x00030047,0x00000019,0x00000002,0x00040047,0x0000001b,0x00000022,0x00000000,0x00040047,
0x0000001b,0x00000021,0x00000000,0x00040047,0x0000001f,0x0000001e,0x00000000,0x00020013,
0x00000002,0x00030021,0x00000003,0x00000002,0x00030016,0x00000006,0x00000020,0x00040017,
0x00000007,0x00000006,0x00000002,0x00040020,0x00000008,0x00000003,0x00000007,0x0004003b,
0x00000008,0x00000009,0x00000003,0x00040020,0x0000000a,0x00000001,0x00000007,0x0004003b,
0x0000000a,0x0000000b,0x00000001,0x00040017,0x0000000d,0x00000006,0x00000004,0x00040020,
0x0000000e,0x00000003,0x0000000d,0x0004003b,0x0000000e,0x0000000f,0x00000003,0x00040020,
0x00000010,0x00000001,0x0000000d,0x0004003b,0x00000010,0x00000011,0x00000001,0x0003001e,
0x00000013,0x0000000d,0x00040020,0x00000014,0x00000003,0x00000013,0x0004003b,0x00000014,
0x00000015,0x00000003,0x00040015,0x00000016,0x00000020,0x00000001,0x0004002b,0x00000016,
0x00000017,0x00000000,0x00040018,0x00000018,0x0000000d,0x00000004,0x0003001e,0x00000019,
0x00000018,0x00040020,0x0000001a,0x00000002,0x00000019,0x0004003b,0x0000001a,0x0000001b,
0x00000002,0x00040020,0x0000001c,0x00000002,0x00000018,0x0004003b,0x0000000a,0x0000001f,
0x00000001,0x0004002b,0x00000006,0x00000021,0x00000000,0x0004002b,0x00000006,0x00000022,
0x3f800000,0x00050036,0x00000002,0x00000004,0x00000000,0x00000003,0x000200f8,0x00000005,
0x0004003d,0x00000007,0x0000000c,0x0000000b,0x0003003e,0x00000009,0x0000000c,0x0004003d,
0x0000000d,0x00000012,0x00000011,0x0003003e,0x0000000f,0x00000012,0x00050041,0x0000001c,
0x0000001d,0x0000001b,0x00000017,0x0004003d,0x00000018,0x0000001e,0x0000001d,0x0004003d,
0x00000007,0x00000020,0x0000001f,0x00050051,0x00000006,0x00000023,0x00000020,0x00000000,
0x00050051,0x00000006,0x00000024,0x00000020,0x00000001,0x00070050,0x0000000d,0x00000025,
0x00000023,0x00000024,0x00000021,0x00000022,0x00050091,0x0000000d,0x00000026,0x0000001e,
0x00000025,0x00050041,0x0000000e,0x00000027,0x00000015,0x00000017,0x0003003e,0x00000027,
0x00000026,0x000100fd,0x00010038
};
21 changes: 10 additions & 11 deletions modules/le_window/le_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#endif
#include "GLFW/glfw3native.h"

constexpr size_t EVENT_QUEUE_SIZE = ( 4096 * 4 ) / sizeof( LeUiEvent ); // Allocate a few pages for events
constexpr size_t EVENT_QUEUE_SIZE = ( 4096 * 4 ) / sizeof( LeUiEvent ); // Allocate a few pages for events
constexpr auto GAMEPAD_SUBSCRIBERS_SINGLETON_ID = hash_64_fnv1a_const( "le_window_gamepad_subscribers" );

struct le_window_settings_o {
Expand Down Expand Up @@ -335,7 +335,6 @@ static void glfw_framebuffer_resize_callback( GLFWwindow* glfwWindow, int width_

window->mSurfaceExtent.width = uint32_t( w );
window->mSurfaceExtent.height = uint32_t( h );

};

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -931,15 +930,15 @@ LE_MODULE_REGISTER_IMPL( le_window, api ) {
window_settings_i.set_height = window_settings_set_height;
window_settings_i.set_gamepads_active = window_settings_set_gamepads_active;

auto& callbacks_i = window_api_i->window_callbacks_i;
callbacks_i.glfw_key_callback_addr = ( void* )glfw_window_key_callback;
callbacks_i.glfw_char_callback_addr = ( void* )glfw_window_character_callback;
callbacks_i.glfw_cursor_pos_callback_addr = ( void* )glfw_window_cursor_position_callback;
callbacks_i.glfw_cursor_enter_callback_addr = ( void* )glfw_window_cursor_enter_callback;
callbacks_i.glfw_mouse_button_callback_addr = ( void* )glfw_window_mouse_button_callback;
callbacks_i.glfw_scroll_callback_addr = ( void* )glfw_window_scroll_callback;
callbacks_i.glfw_framebuffer_size_callback_addr = ( void* )glfw_framebuffer_resize_callback;
callbacks_i.glfw_drop_callback_addr = ( void* )glfw_window_drop_callback;
auto& callbacks_i = window_api_i->window_callbacks_i;
callbacks_i.glfw_key_callback_addr = ( void* )glfw_window_key_callback;
callbacks_i.glfw_char_callback_addr = ( void* )glfw_window_character_callback;
callbacks_i.glfw_cursor_pos_callback_addr = ( void* )glfw_window_cursor_position_callback;
callbacks_i.glfw_cursor_enter_callback_addr = ( void* )glfw_window_cursor_enter_callback;
callbacks_i.glfw_mouse_button_callback_addr = ( void* )glfw_window_mouse_button_callback;
callbacks_i.glfw_scroll_callback_addr = ( void* )glfw_window_scroll_callback;
callbacks_i.glfw_framebuffer_size_callback_addr = ( void* )glfw_framebuffer_resize_callback;
callbacks_i.glfw_drop_callback_addr = ( void* )glfw_window_drop_callback;
callbacks_i.glfw_joystick_connection_callback_addr = ( void* )glfw_joystick_connection_callback;

#if defined PLUGINS_DYNAMIC
Expand Down

0 comments on commit ee28fca

Please sign in to comment.