From d5a52241b0f208bb9245d48744ea69dde0a38844 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Thu, 28 Mar 2019 14:59:54 +1100 Subject: [PATCH] [client] egl: move shaders into seperate files and build into objects --- VERSION | 2 +- client/cmake/MakeObject.cmake | 34 ++++++++ client/renderers/egl/CMakeLists.txt | 13 +++- client/renderers/egl/desktop.c | 81 +++----------------- client/renderers/egl/shader/desktop.vert | 20 +++++ client/renderers/egl/shader/desktop_rgb.frag | 11 +++ client/renderers/egl/shader/desktop_yuv.frag | 27 +++++++ 7 files changed, 114 insertions(+), 74 deletions(-) create mode 100644 client/cmake/MakeObject.cmake create mode 100644 client/renderers/egl/shader/desktop.vert create mode 100644 client/renderers/egl/shader/desktop_rgb.frag create mode 100644 client/renderers/egl/shader/desktop_yuv.frag diff --git a/VERSION b/VERSION index 44dfbb38..919fab5e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -a12-113-gfdba14691c+1 \ No newline at end of file +a12-114-g52c4e15c76+1 \ No newline at end of file diff --git a/client/cmake/MakeObject.cmake b/client/cmake/MakeObject.cmake new file mode 100644 index 00000000..72047833 --- /dev/null +++ b/client/cmake/MakeObject.cmake @@ -0,0 +1,34 @@ +function(make_object out_var) + set(result) + set(result_h) + foreach(in_f ${ARGN}) + file(RELATIVE_PATH out_f ${CMAKE_CURRENT_SOURCE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/${in_f}") + set(out_h "${CMAKE_CURRENT_BINARY_DIR}/${out_f}.h") + set(out_f "${CMAKE_CURRENT_BINARY_DIR}/${out_f}.o") + string(REGEX REPLACE "[/.]" "_" sym_in ${in_f}) + + add_custom_command(OUTPUT ${out_f} + COMMAND ${CMAKE_LINKER} -r -b binary -o ${out_f} ${in_f} + COMMAND ${CMAKE_OBJCOPY} --rename-section .data=.rodata,CONTENTS,ALLOC,LOAD,READONLY,DATA ${out_f} ${out_f} + COMMAND ${CMAKE_OBJCOPY} --redefine-sym _binary_${sym_in}_start=b_${sym_in} ${out_f} ${out_f} + COMMAND ${CMAKE_OBJCOPY} --redefine-sym _binary_${sym_in}_end=b_${sym_in}_end ${out_f} ${out_f} + COMMAND ${CMAKE_OBJCOPY} --strip-symbol _binary_${sym_in}_size ${out_f} ${out_f} + DEPENDS ${in_f} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Creating object from ${in_f}" + VERBATIM + ) + + file(WRITE ${out_h} "extern const char b_${sym_in}[];\n") + file(APPEND ${out_h} "extern const char b_${sym_in}_end[];\n") + file(APPEND ${out_h} "#define b_${sym_in}_size (b_${sym_in}_end - b_${sym_in})\n") + + get_filename_component(h_dir ${out_h} DIRECTORY) + list(APPEND result_h ${h_dir}) + list(APPEND result ${out_f}) + endforeach() + list(REMOVE_DUPLICATES result_h) + + set(${out_var}_OBJS "${result}" PARENT_SCOPE) + set(${out_var}_INCS "${result_h}" PARENT_SCOPE) +endfunction() diff --git a/client/renderers/egl/CMakeLists.txt b/client/renderers/egl/CMakeLists.txt index 786bc2fb..83989647 100644 --- a/client/renderers/egl/CMakeLists.txt +++ b/client/renderers/egl/CMakeLists.txt @@ -1,13 +1,22 @@ cmake_minimum_required(VERSION 3.6) project(renderer_egl LANGUAGES C) -find_package(PkgConfig) +include(GNUInstallDirs) +include(MakeObject) + pkg_check_modules(RENDERER_EGL_PKGCONFIG REQUIRED egl wayland-egl gl ) +make_object( + EGL_SHADER + shader/desktop.vert + shader/desktop_rgb.frag + shader/desktop_yuv.frag +) + add_library(renderer_egl STATIC egl.c shader.c @@ -19,6 +28,7 @@ add_library(renderer_egl STATIC draw.c splash.c alert.c + ${EGL_SHADER_OBJS} ) target_link_libraries(renderer_egl @@ -28,5 +38,6 @@ target_link_libraries(renderer_egl target_include_directories(renderer_egl PRIVATE src + ${EGL_SHADER_INCS} ${RENDERER_EGL_PKGCONFIG_INCLUDE_DIRS} ) diff --git a/client/renderers/egl/desktop.c b/client/renderers/egl/desktop.c index 77598be3..16ec0199 100644 --- a/client/renderers/egl/desktop.c +++ b/client/renderers/egl/desktop.c @@ -28,6 +28,11 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include #include +// these headers are auto generated by cmake +#include "desktop.vert.h" +#include "desktop_rgb.frag.h" +#include "desktop_yuv.frag.h" + struct EGL_Desktop { EGL_Texture * texture; @@ -49,74 +54,6 @@ struct EGL_Desktop bool update; }; -static const char vertex_shader[] = "\ -#version 300 es\n\ -\ -layout(location = 0) in vec3 vertexPosition_modelspace;\ -layout(location = 1) in vec2 vertexUV;\ -\ -uniform vec4 position;\ -\ -out highp vec2 uv;\ -\ -void main()\ -{\ - gl_Position.xyz = vertexPosition_modelspace; \ - gl_Position.w = 1.0; \ - gl_Position.x -= position.x; \ - gl_Position.y -= position.y; \ - gl_Position.x *= position.z; \ - gl_Position.y *= position.w; \ -\ - uv = vertexUV;\ -}\ -"; - - -static const char frag_generic[] = "\ -#version 300 es\n\ -\ -in highp vec2 uv;\ -out highp vec4 color;\ -\ -uniform sampler2D sampler1;\ - \ -void main()\ -{\ - color = texture(sampler1, uv);\ -}\ -"; - -static const char frag_yuv[] = "\ -#version 300 es\n\ -\ -in highp vec2 uv;\ -out highp vec4 color;\ -\ -uniform sampler2D sampler1;\ -uniform sampler2D sampler2;\ -uniform sampler2D sampler3;\ -\ -void main()\ -{\ - highp vec4 yuv = vec4(\ - texture(sampler1, uv).r,\ - texture(sampler2, uv).r,\ - texture(sampler3, uv).r,\ - 1.0\ - );\ - \ - highp mat4 yuv_to_rgb = mat4(\ - 1.0, 0.0 , 1.402, -0.701,\ - 1.0, -0.344, -0.714, 0.529,\ - 1.0, 1.772, 0.0 , -0.886,\ - 1.0, 1.0 , 1.0 , 1.0\ - );\ - \ - color = yuv * yuv_to_rgb;\ -}\ -"; - bool egl_desktop_init(EGL_Desktop ** desktop) { *desktop = (EGL_Desktop *)malloc(sizeof(EGL_Desktop)); @@ -147,16 +84,16 @@ bool egl_desktop_init(EGL_Desktop ** desktop) } if (!egl_shader_compile((*desktop)->shader_generic, - vertex_shader, sizeof(vertex_shader), - frag_generic , sizeof(frag_generic))) + b_shader_desktop_vert , b_shader_desktop_vert_size, + b_shader_desktop_rgb_frag, b_shader_desktop_rgb_frag_size)) { DEBUG_ERROR("Failed to compile the generic desktop shader"); return false; } if (!egl_shader_compile((*desktop)->shader_yuv, - vertex_shader, sizeof(vertex_shader), - frag_yuv , sizeof(frag_yuv ))) + b_shader_desktop_vert , b_shader_desktop_vert_size, + b_shader_desktop_yuv_frag, b_shader_desktop_yuv_frag_size)) { DEBUG_ERROR("Failed to compile the yuv desktop shader"); return false; diff --git a/client/renderers/egl/shader/desktop.vert b/client/renderers/egl/shader/desktop.vert new file mode 100644 index 00000000..3472a22b --- /dev/null +++ b/client/renderers/egl/shader/desktop.vert @@ -0,0 +1,20 @@ +#version 300 es + +layout(location = 0) in vec3 vertexPosition_modelspace; +layout(location = 1) in vec2 vertexUV; + +uniform vec4 position; + +out highp vec2 uv; + +void main() +{ + gl_Position.xyz = vertexPosition_modelspace; + gl_Position.w = 1.0; + gl_Position.x -= position.x; + gl_Position.y -= position.y; + gl_Position.x *= position.z; + gl_Position.y *= position.w; + + uv = vertexUV; +} diff --git a/client/renderers/egl/shader/desktop_rgb.frag b/client/renderers/egl/shader/desktop_rgb.frag new file mode 100644 index 00000000..a435c6d9 --- /dev/null +++ b/client/renderers/egl/shader/desktop_rgb.frag @@ -0,0 +1,11 @@ +#version 300 es + +in highp vec2 uv; +out highp vec4 color; + +uniform sampler2D sampler1; + +void main() +{ + color = texture(sampler1, uv); +} diff --git a/client/renderers/egl/shader/desktop_yuv.frag b/client/renderers/egl/shader/desktop_yuv.frag new file mode 100644 index 00000000..98643bc3 --- /dev/null +++ b/client/renderers/egl/shader/desktop_yuv.frag @@ -0,0 +1,27 @@ +#version 300 es + +in highp vec2 uv; +out highp vec4 color; + +uniform sampler2D sampler1; +uniform sampler2D sampler2; +uniform sampler2D sampler3; + +void main() +{ + highp vec4 yuv = vec4( + texture(sampler1, uv).r, + texture(sampler2, uv).r, + texture(sampler3, uv).r, + 1.0 + ); + + highp mat4 yuv_to_rgb = mat4( + 1.0, 0.0 , 1.402, -0.701, + 1.0, -0.344, -0.714, 0.529, + 1.0, 1.772, 0.0 , -0.886, + 1.0, 1.0 , 1.0 , 1.0 + ); + + color = yuv * yuv_to_rgb; +}