diff --git a/src/emscripten.h b/src/emscripten.h new file mode 100644 index 000000000..90206c33a --- /dev/null +++ b/src/emscripten.h @@ -0,0 +1,32 @@ +/* + * Copyright 2011-2020 Branimir Karadzic. All rights reserved. + * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause + */ + +#ifndef BGFX_EMSCRIPTEN_H_HEADER_GUARD +#define BGFX_EMSCRIPTEN_H_HEADER_GUARD + +#if BX_PLATFORM_EMSCRIPTEN + +# include +# include + +# define _EMSCRIPTEN_CHECK(_check, _call) \ + BX_MACRO_BLOCK_BEGIN \ + EMSCRIPTEN_RESULT __result__ = _call; \ + BX_ASSERT(EMSCRIPTEN_RESULT_SUCCESS == __result__, #_call " FAILED 0x%08x\n", (uint32_t)__result__); \ + BX_MACRO_BLOCK_END + +# if BGFX_CONFIG_DEBUG +# define EMSCRIPTEN_CHECK(_call) _EMSCRIPTEN_CHECK(BX_ASSERT, _call) +# else +# define EMSCRIPTEN_CHECK(_call) _call +# endif // BGFX_CONFIG_DEBUG + +# ifndef HTML5_TARGET_CANVAS_SELECTOR +# define HTML5_TARGET_CANVAS_SELECTOR "#canvas" +# endif // HTML5_TARGET_CANVAS_SELECTOR + +#endif // BX_PLATFORM_EMSCRIPTEN + +#endif // BGFX_EMSCRIPTEN_H_HEADER_GUARD diff --git a/src/glcontext_egl.cpp b/src/glcontext_egl.cpp index 5bf55c873..287195023 100644 --- a/src/glcontext_egl.cpp +++ b/src/glcontext_egl.cpp @@ -370,11 +370,7 @@ EGL_IMPORT ANativeWindow_setBuffersGeometry( (ANativeWindow*)g_platformData.nwh, _width, _height, format); } # elif BX_PLATFORM_EMSCRIPTEN -// TODO: Make this somehow configurable to the developer building a HTML5 page. Currently #canvas -// will take the first canvas element found on the web page. -#define HTML5_TARGET_CANVAS_SELECTOR "#canvas" - - BX_ASSERT(emscripten_set_canvas_element_size(HTML5_TARGET_CANVAS_SELECTOR, _width, _height) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_canvas_element_size() failed in GlContext::resize()!"); + EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(HTML5_TARGET_CANVAS_SELECTOR, _width, _height) ); # else BX_UNUSED(_width, _height); # endif // BX_PLATFORM_* diff --git a/src/glcontext_html5.cpp b/src/glcontext_html5.cpp index 0996a2bf1..251a23c3e 100644 --- a/src/glcontext_html5.cpp +++ b/src/glcontext_html5.cpp @@ -10,8 +10,7 @@ # if BGFX_USE_HTML5 -#include -#include +# include "emscripten.h" // from emscripten gl.c, because we're not going to go // through egl @@ -45,13 +44,13 @@ namespace bgfx { namespace gl ~SwapChainGL() { - emscripten_webgl_destroy_context(m_context); + EMSCRIPTEN_CHECK(emscripten_webgl_destroy_context(m_context) ); BX_FREE(g_allocator, m_canvas); } void makeCurrent() { - BX_ASSERT(emscripten_webgl_make_context_current(m_context) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_webgl_make_context_current() failed!"); + EMSCRIPTEN_CHECK(emscripten_webgl_make_context_current(m_context) ); } void swapBuffers() @@ -74,8 +73,11 @@ namespace bgfx { namespace gl m_primary = createSwapChain((void*)canvas); - if (_width && _height) - BX_ASSERT(emscripten_set_canvas_element_size(canvas, (int)_width, (int)_height) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_canvas_element_size() failed in GlContext::create()!"); + if (0 != _width + && 0 != _height) + { + EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(canvas, (int)_width, (int)_height) ); + } makeCurrent(m_primary); } @@ -101,7 +103,7 @@ namespace bgfx { namespace gl return; } - BX_ASSERT(emscripten_set_canvas_element_size(m_primary->m_canvas, (int) _width, (int) _height) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_set_canvas_element_size() failed in GlContext::resize()!"); + EMSCRIPTEN_CHECK(emscripten_set_canvas_element_size(m_primary->m_canvas, (int) _width, (int) _height) ); } SwapChainGL* GlContext::createSwapChain(void* _nwh) @@ -125,9 +127,10 @@ namespace bgfx { namespace gl { s_attrs.majorVersion = version; EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context(canvas, &s_attrs); + if (context > 0) { - BX_ASSERT(emscripten_webgl_make_context_current(context) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_webgl_make_context_current() failed in GlContext::createSwapChain()!"); + EMSCRIPTEN_CHECK(emscripten_webgl_make_context_current(context) ); SwapChainGL* swapChain = BX_NEW(g_allocator, SwapChainGL)(context, canvas); @@ -137,7 +140,8 @@ namespace bgfx { namespace gl } error = (int) context; } - BX_TRACE("Failed to create WebGL context. (Canvas handle: '%s', last attempt error %d)", canvas, error); + + BX_TRACE("Failed to create WebGL context. (Canvas handle: '%s', last attempt error %d)", canvas, error); return NULL; } diff --git a/src/renderer_gl.cpp b/src/renderer_gl.cpp index b083b6596..4ecbe0b49 100644 --- a/src/renderer_gl.cpp +++ b/src/renderer_gl.cpp @@ -9,10 +9,7 @@ # include "renderer_gl.h" # include # include - -#if BX_PLATFORM_EMSCRIPTEN -# include -#endif +# include "emscripten.h" namespace bgfx { namespace gl { @@ -1596,11 +1593,14 @@ namespace bgfx { namespace gl , GLsizei _dim = 16 ) { - // Avoid creating test textures for WebGL, that causes error noise in the browser console; instead examine the supported texture formats from the spec. + // Avoid creating test textures for WebGL, that causes error noise in the browser + // console; instead examine the supported texture formats from the spec. EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_get_current_context(); EmscriptenWebGLContextAttributes attrs; - BX_ASSERT(emscripten_webgl_get_context_attributes(ctx, &attrs) == EMSCRIPTEN_RESULT_SUCCESS, "emscripten_webgl_get_context_attributes() failed in isTextureFormatValidPerSpec()!"); + + EMSCRIPTEN_CHECK(emscripten_webgl_get_context_attributes(ctx, &attrs) ); int glesVersion = attrs.majorVersion + 1; + switch(_format) { case TextureFormat::A8: @@ -1608,9 +1608,15 @@ namespace bgfx { namespace gl case TextureFormat::R5G6B5: case TextureFormat::RGBA4: case TextureFormat::RGB5A1: - return !_srgb; // GLES2 formats without sRGB. + // GLES2 formats without sRGB. + return !_srgb; + case TextureFormat::D16: - return !_srgb && !_mipAutogen; // GLES2 formats without sRGB, depth textures do not support mipmaps. + // GLES2 formats without sRGB, depth textures do not support mipmaps. + return !_srgb + && !_mipAutogen + ; + case TextureFormat::R16F: case TextureFormat::R32F: case TextureFormat::RG8: @@ -1618,7 +1624,11 @@ namespace bgfx { namespace gl case TextureFormat::RG32F: case TextureFormat::RGB10A2: case TextureFormat::RG11B10F: - return !_srgb && glesVersion >= 3; // GLES3 formats without sRGB + // GLES3 formats without sRGB + return !_srgb + && glesVersion >= 3 + ; + case TextureFormat::R8I: case TextureFormat::R8U: case TextureFormat::R16I: @@ -1645,36 +1655,67 @@ namespace bgfx { namespace gl case TextureFormat::RGB8S: case TextureFormat::RGBA8S: case TextureFormat::RGB9E5F: - return !_srgb && glesVersion >= 3 && !_mipAutogen; // GLES3 formats without sRGB that are not texture filterable or color renderable. + // GLES3 formats without sRGB that are not texture filterable or color renderable. + return !_srgb && glesVersion >= 3 + && !_mipAutogen + ; + case TextureFormat::D24: case TextureFormat::D24S8: case TextureFormat::D32: - return !_srgb && !_mipAutogen && (glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "WEBGL_depth_texture")); // GLES3 formats without sRGB, depth textures do not support mipmaps. + // GLES3 formats without sRGB, depth textures do not support mipmaps. + return !_srgb && !_mipAutogen + && (glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "WEBGL_depth_texture")) + ; + case TextureFormat::D16F: case TextureFormat::D24F: - return !_srgb && !_mipAutogen && glesVersion >= 3; // GLES3 depth formats without sRGB, depth textures do not support mipmaps. + // GLES3 depth formats without sRGB, depth textures do not support mipmaps. + return !_srgb + && !_mipAutogen + && glesVersion >= 3 + ; + case TextureFormat::RGBA16F: case TextureFormat::RGBA32F: - return !_srgb && (glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "OES_texture_half_float")); // GLES3 formats without sRGB + // GLES3 formats without sRGB + return !_srgb + && (glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "OES_texture_half_float") ) + ; + case TextureFormat::RGB8: case TextureFormat::RGBA8: - return !_srgb || glesVersion >= 3 || emscripten_webgl_enable_extension(ctx, "EXT_sRGB"); // sRGB formats + // sRGB formats + return !_srgb + || glesVersion >= 3 + || emscripten_webgl_enable_extension(ctx, "EXT_sRGB") + ; + case TextureFormat::BC1: case TextureFormat::BC2: case TextureFormat::BC3: - return emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_s3tc") - && (!_srgb || emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_s3tc_srgb")); + return emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_s3tc") + && (!_srgb || emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_s3tc_srgb") ) + ; + case TextureFormat::PTC12: case TextureFormat::PTC14: case TextureFormat::PTC12A: case TextureFormat::PTC14A: - return !_srgb && emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_pvrtc"); + return !_srgb + && emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_pvrtc") + ; + case TextureFormat::ETC1: - return !_srgb && emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_etc1"); + return !_srgb + && emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_etc1") + ; + case TextureFormat::ETC2: case TextureFormat::ETC2A: case TextureFormat::ETC2A1: return emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_etc"); + case TextureFormat::ASTC4x4: case TextureFormat::ASTC5x5: case TextureFormat::ASTC6x6: @@ -1682,11 +1723,14 @@ namespace bgfx { namespace gl case TextureFormat::ASTC8x6: case TextureFormat::ASTC10x5: return emscripten_webgl_enable_extension(ctx, "WEBGL_compressed_texture_astc"); + default: - return false; + break; } + + return false; } -#endif +#endif // BX_PLATFORM_EMSCRIPTEN static bool isTextureFormatValid( TextureFormat::Enum _format diff --git a/src/renderer_gl.h b/src/renderer_gl.h index d8e9ae348..ffaca99dc 100644 --- a/src/renderer_gl.h +++ b/src/renderer_gl.h @@ -135,13 +135,11 @@ typedef uint64_t GLuint64; # include "glcontext_html5.h" # endif // BGFX_USE_EGL -# if BX_PLATFORM_EMSCRIPTEN -# include -# endif // BX_PLATFORM_EMSCRIPTEN #endif // BGFX_CONFIG_RENDERER_OPENGL #include "renderer.h" #include "debug_renderdoc.h" +#include "emscripten.h" #ifndef GL_LUMINANCE # define GL_LUMINANCE 0x1909 diff --git a/src/renderer_webgpu.cpp b/src/renderer_webgpu.cpp index e6b0b6a10..76a9deb05 100644 --- a/src/renderer_webgpu.cpp +++ b/src/renderer_webgpu.cpp @@ -12,6 +12,7 @@ # include "renderer_webgpu.h" # include "renderer.h" # include "debug_renderdoc.h" +# include "emscripten.h" # if !BX_PLATFORM_EMSCRIPTEN # ifdef DAWN_ENABLE_BACKEND_D3D12 @@ -25,9 +26,6 @@ # include # include # include -# else -# include -# include # endif // !BX_PLATFORM_EMSCRIPTEN namespace bgfx { namespace webgpu