最近在看《Real Time 3D Terrain Engines Using C++And DirectX 9》,不过是看网上翻译的版本叫《实时地形引擎》看英文实在蛋疼,还好有翻译版的,要多多感谢承天一大哥!这本书讲解了地形制作的一些技术,更吸引人的是实现了一个小型的渲染引擎,对于我这样的游戏引擎初学者很适合。gaia正是书中构建的引擎的名字。这里来写写一些简单的分析,作为学习笔记。

(一)资源管理

1、 data_pool.h

cPoolGroup:模板类,池组,顾名思义,就是装载一组数据的对象

template <class T>class cPoolGroup{public:

    cPoolGroup(uint16 maxCount);    ~cPoolGroup(){};

void        create();void        destroy();

    uint16        addMember(const T& member);    uint16        nextMember();void        release(uint16 index);

    uint16        totalOpen()const;    uint16        totalUsed()const;    uint16        firstOpen()const;bool        isOpen(uint16 index)const;    T&            member(uint16 index);const T&    member(uint16 index)const;    T*            memberPtr(uint16 index);const T*    memberPtr(uint16 index)const;

private:    uint16            m_totalOpen;    uint16            m_firstOpen;    uint16            m_maxCount;    uint16*            m_nextOpenList;    T*                m_memberList;//数据对象链表};

cDataPoolInterface:数据池接口,抽象类

class cDataPoolInterface{public:    typedef void (*MEMBER_CALLBACK)(cDataPoolInterface* pPool, cPoolHandle handle, void* member);

    cDataPoolInterface():m_initialized(false){};virtual ~cDataPoolInterface(){};

virtual void initialize(uint16 growSize)=0;;virtual void destroy()=0;;virtual void clear()=0;virtual cPoolHandle nextHandle()=0;virtual void release(cPoolHandle* pHandle)=0;virtual void forEach(MEMBER_CALLBACK function)=0;

virtual void* getGenericPtr(cPoolHandle index)=0;

bool isInitialized()const {return m_initialized;}

protected:bool m_initialized;};

cDataPool:模板类,继承自cDataPoolInterface,数据池,它包括了若干个cPoolGroup

template <class T>class cDataPool : public cDataPoolInterface{public:

// Data Types & Constants...    typedef T DATA_TYPE;    typedef cPoolGroup<T> MemberGroup;    typedef std::list<MemberGroup*> MemberGroupList;    typedef cDataPoolInterface::MEMBER_CALLBACK MEMBER_CALLBACK;

// Creators...    cDataPool();    ~cDataPool();

void initialize(uint16 growSize);void destroy();

const T& operator[](cPoolHandle handle)const {return get(handle);}    T& operator[](cPoolHandle handle) {return get(handle);}

// Mutators...    cPoolHandle add(const T& member);void clear();    cPoolHandle nextHandle();void release(cPoolHandle* pHandle);void forEach(MEMBER_CALLBACK function);

// Accessors...    bool isHandleValid(cPoolHandle index)const;const T& get(cPoolHandle index)const;    T* getPtr(cPoolHandle index);void* getGenericPtr(cPoolHandle index);

    uint16 totalMembers()const {return m_totalMembers;}    uint16 totalOpen()const {return m_totalOpen;}    uint16 totalUsed()const {return m_totalMembers-m_totalOpen;}

private:

// Private Data...    MemberGroupList m_groupList;// cPoolGroup链表    uint16            m_totalMembers;    uint16            m_totalOpen;    uint16            m_groupCount;    uint32            m_indexMask;int                m_indexShift;

// Private Functions...    explicit cDataPool(const cDataPool& Src);    cDataPool& operator=(const cDataPool& Src);

    cPoolGroup<T>*    addGroup();    cPoolGroup<T>*    findOpenGroup(unsigned* groupNumber);    cPoolGroup<T>*    getGroup(unsigned index);const cPoolGroup<T>*    getGroup(unsigned index)const;

int getGroupNumber(cPoolHandle handle)const;int getItemIndex(cPoolHandle handle)const;    cPoolHandle buildHandle(int group, int index)const;

};

2、 resource_pool.h

cResourcePoolInterface:资源池接口,抽象类

class cResourcePoolInterface{public:

    union FILE_EXTENSION    {char ext[4];        uint32 fourcc;    };

    typedef std::map<cString, cPoolHandle> NAME_LOOKUP_MAP;

    cResourcePoolInterface();virtual ~cResourcePoolInterface(){};

// these functions must be proviided in the derived class (cResourceType)    virtual void initialize(uint16 growSize)=0;virtual void destroy()=0;virtual bool isInitialized()const=0;

virtual void destroyAll()=0;virtual void disableAll()=0;virtual void restoreAll()=0;virtual void clean()=0;

    cResourcePoolItem* createResource(const cString& resourceName);    cResourcePoolItem* loadResource(const cString& filename);    cResourcePoolItem* getResource(cPoolHandle handle);    cResourcePoolItem* findResource(const cString& Name);void destroyResource(cResourcePoolItem* pResource);

bool saveResource(cResourcePoolItem* pResource);

    cPoolHandle findResourceHandle(const cString& Name);const cString* findResourceName(cPoolHandle handle)const;void setResourceName(cPoolHandle handle, const tchar* name);

void registerResourcePool(cResourceCode code);void unregisterResourcePool();

    cResourceCode registrationCode()const;

protected:    cResourceCode m_registrationCode;    NAME_LOOKUP_MAP m_nameMap;

private:// Private Functions...    virtual cPoolHandle internalCreateResource(const cString& resourceName)=0;virtual void internalDestroyResource(cPoolHandle handle)=0;virtual cResourcePoolItem* internalGetResource(cPoolHandle handle)=0;};

cResourcePool:模板类,资源池,继承自cResourcePoolInterface,成员中有一个DataPool可以看做是DataPool的扩充。

template <class T>class cResourcePool : public cResourcePoolInterface{public:

    typedef T DataType;    typedef cDataPool<T> DataPool;

// Creators...    cResourcePool(){};    ~cResourcePool(){};

// Base Class Overrides...    void initialize(uint16 growSize);void destroy();bool isInitialized()const;

void destroyAll();void disableAll();void restoreAll();void clean(); // delete items no longer referenced

    DataType* createResource(const cString& resourceName);    DataType* loadResource(const cString& filename);    DataType* getResource(cPoolHandle handle);    DataType* findResource(const cString& Name);

// static data pool callbacks    static void callbackDestroy(cDataPoolInterface* pPool, cPoolHandle handle, void* resource);static void callbackRestore(cDataPoolInterface* pPool, cPoolHandle handle, void* resource);static void callbackDisable(cDataPoolInterface* pPool, cPoolHandle handle, void* resource);static void callbackClean(cDataPoolInterface* pPool, cPoolHandle handle, void* resource);

private:

// Data...    DataPool        m_dataPool;

// Private Functions...    cPoolHandle internalCreateResource(const cString& resourceName);void internalDestroyResource(cPoolHandle handle);    cResourcePoolItem* internalGetResource(cPoolHandle handle);

// Nonexistant Functions...

    cResourcePool( const cResourcePool& Src);    cResourcePool& operator=( const cResourcePool& Src);};

cResourcePoolItem:资源池对象继承自cReferenceCounter

关于cReferenceCounter上一段英文:

/*    cResourcePoolItem

------------------------------------------------------------------------------------------

      

       A cResourcePoolItem is a simple base class for a shared resource such as a texture,

       animation or vertex buffer.

 

       Note that cResourcePoolItem is derived from cReferenceCounter. The cResourcePoolManager uses

       the value of the cReferenceCounter to decide when objects are no longer in use and can

       be destroyed during the cResourcePoolManager::clean() function. This means the application

       must update the reference count of the cResourcePoolItem whenever links between objects

       are created or removed between objects. The cReferenceCounter base class provides the

       functionality to manipulate the reference count.

      

------------------------------------------------------------------------------------------

*/

class cResourcePoolItem : public cReferenceCounter{public:

// Data Types & Constants...    enum    {        nCreated=0,    // the resource has been created        nLoaded,    // the resource is filled with data and ready for use        nDisabled,    // the resource is currently disabled for use        nAltered,    // the resource has been altered since loaded        nTotalResourceFlags    };

// Creators...    cResourcePoolItem();virtual ~cResourcePoolItem();

// User Functions// These function prototypes are to be used by resources that use volatile or system-specific resources.// Examples would be video-memory resident textures or hardware vertex buffers.    virtual bool createResource()=0;                    // innitialize the resource (called once)    virtual bool destroyResource()=0;                    // destroy the resource    virtual bool disableResource()=0;                    // purge the resource from volatile memory    virtual bool restoreResource()=0;                    // restore the resource to volatile memory    virtual bool loadResource(const tchar* filename=0)=0; // load the resource from a file (or NULL to use the resource name)    virtual bool saveResource(const tchar* filename=0)=0;    // save the resource to a file (or NULL to use the resource name)

// Accessors...    cResourceCode            resourceCode()const;    cResourcePoolInterface* resourcePool()const;    cPoolHandle                resourceHandle()const;    u32Flags                resourceFlags()const;

bool isResourceCreated()const;bool isResourceDisabled()const;bool isResourceLoaded()const;const cString* findResourceName()const;

void setResourceName(const tchar* name);void setAlteredFlag(bool onOff);bool alteredFlag()const;

// mimic COM interfaces    virtual int32 Release();

protected:

// only derrived classes are permitted to modify internal members    void setResourceCode(cResourceCode code);void setResourcePool(cResourcePoolInterface* interfaePtr);void setResourceHandle(cPoolHandle handle);void setResourceFlag(int flagBit, bool Setting);

void notifyCreated();void notifyDisabled();void notifyLoaded();void notifyUnloaded();void notifyRestored();void notifyDestroyed();void notifySaved();

private:

// Data...    cResourceCode            m_resourceCode;    cResourcePoolInterface* m_resourcePool;    cPoolHandle                m_resourceHandle;    u32Flags                m_resourceFlags;

// Private Functions...

// Nonexistant Functions...    cResourcePoolItem( const cResourcePoolItem& Src);    cResourcePoolItem& operator=( const cResourcePoolItem& Src);

    friend cResourcePoolInterface;};

cReferenceCounter:。。(我没太理解它的作用,望大虾指点。。)

/*    cReferenceCounter

-----------------------------------------------------------------

   

       A cReferenceCounter is a base class which allows the program

       to count open instances.

      

-----------------------------------------------------------------

*/

class cReferenceCounter{public:

// Construction and Destruction...    cReferenceCounter()    {        m_nReferenceCount = 0;    }

    cReferenceCounter( const cReferenceCounter& Src)    {// Reference counts are tied to a specific instance// i.e. they are never copied        m_nReferenceCount = 0;    }

virtual ~cReferenceCounter()    {// assert if this object is still in use        assert(m_nReferenceCount == 0);    }

// Operators...    cReferenceCounter& operator=(const cReferenceCounter& Src)    {// this function is provided in order to allow // derived classes to provide copy operators.// the reference count itself is never copied.        return *this;    }

// Mutators...    virtual int32 AddRef()     {        assert(m_nReferenceCount != MAX_INT32);        ++m_nReferenceCount;return(m_nReferenceCount);    };

virtual int32 Release()    {        assert(m_nReferenceCount > 0);        --m_nReferenceCount;return(m_nReferenceCount);    }

// Accessors...    int32 referenceCount()const {return m_nReferenceCount;}

private:

// the internal reference count, stored in 32bits     int32 m_nReferenceCount;};

cResourcePoolManager:单件类,资源管理器,负责申请资源池,找到资源等资源管理功能。

class cResourcePoolManager : public cSingleton<cResourcePoolManager>{public:

    typedef std::map<cResourceCode, cResourcePoolInterface*> ResourcePoolTypeMap;    typedef std::list<cResourcePoolInterface*> ResourcePoolFamilyList;

// Creators...    cResourcePoolManager();    ~cResourcePoolManager(){};

// registration of resource manager interfaces    void registerResourcePool(cResourceCode code, cResourcePoolInterface* pInterface);    cResourcePoolInterface*  unregisterResourcePool(cResourceCode code);

// operations for all resource types    void destroyAll();void disableAll();void restoreAll();void clean(); // delete items no longer referenced

// operations on specific resource types    void destroyResourceFamily(int family);void disableResourceFamily(int family);void restoreResourceFamily(int family);void cleanResourceFamily(int family); 

void destroyResourceType(cResourceCode code);void disableResourceType(cResourceCode code);void restoreResourceType(cResourceCode code);void cleanResourceType(cResourceCode code); 

    cResourcePoolInterface* findResourcePool(cResourceCode code)const;    cPoolHandle findResourceHandle(cResourceCode code, const cString& Name)const;    cResourcePoolItem* findResource(cResourceCode code, const cString& Name)const;    cResourcePoolItem* findResource(cResourceCode code, cPoolHandle handle)const;

private:

    ResourcePoolFamilyList m_resourceFamilyList[k_nTotalResourceFamilies];    ResourcePoolTypeMap    m_resourceTypeMap;};

引擎中所有的资源都派生自cResourcePoolItem,也可以说是对D3D资源类的封装。例如:texture.h里的cTexture:

class cTexture : public cResourcePoolItem{public:

// Data Types & Constants...    enum eTextureFlags    {        k_cubeMap=0,        k_dynamicTexture,        k_renderTarget,        k_paletized,        k_mipMaps,    };

enum eForcedFormatFlags    {        k_forceMipLevels=0,        k_forceFormat,        k_forceSize,    };

// Creators...

    cTexture();    ~cTexture();

// base resource overrides    bool createResource();                    // innitialize the resource (called once)    bool destroyResource();                    // destroy the resource    bool disableResource();                    // purge the resource from volatile memory    bool restoreResource();                    // prepare the resource for use (create any volatile memory objects needed)    bool loadResource(const tchar* filename=0); // load the resource from a file (or NULL to use the resource name)    bool saveResource(const tchar* filename=0);    // save the resource to a file (or NULL to use the resource name)

bool createTexture(uint32 width, uint32 height, uint32 mipLevels, uint32 usage, D3DFORMAT Format, D3DPOOL pool);bool createCubeTexture(uint32 size, uint32 mipLevels, uint32 usage, D3DFORMAT Format, D3DPOOL pool);void generateMidpointNoise(float falloff);void generatePerlinNoise(float scale, int octaves, float falloff);bool generateNormalMap(LPDIRECT3DTEXTURE9 heightMap, uint32 channel, uint32 flags, float amplitude);bool generateNormalizationCubeMap();bool maskWithImage(cImage* pImage);

void releaseTexture();

bool convertToNormalMap(        uint32 channel,         uint32 flags, float amplitude);

// Accessors...    LPDIRECT3DTEXTURE9        getTexture()const;bool uploadImage(const cImage* pImage, bool copyAll = true);bool uploadCubeFace(const cImage* pImage, D3DCUBEMAP_FACES face, bool copyAll = true);

    uint32        width()const{return m_width;}    uint32        height()const{return m_height;}    uint32        mipLevels()const{return m_mipLevels;}    uint32        d3dUsage()const{return m_d3dUsage;}    D3DFORMAT    d3dFormat()const{return m_d3dFormat;}    D3DPOOL        d3dPool()const{return m_d3dPool;}

bool    getSurfaceLevel(int level, LPDIRECT3DSURFACE9* ppSurface);private:

// Data...    uint32                    m_width;    uint32                    m_height;    uint32                    m_mipLevels;    uint32                    m_d3dUsage;    D3DFORMAT                m_d3dFormat;    D3DPOOL                    m_d3dPool;    DWORD                    m_d3dFilter;    DWORD                    m_d3dMipFilter;    D3DCOLOR                m_d3dColorKey;    D3DXIMAGE_INFO            m_d3dImageInfo;    PALETTEENTRY*            m_pPalette;    LPDIRECT3DTEXTURE9        m_pTexture;    LPDIRECT3DCUBETEXTURE9    m_pCubeTexture;

bool loadFromResourceFile(cFile& InputFile);bool configureImageSettings();bool checkTextureRequirements();bool checkCubeTextureRequirements();

bool loadTextureFromImageFile(const tchar* filename);bool loadTextureFromMemory(uint8* memory, uint32 size);bool loadCubeTextureFromImageFile(const tchar* filename);bool loadCubeTextureFromMemory(uint8* memory, uint32 size);

// Nonexistant Functions...    cTexture( const cTexture& Src);    cTexture& operator=( const cTexture& Src);

};

typedef cResourcePool<cTexture> cTextureManager;

此外还有vertex_buffer.h中的cVertexBuffermodel_resource.h中的cModelResource

P56有资源的使用示例

类关系图:

cResourcePoolManager统一管理。

书的第四章gaia引擎总览有提到资源管理相关的内容,但是并没用对源码进行过多的解释,这部分还是要自己硬着头皮看啊。。。

不过看了这些基本理解了资源管理的原理和整个模块的结构,受益匪浅。。

UML:

ps:突然发现一个奇怪的问题,gaia中虽然定义了cResourcePoolManager,但是自己的程序里面并没有使用,而是在displayManager里面直接有几个cResourcePool对资源进行管理。。

如下:

class cDisplayManager {。。。。。。protected:

// Private Data...    bool    m_bEnabled;bool    m_bClearEachFrame; // true to clear each frame to black before rendering

// device-bound resource pools    cTextureManager            m_texturePool;    cSurfaceMaterialManager m_surfaceMaterialPool;    cEffectFileManager        m_effectFilePool;    cRenderMethodManager    m_renderMethodPool;    cVertexBufferManager    m_vertexBufferPool;    cIndexBufferManager        m_indexBufferPool;    cModelManager            m_modelPool;    cImageManager            m_imagePool;。。。。。。};


转载于:https://www.cnblogs.com/wonderKK/archive/2012/02/15/2353079.html

gaia引擎分析(一)资源管理相关推荐

  1. gaia引擎分析(二)场景管理

    只是粗略的分析原理,大虾轻喷~~ Gaia引擎中没有场景管理器(scenemanager)这种东西,但是并不是没有场景管理,而是在cGameHost类中有一课场景树进行场景组织.一棵四叉树用来进行剪裁 ...

  2. ClickHouse系列教程三:MergeTree引擎分析

    ClickHouse系列教程: ClickHouse系列教程 Clickhouse之MergeTree引擎分析 CRUD Clickhouse支持查询(select)和增加(insert),但是不直接 ...

  3. activeMQ 的kahadb存储引擎分析

    activeMQ 的kahadb存储引擎分析 很久没更新blog了,前几天看到淘宝开源了Meta,估计notify也要开源了.其实消息中间件的一个非常重要的核心部件就是持久化存储,可能Meta的功能定 ...

  4. Java三大主流开源工作流引擎分析

    Java三大主流开源工作流引擎分析 首先,这个评论是我从网上,书中,搜索和整理出来的,也许有技术点上的错误点,也许理解没那么深入.但是我是秉着学习的态度加以评论,学习,希望对大家有用,进入正题! 三大 ...

  5. 腾讯手Q webview技术owner厉心刚:JavaScript引擎分析

    声明:本文来自腾讯增值产品部官方公众号小时光茶社,为CSDN原创投稿,未经许可,禁止任何形式的转载. 作者:厉心刚,SNG 增值产品部终端开发,高级工程师:参与多个项目开发(QQ情侣,手Q,阅读,电竞 ...

  6. 京东云引擎使用教程 资源管理器部署方法 新手可以来看看

    京东云引擎使用教程 资源管理器部署方法 新手可以来看看 大家好.我是 Norry,本来今天想转发一段教程给大家 (京东云空间合理使用管理方案). 但是源头限制了图片转发,也不能下载,所以只有把链接贴出 ...

  7. 渲染引擎分析 - 鸿蒙(OpenHarmony) JS UI 源码阅读笔记

    作者:门柳 鸿蒙是华为研发的新一代终端操作系统,能适用于 IoT.手表.手机.Pad.电视等各种类型的设备上,扛起"国产操作系统"的大旗,也遭受了很多非议.2021 年 6 月初发 ...

  8. MySQL · 引擎分析 · InnoDB行锁分析

    前言 理解InnoDB行锁,分析一条SQL语句会加什么样的行锁,会锁住哪些数据范围对业务SQL设计和分析线上死锁问题都会有很大帮助.对于InnoDB的行锁,已经有多篇月报进行了介绍,这里笔者借鉴前面月 ...

  9. 深入MySQL存储引擎分析锁和排序的原理

    几个问题 为什么不建议使用订单号作为主键? 为什么要在需要排序的字段上加索引? for update 的记录不存在会导致锁住全表? redolog 和 binlog 有什么区别? MySQL 如何回滚 ...

最新文章

  1. Nacos 1.3.0 发布,一个修炼内功的版本:全新内核构建!
  2. win10 ping不通解决方案
  3. msfconsole 无法启动,解决办法
  4. phpcmsV9单网页调用其他栏目文章 -方法总结
  5. DataFountain新上计算机视觉比赛-20万巨奖数钢筋
  6. 破解class文件的第一步:深入理解JAVA Class文件
  7. c语言学习-计算200-300之间的偶数和并将和打印输出
  8. F1-VmwareCentOS7.x
  9. mysql——event定时任务
  10. android开发学习——day3
  11. 点云配准(一)— ICP方法
  12. 第二单元 用python学习微积分(九)近似计算的应用和曲线构图
  13. 三、FreeNas实现SMB共享、FTP搭建实验报告
  14. CVE-2019-0708高危漏洞,各家安全厂商的扫描修复方案
  15. table 表格边框线去重
  16. 2021年安全员-C证(陕西省)考试资料及安全员-C证(陕西省)新版试题
  17. 计算机视觉检测 白皓月,基于深度神经网络的视线跟踪技术研究
  18. dwm.exe_什么是桌面窗口管理器(dwm.exe),为什么运行?
  19. 文字绕圆排列:vue
  20. 快速排序--QuickSort()--递归版本

热门文章

  1. mysqlslap 压力测试工具
  2. Java杂记3—流程控制之条件 1
  3. Android RecyclerView初体验
  4. python写入中文到文件乱码的问题
  5. java对象转json格式
  6. C# 关于yield return的研究(转载)
  7. ADO.NET_05_OracleDataReader
  8. php速度为什么快,为什么 === 的速度比 == 快?
  9. win10删除开机密码_讲解win10忘记开机密码
  10. TokenInsight:反映区块链行业整体表现的TI指数较昨日同期上涨1.37%