add builtin shaders

This commit is contained in:
Green Sky 2022-02-21 00:42:18 +01:00
parent 338fbf70d0
commit 4029dcd50c
6 changed files with 181 additions and 1 deletions

View File

@ -9,6 +9,9 @@ add_library(opengl_renderer_s
src/mm/opengl/camera_3d.hpp src/mm/opengl/camera_3d.hpp
src/mm/opengl/camera_3d.cpp src/mm/opengl/camera_3d.cpp
src/mm/opengl/res/shaders_builtin.hpp
src/mm/opengl/res/shaders_builtin.cpp
) )
target_include_directories(opengl_renderer_s PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src") target_include_directories(opengl_renderer_s PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")

View File

@ -0,0 +1,90 @@
#include "./shaders_builtin.hpp"
#include <mm/fs_const_archiver.hpp>
namespace MM::OpenGL {
void load_builtin_shaders_fs(void) {
// ==================== sampling.glsl ==================== START
FS_CONST_MOUNT_FILE("/shaders/builtin/sampling.glsl",
R"(
#ifndef INCLUDE_BUILTIN_SAMPLING
#define INCLUDE_BUILTIN_SAMPLING
// kernel defined 9tap sampling
// samper, uv, 1.0/textureSize(), kernel, sum of kernel
vec3 sampling2D_kernel3x3_vec3(in sampler2D tex, in vec2 pos, in vec2 dir_step, in mat3x3 kernel, in float kernel_weight) {
vec3 color =
// upper row
texture(tex, pos + dir_step * vec2(-1.0, 1.0)).rgb * kernel[0][0]
+ texture(tex, pos + dir_step * vec2(0.0, 1.0)).rgb * kernel[0][1]
+ texture(tex, pos + dir_step * vec2(1.0, 1.0)).rgb * kernel[0][2]
// middle row
+ texture(tex, pos + dir_step * vec2(-1.0, 0.0)).rgb * kernel[1][0]
+ texture(tex, pos).rgb * kernel[1][1]
+ texture(tex, pos + dir_step * vec2(1.0, 0.0)).rgb * kernel[1][2]
// lower row
+ texture(tex, pos + dir_step * vec2(-1.0, -1.0)).rgb * kernel[2][0]
+ texture(tex, pos + dir_step * vec2(0.0, -1.0)).rgb * kernel[2][1]
+ texture(tex, pos + dir_step * vec2(1.0, -1.0)).rgb * kernel[2][2]
;
return color / vec3(kernel_weight);
}
// 3x3 9tap tent filter
// 1 2 1
// 2 4 2
// 1 2 1
vec3 sampling2D_tent3x3_vec3(in sampler2D tex, in vec2 pos, in vec2 dir_step) {
const mat3x3 tent_kernel = mat3x3(
vec3(1.0, 2.0, 1.0),
vec3(2.0, 4.0, 2.0),
vec3(1.0, 2.0, 1.0)
);
const float tent_kernel_weight = 16.0;
return sampling2D_kernel3x3_vec3(
tex,
pos,
dir_step,
tent_kernel,
tent_kernel_weight
);
}
#endif
)")
// ==================== sampling.glsl ==================== END
// ==================== tonemapping.glsl ==================== START
FS_CONST_MOUNT_FILE("/shaders/builtin/tonemapping.glsl",
R"(
#ifndef INCLUDE_BUILTIN_TONEMAPPING
#define INCLUDE_BUILTIN_TONEMAPPING
// https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
vec3 tonemapACESFilm(vec3 x) {
const float a = 2.51;
const float b = 0.03;
const float c = 2.43;
const float d = 0.59;
const float e = 0.14;
return clamp((x*(a*x+b))/(x*(c*x+d)+e), vec3(0.0), vec3(1.0));
}
vec3 tonemapReinhard(vec3 x) {
return x / (x + vec3(1.0));
}
#endif
)")
// ==================== tonemapping.glsl ==================== END
}
} // MM::OpenGL

View File

@ -0,0 +1,12 @@
#pragma once
namespace MM::OpenGL {
// loads glsl files into const fs at "/shaders/builtin/"
// file list:
// - sampling.glsl
// - tonemapping.glsl
void load_builtin_shaders_fs(void);
} // MM::OpenGL

