• 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
melbow

Advanced Render Queue API

31 posts in this topic

Yeah you could do this with a single level of inheritance. I do use two levels like you demonstrated, IncidentRay, but State (and DrawCall) don't have any other members.
I only have this second layer of inheritance so that elsewhere in the API I can say that StateGroups contain States (which are just Commands) and SubMeshes/RenderItems contain DrawCalls (which are also just Commands). It just ensures that the user can't put a DrawCall-derived command in a StateGroup, nor use a State-derived command to represent geometry in a SubMesh, without generating a compile-time error.

 

 

Moving the errors to compile time is definitely a good thing.  So then the RenderItem struct can have a pointer to a DrawCall rather than just a pointer to a Command. I'm not quite sure how it would work for the state-group, however.  I was thinking of having a StateGroup struct which actually just stores the header -- i.e. the number of states, the size in bytes, etc.  Then the actual commands would be allocated contiguously after the header.  For example,

 

struct StateGroup {
    u8 numStates;
    u32 sizeInBytes;
};

void Foo(StateGroup* group)
{
    State* states = (State*)((u8*)group + sizeof(StateGroup));
}

 

I can't see a way to stop the user from putting DrawCalls in the state group.  Do you implement StateGroups differently, or is there something I'm missing?

 

 

If two different template functions happen to produce the same assembly code (e.g. vector and vector are likely exactly the same at the asm level) then your compile times will suffer, as the compiler will generate the same code multiple times, but then a modern linker will remove/merge the duplicates.
On old compilers (nothing you'll be using these days), the linker wouldn't perform this step, so if you had many different vector (with different T's) you'd end up with a ton of identical functions in your final executable... This is one reason why game developers didn't used to like using templates or the STL.

 

Yeah, I forgot that linkers can do that; thanks for the reminder.  TiagoCosta's use of templates is probably completely fine then.

 

EDIT: This editor seems to keep messing up the angle brackets for templates, so I can't get the quote above right.

Edited by IncidentRay
0

Share this post


Link to post
Share on other sites
If you're going for memory efficiency, be aware of your compiler's padding behaviour when designing your header structures -- e.g. your numStates var may as well be a u32 on my compiler.
If you support having external tools that generate these kinds of data-structures (e.g. I put state-groups directly in my engine's model, material and technique files so that I don't have to allocate extra memory when parsing those files on-load), then you've also got to be aware that those tools are dependent on your compiler's structure layout, or you've got to use compiler-specific extensions to force it to use the structure layout that you want.
struct StateGroup {
    u8 numStates;
    u8 _padding_[3];//!
    u32 sizeInBytes;
};
struct StateGroupB {
    u16 numStates;
    u16 sizeInBytes;//64KiB of state data should be enough for anyone ;)
    u32 _paddding_;//n.b. your compiler might do this to align the u64 below!
    u64 stateMask;//this isn't required, but I chose to add it
};
#pragma pack(push)//compiler-specific code to ensure no padding...
#pragma pack(1)//you're now responsible for understanding the alignment requirements of your target CPU...
struct StateGroupC {
    u64 stateMask;
    u16 numStates;
    u16 sizeInBytes;
};
#pragma pack(pop)

I do pretty much the same thing that you've thought of here, except I also put a bitfield in the header, with one bit for each type of state.
For an example of where this may be be useful -- in my loop that processes state-groups (posted earlier), I allow an item to contain 'layers' of state-groups, where state-values in earlier/higher groups take precedence over the values in later/lower groups. The bitfield lets me quickly check if a state-group contains any states that I'm interested in looking at, or whether it only contains states that I've already got values for and can be skipped entirely.
I can't see a way to stop the user from putting DrawCalls in the state group.  Do you implement StateGroups differently, or is there something I'm missing?
It depends on how you generate your StateGroups. I use the same allocation scheme -- a header followed by states -- and use a stack allocator to achieve this.
As a simple example of how you could enforce compile-time checking while creating a state-group:
class StateGroupWriter
{
  StackAlloc& a;
  StateGroupHeader* header;
public:
  void Begin() { header = a.Alloc<StateGroupHeader>(); header->numStates = 0; header->stateMask = 0; }
  void Write( State& state )
  {
    uint size = state.Size();
    u8* mem = a.Alloc(size);
    memcpy(mem, &state, size);
    ++header->numStates;
    header->stateMask |= u64(1)<<state.id;
  }
  StateGroupHeader* End()
  {
    StateGroupHeader* result = header;
    header = 0;
    result->sizeInBytes = a.Mark() - ((u8*)result);
    return result;
  }
};
Edited by Hodgman
1

Share this post


Link to post
Share on other sites

[quote name='Hodgman' timestamp='1358051934' post='5020960']
If you're going for memory efficiency, be aware of your compiler's padding behaviour when designing your header structures
[/quote]

 

Yeah, that StateGroup struct was just a quick example; I'd definitely worry about padding in real code.

 

[quote name='Hodgman' timestamp='1358051934' post='5020960']
e.g. I put state-groups directly in my engine's model, material and technique files
[/quote]

 

Putting the state groups in resources files is an intriguing idea.  How do you deal with pointers in this case, as obviously they will have different values each time you run the engine? I suppose you could store some other data in the pointer field, and then fix the pointers at runtime -- e.g. maybe a hash of a filename.  Or do you do something else?

 

 

[quote name='Hodgman' timestamp='1358051934' post='5020960']

I do pretty much the same thing that you've thought of here, except I also put a bitfield in the header, with one bit for each type of state.
[/quote]

 

Your recommendation of storing a state bitfield sounds good -- that looks like it would be quite useful.  As I understand it, the bitfield can be used to quickly check whether a state group can be totally ignored, but if not, the next step is to check each state individually?

 

Thanks for the detailed example of how you might generate state groups; that answers several questions I had.

0

Share this post


Link to post
Share on other sites
How do you deal with pointers in this case, as obviously they will have different values each time you run the engine? I suppose you could store some other data in the pointer field, and then fix the pointers at runtime -- e.g. maybe a hash of a filename.  Or do you do something else?

First up, if you want to research this, the technique of loading your runtime data structures straight out of a file with no (or little) on-load processing is usually called "load-in-place" or "in-place memory", or something similar. There's a gamasutra article here.

 

For pointers within a particular asset (e.g. a pointer from a model header to an array of state-group pointers, to a state-group) I use the Offset (relative address) and Address (absolute address) classes in this header -- i.e. I don't use actual pointers.

 

For things that need to be pointers at runtime, but can't be known in advance (e.g. a pointer to an actual D3D vertex buffer), then they have to undergo "pointer patching" on-load.

For example, say a model asset has n vertex-buffers, and also has some state-groups that need to contain pointers to those vertex buffers. The state groups are saved with an integer from 0..n, in place of the VB pointer, which indicates the index of the VB that it should point to. On-load, the model's VBs are created by D3D and we now know the real pointer values. We can then iterate through the state-groups, reading these index integers and using them to look up the appropriate VB pointer, and writing the pointer over the top of the integer.

 

For references to other assets, I use filename hashes, yep. As above, these hashes can be converted to real pointers on-load, if required.

 

but if not, the next step is to check each state individually?

Yeah, my states are variable size, and the group-header doesn't contain the actual offset of each state. Therefore to iterate through the group, you have to inspect each state in a linear order, and determine the current state's size to know how far to jump ahead to find the next state. The bitfield does allow you to halt this iteration early if you know that you've already inspected all of the 'interesting' states in the group though.

 

As an alternative, you could allocate an array of size numStates in/after the header and write the offset of each state into this array. If you then ordered the states by their ID value, you'd be able to quickly jump to a particular state that you're interested in without iterating through each one.

Edited by Hodgman
1

Share this post


Link to post
Share on other sites
Is the reason for storing StateGroups as a header and chunk of States that you make it more cache friendly due to locality?

Also, you say you store StateGroups in Resources. Am I correct in assuming some StateGroups must still be created at runtime, e.g. geometry instance transforms. And therefore RenderItems creation also cannot be done when the game object is initialized, but must be done every frame. Is this correct or am I completely off base?
0

Share this post


Link to post
Share on other sites
First up, if you want to research this, the technique of loading your runtime data structures straight out of a file with no (or little) on-load processing is usually called "load-in-place" or "in-place memory", or something similar. There's a gamasutra article here.
 
For pointers within a particular asset (e.g. a pointer from a model header to an array of state-group pointers, to a state-group) I use the Offset (relative address) and Address (absolute address) classes in this header -- i.e. I don't use actual pointers.

 

I had heard of this technique before; I was more wondering about the specific case of state-groups, where it seems that a lot of states would need to store pointers.  For example, pointers to shaders, textures, vertex buffers, index buffers, cbuffer data, etc.  Your example of model assets cleared up my questions about this, though.  I wasn't quite sure what the name for this technique was, however, so your suggestions for terms to research look helpful.  I'll have a look at the article you linked as well.

 

So in general, the idea is to also store the actual data as well as the state groups, then in place of an actual pointer use an offset or address to the data, and then perform pointer-patching when loading.

 

Yeah, my states are variable size, and the group-header doesn't contain the actual offset of each state. Therefore to iterate through the group, you have to inspect each state in a linear order, and determine the current state's size to know how far to jump ahead to find the next state. The bitfield does allow you to halt this iteration early if you know that you've already inspected all of the 'interesting' states in the group though.
 
As an alternative, you could allocate an array of size numStates in/after the header and write the offset of each state into this array. If you then ordered the states by their ID value, you'd be able to quickly jump to a particular state that you're interested in without iterating through each one.

 

I think linear iteration would probably be fine for me as well.  Using the bitfield to skip the rest of a state-group if possible sounds like a good idea.

Edited by IncidentRay
0

Share this post


Link to post
Share on other sites
So I think I've managed to go full circle from virtual Draw methods to a Render Command Queue back to virtual Draw methods. It just doesn't seem worth it to do all this work just for a unified renderer when you could have a virtual Draw method and call it a day. If renderable objects are managed and batched prior to being made into RenderItems then as far as I can tell, you could just give the batch a Draw method and enqueue the batch.

I guess I don't really see the point of the Commands.

I am at a loss because I want to do this the right way, but I have too little experience with coding rendering APIs to know what that is. And please don't say, "There is no 'right way.'"

Could anybody recommend a book that covers render queues similar to what has been described by Hodgman?
0

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