• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Icebone1000

Advice/Hate that templated resource cache class of mine...

5 posts in this topic

So, after 2 years trying to make this class...kidding, but it really hurt my mind, first time trying to make good use of templates (instead of "hey, look, Im using templates ;D")..

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:

[CODE]
//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
};
//------------------------------------------------------------------------
}
[/CODE] Edited by Icebone1000
0

Share this post


Link to post
Share on other sites
DirectX is not a good canidate for Templates. Unless its something mathmatical like vectors or matrix addition.
My question is: what is your definition of resource? Typically a resource is something that you could load or unload like a texure or sound.
I been looking at your code for a while and I got confused. So I started tinkering with your code and dropped Templates all together.
[source lang="cpp"]// Note that dx SDK doesnt consider states and layouts as resources...just a note ;D
// I agree that states and layouts are not really resources ;)

namespace dx
{

struct DX_SHADER_ITEM
{
unsigned int ID; //ID for Shader
D3D11_INPUT_ELEMENT_DESC* pInputDesc; //Decription of Shader Inputs
SIZE_T nInput; //Number of Inputs
void* pByteCode; //Compiled Shader Using Input Signature
SIZE_T nByteCodeSize; //Size of Shader
ID3D10InputLayout* pInputLayout //DX Layout Interface
};


class DX_ShaderManager
{
public:
DX_ShaderManager( ID3D11Device* pDevice );
~DX_ShaderManager(void);

unsigned int Create(D3D11_INPUT_ELEMENT_DESC* pInputDesc, SIZE_T nInput, void* pByteCode, SIZE_T nByteCodeSize )
{
DX_SHADER_ITEM* pShaderItem = new(DX_SHADER);

pDevice->CreateInputLayout( pShaderItem.pInputDesc,
pShaderItem.nInput,
pShaderItem.pByteCode,
pShaderItem.nByteCodeSize,
&pShaderItem.pInputLayout);
//TODO:
//error handling

//Create Storage Mechanism for DX_SHADER_ITEM

return pShaderItem->ID; //If passed error handling
}

bool SetShader(unsigned int ID)
{
// Mechanism to get DX_SHADER_ITEM
pShaderItem = getID(ID);

void IASetInputLayout(pShaderItem.pInputLayout);
}

DX_SHADER_ITEM* getID(unsigned int ID)
{
return DX_SHADER_ITEM*;
}

private:
m_pDevice;
};

}//End namespace dx[/source]

IDK about managing shaders like this.
0

Share this post


Link to post
Share on other sites
Thanks for the inputs..I really am just lost on how to do it, I felt I need a centralized way to create and reuse the stuff.
If each class creates its own stuff, at some time they will start create stuff that was already created..

But now I also dont know how would those classes make good use of the resource caches. I was thinking if I should make the classes store the ID for the resources on the cache, or make it transparent asking for the resource with a descriptor and then comparing descriptors traversing the cache..

Before trying that more elaborated thing I had a sprite class that did everything, but each time I needed a different thing I had to create another sprite object witch different parameters, and it would recreate all main stuff again, even recompile the shaders, it worked, but it sucked..

I always try to make stuff simple, but I dont think its possible anymore
0

Share this post


Link to post
Share on other sites
I'm not 100% sure what you're looking for. Do you mean that if you create a resource that uses the same descriptor as an existing resource then you want to return a reference to the existing resource rather than create a new one?

If so then I might suggest using a std::map of the resources with a hash of the descriptor as the key. You'll probably want to use some kind of reference-counted wrapper for your resources to make it easier to track when the resources are no longer used anywhere.
2

Share this post


Link to post
Share on other sites
"Some kind of reference counted wrapper" could be a shared_ptr with a custom deleter.
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0