View File

@ -10,6 +10,7 @@
#include <mm/opengl/texture_loader.hpp> #include <mm/opengl/texture_loader.hpp>
#include "../opengl/res/default_texture.h" // data #include "../opengl/res/default_texture.h" // data
#include "../opengl/res/errig_texture.h" // data #include "../opengl/res/errig_texture.h" // data
#include "../opengl/res/shaders_builtin.hpp" // data-ish
#include <tracy/Tracy.hpp> #include <tracy/Tracy.hpp>
#ifndef MM_OPENGL_3_GLES #ifndef MM_OPENGL_3_GLES
@ -111,7 +112,7 @@ bool OpenGLRenderer::enable(Engine& engine, std::vector<UpdateStrategies::TaskIn
return false; return false;
} }
{ // default texures { // default textures
auto& rm_t = MM::ResourceManager<MM::OpenGL::Texture>::ref(); auto& rm_t = MM::ResourceManager<MM::OpenGL::Texture>::ref();
if (!rm_t.contains("default"_hs)) { if (!rm_t.contains("default"_hs)) {
if (!rm_t.load<MM::OpenGL::TextureLoaderConstBuffer>("default", default_png, default_png_len)) { if (!rm_t.load<MM::OpenGL::TextureLoaderConstBuffer>("default", default_png, default_png_len)) {
@ -125,6 +126,10 @@ bool OpenGLRenderer::enable(Engine& engine, std::vector<UpdateStrategies::TaskIn
} }
} }
{ // builtin shaders
OpenGL::load_builtin_shaders_fs();
}
{ // add task { // add task
task_array.push_back( task_array.push_back(
UpdateStrategies::TaskInfo{"OpenGLRenderer::render"} UpdateStrategies::TaskInfo{"OpenGLRenderer::render"}

View File

@ -9,6 +9,17 @@ add_test(NAME opengl_renderer_s_test COMMAND opengl_renderer_s_test)
################# #################
add_executable(opengl_builtins_test ./builtins.cpp)
target_link_libraries(opengl_builtins_test
opengl_renderer_s
gtest_main
)
add_test(NAME opengl_builtins_test COMMAND opengl_builtins_test)
#################
add_executable(imgui_render_task_test imgui_render_task_test.cpp) add_executable(imgui_render_task_test imgui_render_task_test.cpp)
target_link_libraries(imgui_render_task_test target_link_libraries(imgui_render_task_test

View File

@ -0,0 +1,59 @@
#include <gtest/gtest.h>
#include <mm/engine.hpp>
#include <mm/services/filesystem.hpp>
#include <mm/services/sdl_service.hpp>
#include <mm/services/opengl_renderer.hpp>
//#include <mm/opengl/res/shaders_builtin.hpp>
#include <mm/opengl/shader.hpp>
#include <mm/opengl/shader_builder.hpp>
#include <mm/fs_const_archiver.hpp> // include only works on files rn
static const char* argv0 = "";
TEST(builtins, all) {
MM::Engine engine;
engine.addService<MM::Services::FilesystemService>(argv0);
ASSERT_TRUE(engine.enableService<MM::Services::FilesystemService>());
engine.addService<MM::Services::SDLService>();
ASSERT_TRUE(engine.enableService<MM::Services::SDLService>());
engine.addService<MM::Services::OpenGLRenderer>();
ASSERT_TRUE(engine.enableService<MM::Services::OpenGLRenderer>()); // adds builtins
engine.update();
FS_CONST_MOUNT_FILE("/shaders/test_frag.glsl",
GLSL_VERSION_STRING
R"(
#ifdef GL_ES
precision mediump float;
#endif
#include "/shaders/builtin/sampling.glsl"
#include "/shaders/builtin/tonemapping.glsl"
void main() {
}
)");
auto sb = MM::OpenGL::ShaderBuilder::start();
sb.addStageVertex("void main() {}");
sb.addStageFragmentF(engine, "/shaders/test_frag.glsl");
auto shader = sb.finish();
ASSERT_TRUE(shader);
}
int main(int argc, char** argv) {
argv0 = argv[0];
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}