From 4d7926b698fd7beef22105259bb0e9899f2ce061 Mon Sep 17 00:00:00 2001 From: lisyarus Date: Fri, 3 Jul 2026 22:29:58 +0300 Subject: [PATCH] Final Emscripten OpenGL fixes --- .../include/psemek/gfx/gl.hpp | 4 +-- libs/gfx/api/gles30-emscripten/source/gl.cpp | 28 +++++++++++++------ libs/gfx/include/psemek/gfx/framebuffer.hpp | 6 ++++ libs/gfx/include/psemek/gfx/program.hpp | 5 +++- libs/gfx/source/framebuffer.cpp | 6 ++++ libs/gfx/source/painter.cpp | 5 +--- libs/gfx/source/program.cpp | 12 ++++---- libs/gfx/source/texture.cpp | 4 +++ 8 files changed, 49 insertions(+), 21 deletions(-) diff --git a/libs/gfx/api/gles30-emscripten/include/psemek/gfx/gl.hpp b/libs/gfx/api/gles30-emscripten/include/psemek/gfx/gl.hpp index 08bf384e..0d208f26 100644 --- a/libs/gfx/api/gles30-emscripten/include/psemek/gfx/gl.hpp +++ b/libs/gfx/api/gles30-emscripten/include/psemek/gfx/gl.hpp @@ -1288,8 +1288,8 @@ namespace gl // GL_EXT_texture_filter_anisotropic - constexpr GLenum TEXTURE_MAX_ANISOTROPY_EXT = 0x84FE; - constexpr GLenum MAX_TEXTURE_MAX_ANISOTROPY_EXT = 0x84FF; + constexpr GLenum TEXTURE_MAX_ANISOTROPY = 0x84FE; + constexpr GLenum MAX_TEXTURE_MAX_ANISOTROPY = 0x84FF; namespace sys diff --git a/libs/gfx/api/gles30-emscripten/source/gl.cpp b/libs/gfx/api/gles30-emscripten/source/gl.cpp index 2cef2465..a422f863 100644 --- a/libs/gfx/api/gles30-emscripten/source/gl.cpp +++ b/libs/gfx/api/gles30-emscripten/source/gl.cpp @@ -11,6 +11,9 @@ #include #elif defined(_WIN32) #include +#elif defined(__EMSCRIPTEN__) +#include +#include #else #include #endif @@ -77,7 +80,14 @@ namespace gl return reinterpret_cast(GetProcAddress(image, reinterpret_cast(name))); } - + + #elif defined(__EMSCRIPTEN__) + + static void * get_proc_address(const char *func) + { + return reinterpret_cast(eglGetProcAddress(func)); + } + #else // GLX static void * get_proc_address(const char *func) @@ -886,14 +896,14 @@ namespace gl const char * shader_prefix() { - return R"(#version 300 es - -precision highp int; -precision highp float; -precision highp sampler2D; -precision highp usampler2D; - -)"; + return R"(#version 300 es + +precision highp int; +precision highp float; +precision highp sampler2D; +precision highp usampler2D; + +)"; } } // namespace sys diff --git a/libs/gfx/include/psemek/gfx/framebuffer.hpp b/libs/gfx/include/psemek/gfx/framebuffer.hpp index abc27ff3..f4f69da6 100644 --- a/libs/gfx/include/psemek/gfx/framebuffer.hpp +++ b/libs/gfx/include/psemek/gfx/framebuffer.hpp @@ -34,17 +34,23 @@ namespace psemek::gfx void color(texture_2d const & tex, int attachment = 0); void color(texture_2d_array const & tex, int layer, int attachment = 0); +#ifndef __EMSCRIPTEN__ void color(texture_cubemap const & tex, int attachment = 0); +#endif void color(texture_cubemap const & tex, int face, int attachment = 0); void color(renderbuffer const & rb, int attachment = 0); void depth(texture_2d const & tex); void depth(texture_2d_array const & tex, int layer); +#ifndef __EMSCRIPTEN__ void depth(texture_cubemap const & tex); +#endif void depth(texture_cubemap const & tex, int face); void depth(renderbuffer const & rb); void depth_stencil(texture_2d const & tex); void depth_stencil(texture_2d_array const & tex, int layer); +#ifndef __EMSCRIPTEN__ void depth_stencil(texture_cubemap const & tex); +#endif void depth_stencil(texture_cubemap const & tex, int face); void depth_stencil(renderbuffer const & rb); diff --git a/libs/gfx/include/psemek/gfx/program.hpp b/libs/gfx/include/psemek/gfx/program.hpp index 08136ce8..2b6774ba 100644 --- a/libs/gfx/include/psemek/gfx/program.hpp +++ b/libs/gfx/include/psemek/gfx/program.hpp @@ -16,9 +16,12 @@ namespace psemek::gfx struct program { - program(std::string_view compute_source); program(std::string_view vertex_source, std::string_view fragment_source); + +#ifndef __EMSCRIPTEN__ + program(std::string_view compute_source); program(std::string_view vertex_source, std::string_view geometry_source, std::string_view fragment_source); +#endif program(program && other); diff --git a/libs/gfx/source/framebuffer.cpp b/libs/gfx/source/framebuffer.cpp index d3cb3f4c..811c4c52 100644 --- a/libs/gfx/source/framebuffer.cpp +++ b/libs/gfx/source/framebuffer.cpp @@ -102,11 +102,13 @@ namespace psemek::gfx gl::FramebufferTextureLayer(gl::DRAW_FRAMEBUFFER, gl::COLOR_ATTACHMENT0 + attachment, tex.id(), 0, layer); } +#ifndef __EMSCRIPTEN__ void framebuffer::color(texture_cubemap const & tex, int attachment) { bind(); gl::FramebufferTexture(gl::DRAW_FRAMEBUFFER, gl::COLOR_ATTACHMENT0 + attachment, tex.id(), 0); } +#endif void framebuffer::color(texture_cubemap const & tex, int face, int attachment) { @@ -140,11 +142,13 @@ namespace psemek::gfx gl::FramebufferTextureLayer(gl::DRAW_FRAMEBUFFER, gl::DEPTH_ATTACHMENT, tex.id(), 0, layer); } +#ifndef __EMSCRIPTEN__ void framebuffer::depth(texture_cubemap const & tex) { bind(); gl::FramebufferTexture(gl::DRAW_FRAMEBUFFER, gl::DEPTH_ATTACHMENT, tex.id(), 0); } +#endif void framebuffer::depth(texture_cubemap const & tex, int face) { @@ -178,11 +182,13 @@ namespace psemek::gfx gl::FramebufferTextureLayer(gl::DRAW_FRAMEBUFFER, gl::DEPTH_STENCIL_ATTACHMENT, tex.id(), 0, layer); } +#ifndef __EMSCRIPTEN__ void framebuffer::depth_stencil(texture_cubemap const & tex) { bind(); gl::FramebufferTexture(gl::DRAW_FRAMEBUFFER, gl::DEPTH_STENCIL_ATTACHMENT, tex.id(), 0); } +#endif void framebuffer::depth_stencil(texture_cubemap const & tex, int face) { diff --git a/libs/gfx/source/painter.cpp b/libs/gfx/source/painter.cpp index 28bbac6d..2295192a 100644 --- a/libs/gfx/source/painter.cpp +++ b/libs/gfx/source/painter.cpp @@ -67,7 +67,7 @@ out vec4 out_color; void main() { - out_color = color * texture(u_texture, texcoord); + out_color = color * vec4(vec3(1.0), texture(u_texture, texcoord).r); } )"; @@ -164,9 +164,6 @@ namespace psemek::gfx font_texture.load(gfx::read_image(io::memory_istream{resource::font_9x12_png.data})); font_texture.bind(); - gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_SWIZZLE_G, gl::RED); - gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_SWIZZLE_B, gl::RED); - gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_SWIZZLE_A, gl::RED); gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MAG_FILTER, gl::NEAREST); gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_MIN_FILTER, gl::NEAREST); gl::TexParameteri(gl::TEXTURE_2D, gl::TEXTURE_WRAP_S, gl::CLAMP_TO_EDGE); diff --git a/libs/gfx/source/program.cpp b/libs/gfx/source/program.cpp index a092048a..a0d39c27 100644 --- a/libs/gfx/source/program.cpp +++ b/libs/gfx/source/program.cpp @@ -313,20 +313,22 @@ namespace psemek::gfx return program; } - program::program(std::string_view compute_source) - { - program_ = create_program({{gl::COMPUTE_SHADER, compute_source}}); - } - program::program(std::string_view vertex_source, std::string_view fragment_source) { program_ = create_program({{gl::VERTEX_SHADER, vertex_source}, {gl::FRAGMENT_SHADER, fragment_source}}); } +#ifndef __EMSCRIPTEN__ + program::program(std::string_view compute_source) + { + program_ = create_program({{gl::COMPUTE_SHADER, compute_source}}); + } + program::program(std::string_view vertex_source, std::string_view geometry_source, std::string_view fragment_source) { program_ = create_program({{gl::VERTEX_SHADER, vertex_source}, {gl::GEOMETRY_SHADER, geometry_source}, {gl::FRAGMENT_SHADER, fragment_source}}); } +#endif program::program(program && other) : program_(other.program_) diff --git a/libs/gfx/source/texture.cpp b/libs/gfx/source/texture.cpp index feac9408..6297c35e 100644 --- a/libs/gfx/source/texture.cpp +++ b/libs/gfx/source/texture.cpp @@ -19,7 +19,11 @@ namespace psemek::gfx static std::optional get_max_anisotropy() { +#ifdef __EMSCRIPTEN__ + if (!gl::sys::ext_EXT_texture_filter_anisotropic()) return std::nullopt; +#else if (!gl::sys::ext_ARB_texture_filter_anisotropic()) return std::nullopt; +#endif float level; gl::GetFloatv(gl::MAX_TEXTURE_MAX_ANISOTROPY, &level);