2018-08-06 21:34:16 -07:00
|
|
|
//
|
|
|
|
|
// KRContext.h
|
|
|
|
|
// KREngine
|
|
|
|
|
//
|
|
|
|
|
// Created by Kearwood Gilbert on 12-04-12.
|
|
|
|
|
// Copyright (c) 2012 Kearwood Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef KREngine_KRContext_h
|
|
|
|
|
#define KREngine_KRContext_h
|
|
|
|
|
|
|
|
|
|
#include "KREngine-common.h"
|
|
|
|
|
#include "KRBundleManager.h"
|
|
|
|
|
#include "KRSceneManager.h"
|
|
|
|
|
#include "KRTextureManager.h"
|
|
|
|
|
#include "KRMaterialManager.h"
|
2019-12-01 15:49:49 -08:00
|
|
|
#include "KRPipelineManager.h"
|
2018-08-06 21:34:16 -07:00
|
|
|
#include "KRMeshManager.h"
|
|
|
|
|
#include "KRAnimationManager.h"
|
|
|
|
|
#include "KRAnimationCurveManager.h"
|
|
|
|
|
#include "KRUnknownManager.h"
|
2019-12-01 16:20:55 -08:00
|
|
|
#include "KRShaderManager.h"
|
2019-08-18 17:57:41 -07:00
|
|
|
#include "KRSourceManager.h"
|
2018-08-06 21:34:16 -07:00
|
|
|
#include "KRStreamer.h"
|
|
|
|
|
|
|
|
|
|
class KRAudioManager;
|
|
|
|
|
|
|
|
|
|
class KRContext {
|
|
|
|
|
public:
|
2019-12-01 15:49:49 -08:00
|
|
|
static int KRENGINE_MAX_PIPELINE_HANDLES;
|
2018-08-06 21:34:16 -07:00
|
|
|
static int KRENGINE_GPU_MEM_MAX;
|
|
|
|
|
static int KRENGINE_GPU_MEM_TARGET;
|
|
|
|
|
static int KRENGINE_MAX_TEXTURE_DIM;
|
|
|
|
|
static int KRENGINE_MIN_TEXTURE_DIM;
|
|
|
|
|
static int KRENGINE_PRESTREAM_DISTANCE;
|
|
|
|
|
static int KRENGINE_SYS_ALLOCATION_GRANULARITY;
|
|
|
|
|
static int KRENGINE_SYS_PAGE_SIZE;
|
|
|
|
|
|
|
|
|
|
|
2019-07-28 16:46:46 -07:00
|
|
|
KRContext(const KrInitializeInfo* initializeInfo);
|
2018-08-06 21:34:16 -07:00
|
|
|
~KRContext();
|
2019-07-28 17:14:33 -07:00
|
|
|
|
2020-07-22 19:38:01 -07:00
|
|
|
KrResult createWindowSurface(const KrCreateWindowSurfaceInfo* createWindowSurfaceInfo);
|
|
|
|
|
KrResult deleteWindowSurface(const KrDeleteWindowSurfaceInfo* deleteWindowSurfaceInfo);
|
|
|
|
|
|
2019-08-03 18:53:05 -07:00
|
|
|
KrResult createBundle(const KrCreateBundleInfo* createBundleInfo);
|
2019-08-18 17:57:41 -07:00
|
|
|
KrResult moveToBundle(const KrMoveToBundleInfo* moveToBundleInfo);
|
|
|
|
|
KrResult loadResource(const KrLoadResourceInfo* loadResourceInfo);
|
2019-07-28 17:14:33 -07:00
|
|
|
KrResult unloadResource(const KrUnloadResourceInfo* unloadResourceInfo);
|
2020-08-09 20:34:21 -07:00
|
|
|
KrResult getResourceData(const KrGetResourceDataInfo* getResourceDataInfo, KrGetResourceDataCallback callback);
|
2019-12-08 16:29:49 -08:00
|
|
|
KrResult mapResource(const KrMapResourceInfo* mapResourceInfo);
|
2020-07-13 18:54:15 -07:00
|
|
|
KrResult unmapResource(const KrUnmapResourceInfo* unmapResourceInfo);
|
2019-08-03 18:53:05 -07:00
|
|
|
KrResult saveResource(const KrSaveResourceInfo* saveResourceInfo);
|
2019-07-28 17:14:33 -07:00
|
|
|
|
2020-08-06 18:15:58 -07:00
|
|
|
KrResult compileAllShaders(const KrCompileAllShadersInfo* pCompileAllShadersInfo);
|
|
|
|
|
|
2020-06-11 21:41:17 -07:00
|
|
|
KrResult createScene(const KrCreateSceneInfo* createSceneInfo);
|
2020-07-13 16:45:35 -07:00
|
|
|
KrResult findNodeByName(const KrFindNodeByNameInfo* pFindNodeByNameInfo);
|
|
|
|
|
KrResult findAdjacentNodes(const KrFindAdjacentNodesInfo* pFindAdjacentNodesInfo);
|
|
|
|
|
KrResult setNodeLocalTransform(const KrSetNodeLocalTransformInfo* pSetNodeLocalTransform);
|
|
|
|
|
KrResult setNodeWorldTransform(const KrSetNodeWorldTransformInfo* pSetNodeWorldTransform);
|
|
|
|
|
KrResult deleteNode(const KrDeleteNodeInfo* pDeleteNodeInfo);
|
|
|
|
|
KrResult deleteNodeChildren(const KrDeleteNodeChildrenInfo* pDeleteNodeChildrenInfo);
|
|
|
|
|
KrResult appendBeforeNode(const KrAppendBeforeNodeInfo* pAppendBeforeNodeInfo);
|
|
|
|
|
KrResult appendAfterNode(const KrAppendAfterNodeInfo* pAppendAfterNodeInfo);
|
|
|
|
|
KrResult appendFirstChildNode(const KrAppendFirstChildNodeInfo* pAppendFirstChildNodeInfo);
|
|
|
|
|
KrResult appendLastChildNode(const KrAppendLastChildNodeInfo* pAppendLastChildNodeInfo);
|
|
|
|
|
KrResult updateNode(const KrUpdateNodeInfo* pUpdateNodeInfo);
|
|
|
|
|
|
2020-06-11 21:41:17 -07:00
|
|
|
|
2019-08-18 17:57:41 -07:00
|
|
|
KRResource* loadResource(const std::string &file_name, KRDataBlock *data);
|
|
|
|
|
|
2018-08-06 21:34:16 -07:00
|
|
|
|
|
|
|
|
KRBundleManager *getBundleManager();
|
|
|
|
|
KRSceneManager *getSceneManager();
|
|
|
|
|
KRTextureManager *getTextureManager();
|
|
|
|
|
KRMaterialManager *getMaterialManager();
|
2019-12-01 15:49:49 -08:00
|
|
|
KRPipelineManager *getPipelineManager();
|
2018-08-06 21:34:16 -07:00
|
|
|
KRMeshManager *getMeshManager();
|
|
|
|
|
KRAnimationManager *getAnimationManager();
|
|
|
|
|
KRAnimationCurveManager *getAnimationCurveManager();
|
|
|
|
|
KRAudioManager *getAudioManager();
|
|
|
|
|
KRUnknownManager *getUnknownManager();
|
2019-12-01 16:20:55 -08:00
|
|
|
KRShaderManager *getShaderManager();
|
2019-08-18 17:57:41 -07:00
|
|
|
KRSourceManager *getSourceManager();
|
2018-08-06 21:34:16 -07:00
|
|
|
|
|
|
|
|
KRCamera *createCamera(int width, int height);
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
KRENGINE_GL_EXT_texture_storage,
|
|
|
|
|
KRENGINE_NUM_EXTENSIONS
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const char * extension_names[KRENGINE_NUM_EXTENSIONS];
|
|
|
|
|
static bool extension_available[KRENGINE_NUM_EXTENSIONS];
|
|
|
|
|
|
|
|
|
|
void startFrame(float deltaTime);
|
|
|
|
|
void endFrame(float deltaTime);
|
|
|
|
|
|
|
|
|
|
long getCurrentFrame() const;
|
|
|
|
|
long getLastFullyStreamedFrame() const;
|
|
|
|
|
float getAbsoluteTime() const;
|
|
|
|
|
|
|
|
|
|
long getAbsoluteTimeMilliseconds();
|
|
|
|
|
|
|
|
|
|
std::vector<KRResource *> getResources();
|
|
|
|
|
bool getStreamingEnabled();
|
|
|
|
|
void setStreamingEnabled(bool enable);
|
|
|
|
|
|
|
|
|
|
#if TARGET_OS_IPHONE || TARGET_OS_MAC
|
|
|
|
|
// XXX This doesn't belong here, and might not actually be needed at all
|
|
|
|
|
void getMemoryStats(long &free_memory);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
LOG_LEVEL_INFORMATION,
|
|
|
|
|
LOG_LEVEL_WARNING,
|
|
|
|
|
LOG_LEVEL_ERROR
|
|
|
|
|
} log_level;
|
|
|
|
|
|
|
|
|
|
typedef void log_callback(void *userdata, const std::string &message, log_level level);
|
|
|
|
|
|
|
|
|
|
static void SetLogCallback(log_callback *log_callback, void *user_data);
|
|
|
|
|
static void Log(log_level level, const std::string message_format, ...);
|
|
|
|
|
|
|
|
|
|
void doStreaming();
|
|
|
|
|
void receivedMemoryWarning();
|
|
|
|
|
|
|
|
|
|
static void activateStreamerContext();
|
|
|
|
|
static void activateRenderContext();
|
2021-08-10 20:13:32 -07:00
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
|
KrSurfaceHandle surfaceHandle;
|
|
|
|
|
VkSurfaceKHR surface;
|
|
|
|
|
VkPhysicalDevice device;
|
|
|
|
|
VkDevice logicalDevice;
|
|
|
|
|
VkPhysicalDeviceProperties deviceProperties;
|
|
|
|
|
VkPhysicalDeviceFeatures deviceFeatures;
|
|
|
|
|
VkQueue graphicsQueue;
|
|
|
|
|
VkQueue presentQueue;
|
|
|
|
|
VkSwapchainKHR swapChain;
|
|
|
|
|
std::vector<VkImage> swapChainImages;
|
|
|
|
|
VkFormat swapChainImageFormat;
|
|
|
|
|
VkExtent2D swapChainExtent;
|
|
|
|
|
std::vector<VkImageView> swapChainImageViews;
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
HWND hWnd;
|
|
|
|
|
#endif
|
|
|
|
|
} SurfaceInfo;
|
|
|
|
|
|
|
|
|
|
SurfaceInfo& GetSurfaceInfo(size_t index);
|
|
|
|
|
size_t GetSurfaceCount() const;
|
2018-08-06 21:34:16 -07:00
|
|
|
|
|
|
|
|
#if TARGET_OS_MAC
|
|
|
|
|
static void attachToView(void *view);
|
|
|
|
|
#endif
|
2020-07-22 17:11:27 -07:00
|
|
|
void addResource(KRResource* resource, const std::string& name);
|
|
|
|
|
void removeResource(KRResource* resource);
|
2018-08-06 21:34:16 -07:00
|
|
|
private:
|
|
|
|
|
KRBundleManager *m_pBundleManager;
|
|
|
|
|
KRSceneManager *m_pSceneManager;
|
|
|
|
|
KRTextureManager *m_pTextureManager;
|
|
|
|
|
KRMaterialManager *m_pMaterialManager;
|
2019-12-01 15:49:49 -08:00
|
|
|
KRPipelineManager *m_pPipelineManager;
|
2018-08-06 21:34:16 -07:00
|
|
|
KRMeshManager *m_pMeshManager;
|
|
|
|
|
KRAnimationManager *m_pAnimationManager;
|
|
|
|
|
KRAnimationCurveManager *m_pAnimationCurveManager;
|
|
|
|
|
KRAudioManager *m_pSoundManager;
|
|
|
|
|
KRUnknownManager *m_pUnknownManager;
|
2019-12-01 16:20:55 -08:00
|
|
|
KRShaderManager *m_pShaderManager;
|
2019-08-18 17:57:41 -07:00
|
|
|
KRSourceManager *m_pSourceManager;
|
2019-07-28 16:46:46 -07:00
|
|
|
|
|
|
|
|
KRResource** m_resourceMap;
|
|
|
|
|
size_t m_resourceMapSize;
|
2018-08-06 21:34:16 -07:00
|
|
|
|
|
|
|
|
void detectExtensions();
|
|
|
|
|
bool m_bDetectedExtensions;
|
|
|
|
|
|
|
|
|
|
long m_current_frame; // TODO - Does this need to be atomic?
|
|
|
|
|
long m_last_memory_warning_frame; // TODO - Does this need to be atomic?
|
|
|
|
|
long m_last_fully_streamed_frame; // TODO - Does this need to be atomic?
|
|
|
|
|
float m_absolute_time;
|
|
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
mach_timebase_info_data_t m_timebase_info;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
std::atomic<bool> m_streamingEnabled;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static log_callback *s_log_callback;
|
|
|
|
|
static void *s_log_callback_user_data;
|
|
|
|
|
|
|
|
|
|
KRStreamer m_streamer;
|
|
|
|
|
VkInstance m_vulkanInstance;
|
|
|
|
|
|
|
|
|
|
void createDeviceContexts();
|
|
|
|
|
void destroyDeviceContexts();
|
2020-07-23 17:03:40 -07:00
|
|
|
void destroySurfaces();
|
2020-07-22 17:11:27 -07:00
|
|
|
|
|
|
|
|
unordered_multimap<std::string, KRResource*> m_resources;
|
2021-08-10 20:13:32 -07:00
|
|
|
|
2020-07-22 19:38:01 -07:00
|
|
|
unordered_map<KrSurfaceHandle, SurfaceInfo> m_surfaces;
|
2021-08-10 17:57:13 -07:00
|
|
|
std::thread m_presentationThread;
|
|
|
|
|
void presentationThreadFunc();
|
|
|
|
|
std::atomic<bool> m_stop;
|
|
|
|
|
void renderFrame();
|
2018-08-06 21:34:16 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|