I get really lost on how I could achieve this, and got into stuff like variadic templates and inplace factories, but I couldnt manage to do with these..so my current one ended needing 3 classes:
the resourceCache, who stores the resources, fully templated, no overloads or specialization;
the resourceDescriptor, a templated class completely empty, i need to provide a specialization for each resource, the point is make the cache class above not need overloads or specialization..
the resourceCreator, a class with a bunch of static Create overloaded functions, witch takes descriptors as params...
Resuming, I remove some burden of the cache class by creating 2 more classes.
The problem was that the resources (dx ID3D11... resources/states/layouts) need a different call on ID3D11Device, with different params, and that screw me from the very beginning..
I really dont know if I did a big shit here (i just finished it), like, maybe was better giving up the template and create a cache class for each resource?
Heres the code, dont mind the simpleness, I wanted it to be really straightforward:
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
/*
created: 2012/07/15
created: 15:7:2012 23:09
filename: DXResourceCache.h
file base: DXResourceCache
file ext: h
author: Giuliano Suminsky (a.k.a. Icebone1000)
purpose: Creating and storing dx resources made centralized.
© Giuliano Suminsky (a.k.a. Icebone1000) , rights reserved.
*/
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
#pragma once
#pragma comment( lib, "D3D11.lib")
#pragma comment( lib, "D3DX11.lib")
// dx includes
#include <D3Dcommon.h>
#include <D3D11.h>
#include <D3DX11core.h> //including because of D3DERR_INVALIDCALL
// stl includes
#include <exception>
// my includes
#include "../../../myMACROS.h"
// Note that dx SDK doesnt consider states and layouts as resources...just a note ;D
namespace dx{
template< class T >
struct DX_Resource_Descriptor{
};
template<>
struct DX_Resource_Descriptor<ID3D11InputLayout>{
D3D11_INPUT_ELEMENT_DESC * inputElementsDesc_p;
UINT nElements;
void* pShaderSig_p;
SIZE_T iSizeShaderSig_p;
};
//TODO:
//specialization for each resource
//------------------------------------------------------------------------
// A fixed size resource cache class.
//------------------------------------------------------------------------
template< class T, int fixedSize >
class DX_Resource_Cache{
private:
ID3D11Device * m_pDeviceRef; // the one that actually can create stuff
UINT m_iCurrentIndex; // next slot to be allocated, also the ID of the resources
//UINT m_nFreedSlots; // how many freed slot there are?
T* m_cache[fixedSize]; // the container per se
public:
//------------------------------------------------------------------------
// Receives a descriptor for the current template type.
//------------------------------------------------------------------------
UINT CreateResource( T *& pResource_p, DX_Resource_Descriptor<T> resourceDesc_p )
{
if( m_iCurrentIndex > fixedSize)
{
throw std::exception("resource cache limit achieved");
}
pResource_p = m_cache[m_iCurrentIndex] = DX_ResourceCreator.Create(m_pDeviceRef, resourceDesc_p);
++m_iCurrentIndex; //points to next element
return (m_iCurrentIndex-1);
}
//------------------------------------------------------------------------
public:
//------------------------------------------------------------------------
// Main function to return stored resources.
//------------------------------------------------------------------------
T* Acquire( const UINT id_p ) const
{
if( id_p > fixedSize )
{
throw std::exception("cache overflow, wtf is that id?");
}
//assert(m_slotsInfo[id_p].useCount != 0);
//++m_slotsInfo[id_p].useCount;
//m_slotsInfo[id_p].desc.Create(m_pDeviceRef);
return m_cache[id_p];
}
//------------------------------------------------------------------------
public:
//------------------------------------------------------------------------
// Releases all non null pointers in the cache.
//------------------------------------------------------------------------
virtual ~DX_Resource_Cache()
{
for( UINT it = 0; it < fixedSize; it++ )
{
donne( m_cache[it]);
}
}
//------------------------------------------------------------------------
};
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// Simple class to provide overloads for creating resources with the same call.
//------------------------------------------------------------------------
class DX_ResourceCreator{
static ID3D11InputLayout* Create( ID3D11Device *pDeviceRef_p, DX_Resource_Descriptor<ID3D11InputLayout> resourceDesc_p ){
ID3D11InputLayout *pInputLayout;
pDeviceRef_p->CreateInputLayout( resourceDesc_p.inputElementsDesc_p, resourceDesc_p.nElements, resourceDesc_p.pShaderSig_p, resourceDesc_p.iSizeShaderSig_p, &pInputLayout);
//TODO:
//error handling
return pInputLayout;
}
//TODO:
//overloads for each resource here
};
//------------------------------------------------------------------------
}
Edited by Icebone1000, 20 July 2012 - 06:19 AM.







