Enable cache render data for webgl by default (#2151)

* Remove unused static function
This commit is contained in:
halx99 2024-09-15 06:00:48 +08:00 committed by GitHub
parent 9258d8a47f
commit cfcdddd89b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 124 additions and 103 deletions

View File

@ -531,9 +531,9 @@ set(AX_WASM_SHELL_FILE "${_AX_ROOT}/core/platform/wasm/shell_minimal.html" CACHE
option(AX_WASM_ENABLE_DEVTOOLS "Enable wasm devtools" ON) option(AX_WASM_ENABLE_DEVTOOLS "Enable wasm devtools" ON)
set(_AX_WASM_EXPORTS "_main") set(_AX_WASM_EXPORTS "_main,_axmol_wgl_context_lost,_axmol_wgl_context_restored,_axmol_hdoc_visibilitychange")
if(AX_WASM_ENABLE_DEVTOOLS) if(AX_WASM_ENABLE_DEVTOOLS)
set(_AX_WASM_EXPORTS "${_AX_WASM_EXPORTS},_axmol_director_pause,_axmol_director_resume,_axmol_director_step") string(APPEND _AX_WASM_EXPORTS ",_axmol_dev_pause,_axmol_dev_resume,_axmol_dev_step")
endif() endif()
set(AX_WASM_EXPORTS "${_AX_WASM_EXPORTS}" CACHE STRING "" FORCE) set(AX_WASM_EXPORTS "${_AX_WASM_EXPORTS}" CACHE STRING "" FORCE)

View File

@ -95,13 +95,6 @@ void RenderTexture::listenToBackground(EventCustom* /*event*/)
void RenderTexture::listenToForeground(EventCustom* /*event*/) void RenderTexture::listenToForeground(EventCustom* /*event*/)
{ {
#if AX_ENABLE_CACHE_TEXTURE_DATA #if AX_ENABLE_CACHE_TEXTURE_DATA
const Vec2& s = _texture2D->getContentSizeInPixels();
// TODO new-renderer: field _depthAndStencilFormat removal
// if (_depthAndStencilFormat != 0)
// {
// setupDepthAndStencil(s.width, s.height);
// }
_texture2D->setAntiAliasTexParameters(); _texture2D->setAntiAliasTexParameters();
#endif #endif
} }

View File

@ -23,8 +23,8 @@
THE SOFTWARE. THE SOFTWARE.
****************************************************************************/ ****************************************************************************/
#ifndef __CCEVENT_TYPE_H__ #ifndef __AXEVENT_TYPE_H__
#define __CCEVENT_TYPE_H__ #define __AXEVENT_TYPE_H__
/// @cond DO_NOT_SHOW /// @cond DO_NOT_SHOW
/** /**

View File

@ -27,11 +27,12 @@ THE SOFTWARE.
#include "base/Random.h" #include "base/Random.h"
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
#include <limits> # include <limits>
#include <emscripten.h> # include <emscripten.h>
std::mt19937& ax::RandomHelper::getEngine() std::mt19937& ax::RandomHelper::getEngine()
{ {
static std::mt19937 engine(emscripten_random() * (std::numeric_limits<int>::max)()); static std::mt19937 engine(
static_cast<int>(emscripten_random() * static_cast<float>((std::numeric_limits<int>::max)())));
return engine; return engine;
} }
#else #else

View File

@ -172,7 +172,7 @@ namespace ax { namespace network {
dir = storagePath.substr(0, found + 1); dir = storagePath.substr(0, found + 1);
if (false == util->isDirectoryExist(dir)) if (false == util->isDirectoryExist(dir))
{ {
if (false == util->createDirectory(dir)) if (false == util->createDirectories(dir))
{ {
errCode = DownloadTask::ERROR_CREATE_DIR_FAILED; errCode = DownloadTask::ERROR_CREATE_DIR_FAILED;
errCodeInternal = 0; errCodeInternal = 0;

View File

@ -88,7 +88,7 @@ Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
* *
* @since v0.99.5 * @since v0.99.5
*/ */
#if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID) #if (AX_TARGET_PLATFORM == AX_PLATFORM_ANDROID) || (AX_TARGET_PLATFORM == AX_PLATFORM_WASM)
# if !defined(AX_ENABLE_CACHE_TEXTURE_DATA) # if !defined(AX_ENABLE_CACHE_TEXTURE_DATA)
# define AX_ENABLE_CACHE_TEXTURE_DATA 1 # define AX_ENABLE_CACHE_TEXTURE_DATA 1
# endif # endif

View File

@ -88,7 +88,7 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeInit(JNIEnv*, jcla
auto glView = director->getGLView(); auto glView = director->getGLView();
if (!glView) if (!glView)
{ {
glView = ax::GLViewImpl::create("Android app"); glView = ax::GLViewImpl::create("axmol2");
glView->setFrameSize(w, h); glView->setFrameSize(w, h);
director->setGLView(glView); director->setGLView(glView);
@ -97,7 +97,7 @@ JNIEXPORT void JNICALL Java_org_axmol_lib_AxmolRenderer_nativeInit(JNIEnv*, jcla
else else
{ {
backend::DriverBase::getInstance()->resetState(); backend::DriverBase::getInstance()->resetState();
ax::Director::getInstance()->resetMatrixStack(); director->resetMatrixStack();
ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED); ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true); director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true);
director->setGLDefaultValues(); director->setGLDefaultValues();

View File

@ -28,35 +28,71 @@ THE SOFTWARE.
#include "platform/PlatformConfig.h" #include "platform/PlatformConfig.h"
#if AX_TARGET_PLATFORM == AX_PLATFORM_WASM #if AX_TARGET_PLATFORM == AX_PLATFORM_WASM
#include "platform/wasm/Application-wasm.h" # include "platform/wasm/Application-wasm.h"
#include <unistd.h> # include "platform/wasm/devtools-wasm.h"
#include <sys/time.h> # include <unistd.h>
#include <string> # include <sys/time.h>
#include "base/Director.h" # include <string>
#include "base/Utils.h" # include "base/Director.h"
#include "platform/FileUtils.h" # include "base/Utils.h"
#include <emscripten/emscripten.h> # include "platform/FileUtils.h"
# include <emscripten/emscripten.h>
extern "C" {
//
void axmol_hdoc_visibilitychange(bool hidden)
{
ax::EventCustom event(hidden ? EVENT_COME_TO_BACKGROUND : EVENT_COME_TO_FOREGROUND);
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&event, true);
}
// webglcontextlost
void axmol_wgl_context_lost()
{
AXLOGI("receive event: webglcontextlost");
}
// webglcontextrestored
void axmol_wgl_context_restored()
{
AXLOGI("receive event: webglcontextrestored");
auto director = ax::Director::getInstance();
ax::backend::DriverBase::getInstance()->resetState();
director->resetMatrixStack();
ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true);
director->setGLDefaultValues();
# if AX_ENABLE_CACHE_TEXTURE_DATA
ax::VolatileTextureMgr::reloadAllTextures();
# endif
}
void axmol_dev_pause()
{
ax::DevToolsImpl::getInstance()->pause();
}
void axmol_dev_resume()
{
ax::DevToolsImpl::getInstance()->resume();
}
void axmol_dev_step()
{
ax::DevToolsImpl::getInstance()->step();
}
}
namespace ax namespace ax
{ {
// sharedApplication pointer // sharedApplication pointer
Application * Application::sm_pSharedApplication = nullptr; Application* Application::sm_pSharedApplication = nullptr;
static long getCurrentMillSecond() { Application::Application() : _animationSpeed(60)
long lLastTime;
struct timeval stCurrentTime;
gettimeofday(&stCurrentTime,NULL);
lLastTime = stCurrentTime.tv_sec*1000+stCurrentTime.tv_usec*0.001; // milliseconds
return lLastTime;
}
Application::Application()
: _animationSpeed(60)
{ {
AX_ASSERT(! sm_pSharedApplication); AX_ASSERT(!sm_pSharedApplication);
sm_pSharedApplication = this; sm_pSharedApplication = this;
} }
@ -69,7 +105,7 @@ Application::~Application()
extern "C" void mainLoopIter(void) extern "C" void mainLoopIter(void)
{ {
auto director = Director::getInstance(); auto director = Director::getInstance();
auto glview = director->getGLView(); auto glview = director->getGLView();
director->mainLoop(); director->mainLoop();
glview->pollEvents(); glview->pollEvents();
@ -79,25 +115,25 @@ int Application::run()
{ {
initGLContextAttrs(); initGLContextAttrs();
// Initialize instance and cocos2d. // Initialize instance and cocos2d.
if (! applicationDidFinishLaunching()) if (!applicationDidFinishLaunching())
{ {
return 1; return 1;
} }
auto director = Director::getInstance(); auto director = Director::getInstance();
auto glview = director->getGLView(); auto glview = director->getGLView();
// Retain glview to avoid glview being released in the while loop // Retain glview to avoid glview being released in the while loop
glview->retain(); glview->retain();
//emscripten_set_main_loop(&mainLoopIter, 0, 1); // emscripten_set_main_loop(&mainLoopIter, 0, 1);
emscripten_set_main_loop(&mainLoopIter, _animationSpeed, 1); emscripten_set_main_loop(&mainLoopIter, _animationSpeed, 1);
// TODO: ? does these cleanup really run? // TODO: ? does these cleanup really run?
/* Only work on Desktop /* Only work on Desktop
* Director::mainLoop is really one frame logic * Director::mainLoop is really one frame logic
* when we want to close the window, we should call Director::end(); * when we want to close the window, we should call Director::end();
* then call Director::mainLoop to do release of internal resources * then call Director::mainLoop to do release of internal resources
*/ */
if (glview->isOpenGLReady()) if (glview->isOpenGLReady())
{ {
director->end(); director->end();
@ -120,7 +156,7 @@ void Application::setResourceRootPath(const std::string& rootResDir)
{ {
_resourceRootPath += '/'; _resourceRootPath += '/';
} }
FileUtils* pFileUtils = FileUtils::getInstance(); FileUtils* pFileUtils = FileUtils::getInstance();
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths(); std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
searchPaths.insert(searchPaths.begin(), _resourceRootPath); searchPaths.insert(searchPaths.begin(), _resourceRootPath);
pFileUtils->setSearchPaths(searchPaths); pFileUtils->setSearchPaths(searchPaths);
@ -143,9 +179,7 @@ std::string Application::getVersion()
bool Application::openURL(std::string_view url) bool Application::openURL(std::string_view url)
{ {
EM_ASM_ARGS({ EM_ASM_ARGS({ window.open(UTF8ToString($0)); }, url.data());
window.open(UTF8ToString($0));
}, url.data());
return true; return true;
} }
@ -165,21 +199,26 @@ Application* Application::sharedApplication()
return Application::getInstance(); return Application::getInstance();
} }
const char * Application::getCurrentLanguageCode() const char* Application::getCurrentLanguageCode()
{ {
static char code[3]={0}; static char code[3] = {0};
char pLanguageName[16]; char pLanguageName[16];
EM_ASM_ARGS({ EM_ASM_ARGS(
var lang = localStorage.getItem('localization_language'); {
if (lang == null) { var lang = localStorage.getItem('localization_language');
stringToUTF8(window.navigator.language.replace(/-.*/, ""), $0, 16); if (lang == null)
} else { {
stringToUTF8(lang, $0, 16); stringToUTF8(window.navigator.language.replace(/ - .*/, ""), $0, 16);
} }
}, pLanguageName); else
strncpy(code,pLanguageName,2); {
code[2]='\0'; stringToUTF8(lang, $0, 16);
}
},
pLanguageName);
strncpy(code, pLanguageName, 2);
code[2] = '\0';
return code; return code;
} }
@ -187,19 +226,23 @@ LanguageType Application::getCurrentLanguage()
{ {
char pLanguageName[16]; char pLanguageName[16];
EM_ASM_ARGS({ EM_ASM_ARGS(
var lang = localStorage.getItem('localization_language'); {
if (lang == null) { var lang = localStorage.getItem('localization_language');
stringToUTF8(window.navigator.language.replace(/-.*/, ""), $0, 16); if (lang == null)
} else { {
stringToUTF8(lang, $0, 16); stringToUTF8(window.navigator.language.replace(/ - .*/, ""), $0, 16);
} }
}, pLanguageName); else
{
stringToUTF8(lang, $0, 16);
}
},
pLanguageName);
return utils::getLanguageTypeByISO2(pLanguageName); return utils::getLanguageTypeByISO2(pLanguageName);
} }
} } // namespace ax
#endif // AX_TARGET_PLATFORM == AX_PLATFORM_WASM
#endif // AX_TARGET_PLATFORM == AX_PLATFORM_WASM

View File

@ -78,22 +78,4 @@ DevToolsImpl* DevToolsImpl::getInstance()
} }
extern "C"
{
void axmol_director_pause()
{
ax::DevToolsImpl::getInstance()->pause();
}
void axmol_director_resume()
{
ax::DevToolsImpl::getInstance()->resume();
}
void axmol_director_step()
{
ax::DevToolsImpl::getInstance()->step();
}
}
#endif // AX_TARGET_PLATFORM == AX_PLATFORM_WASM #endif // AX_TARGET_PLATFORM == AX_PLATFORM_WASM

View File

@ -46,9 +46,9 @@
<input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked, <input type="button" value="Fullscreen" onclick="Module.requestFullscreen(document.getElementById('pointerLock').checked,
document.getElementById('resize').checked)"> document.getElementById('resize').checked)">
| |
<input type="button" value="Pause" onclick="Module.ccall('axmol_director_pause')"> <input type="button" value="Pause" onclick="Module.ccall('axmol_dev_pause')">
<input type="button" value="Resume" onclick="Module.ccall('axmol_director_resume')"> <input type="button" value="Resume" onclick="Module.ccall('axmol_dev_resume')">
<input type="button" value="Step" onclick="Module.ccall('axmol_director_step')"> <input type="button" value="Step" onclick="Module.ccall('axmol_dev_step')">
</div> </div>
<div class="emscripten"> <div class="emscripten">
@ -140,7 +140,10 @@
// As a default initial behavior, pop up an alert when webgl context is lost. To make your // As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping! // application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2 // See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false); canvas.addEventListener("webglcontextlost", function(e) { Module.ccall('axmol_wgl_context_lost'); alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
canvas.addEventListener("webglcontextrestored", function(e) { Module.ccall('axmol_wgl_context_restored'); e.preventDefault(); }, false);
document.addEventListener("visibilitychange", () => { Module.ccall('axmol_hdoc_visibilitychange', document.hidden); });
return canvas; return canvas;
})(), })(),

View File

@ -97,15 +97,14 @@ void AxmolRenderer::DeviceLost()
auto director = ax::Director::getInstance(); auto director = ax::Director::getInstance();
if (director->getGLView()) if (director->getGLView())
{ {
// TODO: backend::DriverBase::getInstance()->resetState();
// ax::GL::invalidateStateCache(); ax::Director::getInstance()->resetMatrixStack();
// ax::GLProgramCache::getInstance()->reloadDefaultGLPrograms();
// ax::DrawPrimitives::init();
// ax::VolatileTextureMgr::reloadAllTextures();
ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED); ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true); director->getEventDispatcher()->dispatchEvent(&recreatedEvent, true);
director->setGLDefaultValues(); director->setGLDefaultValues();
#if AX_ENABLE_CACHE_TEXTURE_DATA
ax::VolatileTextureMgr::reloadAllTextures();
#endif
Application::getInstance()->applicationWillEnterForeground(); Application::getInstance()->applicationWillEnterForeground();
ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND); ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);