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

DWORD instead of std::bitset fails

10 posts in this topic

Hello,

 

without talking too much about the modalities, why does this code work:

	        //iteratre through render instances
	        for(auto pInstance : vSorted)
	        {
		        //store pointer to instance and state groups
		        const StateGroup** pStateGroups = pInstance->GetStateGroups();

		        std::bitset<64> bPreviousApplied;

		        //iterate through instance state groups
		        for(unsigned int i = 0; i < pInstance->GetStateCount(); i++)
		        {
			        //store pointer to state group and state pointer
			        const StateGroup* pGroup(*pStateGroups);
			        const BaseState** pvCmds = pGroup->GetCommands();

			        //iterate through command group
			        for(unsigned int j = 0; j < pGroup->GetCommandCount(); j++)
			        {
				        //get pointer to state
				        const BaseState* pState(*pvCmds);
				        //get state type
				        const BaseState::Type type(pState->GetType());

				        //ignore if same command was applied earlier on stack
				        if(bPreviousApplied[type])
                        {
                            pvCmds++;
					        continue;
                        }

				        //set applied status to skip any further commands of this type
				        bPreviousApplied[type] = true;

and this one doesn't?

	        //iteratre through render instances
	        for(auto pInstance : vSorted)
	        {
		        //store pointer to instance and state groups
		        const StateGroup** pStateGroups = pInstance->GetStateGroups();

		        DWORD bPreviousApplied(0);

		        //iterate through instance state groups
		        for(unsigned int i = 0; i < pInstance->GetStateCount(); i++)
		        {
			        //store pointer to state group and state pointer
			        const StateGroup* pGroup(*pStateGroups);
			        const BaseState** pvCmds = pGroup->GetCommands();

			        //iterate through command group
			        for(unsigned int j = 0; j < pGroup->GetCommandCount(); j++)
			        {
				        //get pointer to state
				        const BaseState* pState(*pvCmds);
				        //get state type
				        const BaseState::Type type(pState->GetType());

				        //ignore if same command was applied earlier on stack
				        if((bPreviousApplied & (1 << type)) == (1 << type))
                        {
                            pvCmds++;
					        continue;
                        }

				        //set applied status to skip any further commands of this type
				        bPreviousApplied |=  (1 << type);

in the second code, where all I did was change the std::bitset to a standard dword. However, in this code he will almost never jump into the if condition, resulting in strange visual anomalies. Well, does somebody see anything I screwed up? I though I got the bit-part of c++ figured out, but seems I didn't. I'd be glad for any help, its already getting late and I start feeling kinda numb :/

0

Share this post


Link to post
Share on other sites


Can type be larger than 32? You have a 64-bit bitfield but a 32-bit DWORD.

 

It can definately go beyond 32, yes. I already tried it with size_t before, didn't do anything eigther... however, now trying it with "ULONGLONG", while it still makes no difference in execution, at least I get this warning:

warning C4334: '<<' : result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)

Anything I need to do to make a shift to 64 bit (already tried converting "type" to ULONGLONG...

0

Share this post


Link to post
Share on other sites
Yes, "1<<x" is dangerous, because "1" is an "int". If "int" is 32 bits, then if x is 32 or higher, you're gonna have a bad time.
Make sure that "1" is cast to the appropriate type before shifting, as above.
2

Share this post


Link to post
Share on other sites

Thanks, 1ULL actually did the job! Unfortunately it was not worth trying anyway, I've been shown in my profiling that the if(bitset[x]) - line was 5% of my overall CPU time in my stress-tests, so I thought about changing it to a literal type just to try it out. With that bug, I got about 5ms (from 24 to 28 FPS), but now I'm back to old values. Well, nevermind, I've at least learned something new about bitshifts.

 


I would recommend using bitmagic http://bmagic.sourceforge.net/

 

Thanks for the suggestion, however in my current project I don't want to use any external libary whatsoever, for the sake of learning things from the very ground. If I ever feel for working more productively, I'll keep it in mind though.

0

Share this post


Link to post
Share on other sites
Getting off topic now, but FWIW, on many CPUs, "literal << variable" is far, far slower than "variable << literal".
The former (lhs<<rhs where rhs isn't known at compile time) is often microcoded and/or implemented as the loop: [font='courier new']output=lhs;for(int i=0; i!=rhs; ++i) output <<= 1;[/font]
i.e. shifting by a variable amount is very slow, but shifting by a constant amount is fast.
Often it's very easy to convert your loops (though not this case, hence off topic), e.g.
[font='courier new']for(i=0; ...; ++i) { mask = 1<<i; ... }[/font]
Becomes:
[font='courier new']for(i=0, mask=1; ...; ++i, mask<<=1) { ... }[/font]

However, the OP code might benefit from not repeating this expensive operation (or [font='courier new']operator[][/font]) 3 times in a row, and instead caching the result once, depending on how much faith you have in the compiler to spot and clean up the redundancy wink.png Edited by Hodgman
0

Share this post


Link to post
Share on other sites

Thanks, noted it, in case I might come across a case where I can optimize this.

 

 

 


However, the OP code might benefit from not repeating this expensive operation (or operator[]) 3 times in a row, and instead caching the result once, depending on how much faith you have in the compiler to spot and clean up the redundancy wink.png

 

I already tried that, it didn't show any significant difference neigther in FPS nor when profiling, in release mode. Even though in my tests, the operation was likely executed about 300.000+ times, it seems most of the time spend here comes from the if()-condition itself. Though even more offtopic, do you have any suggestion on how to improve that, maybe some complete different branch-less approach for checking if a state was already applied before?

Edited by Juliean
0

Share this post


Link to post
Share on other sites

Though even more offtopic, do you have any suggestion on how to improve that, maybe some complete different branch-less approach for checking if a state was already applied before?

I assume that actually applying/submitting the states that make it past that continue statement pretty much has to involve branching, but sure, you could eliminate that if-continue branch like this:

const BaseState* toApply[MAX_STATES];
int toApplyCount = 0;
for(...)
{
	toApply[toApplyCount] = pState;
	u64 mask = (1ULL << type);
	int isValid = (bPreviousApplied & mask) ? 1 : 0; //should compile to a conditional move, not a branch
	toApplyCount += isValid;
}
for(int i=0; i!=toApplyCount; ++i)
{
	toApply[i]->Apply();
}

n.b. the pattern of if((bitfield & mask) == mask) is only required if the mask contains a pattern of more than one bit and you need to check that every bit in the mask are all set. If mask only has one bit set, you can just use if(bitfield & mask). But I guess that only matters if you're doing the bitfield logic by hand instead of using std::bitset ;)

Edited by Hodgman
1

Share this post


Link to post
Share on other sites


I assume that actually applying/submitting the states that make it past that continue statement pretty much has to involve branching, but sure, you could eliminate that if-continue branch like this:

 

Thanks, I'll try it out as soon as I'm done upgrading to DX11 (I'm much too hyped to backtrack in my repository right now :D). Howerver, supplying the states doesn't involve branching per se, its a long switch-statement, which is appearently optimized away (jump table?), because it doesn't take any noticable amount of performance (an if-else nesting takes litteraly 100 times as much performance, profiled). Doing the same thing for this if-condition didn't gain any performance, on the other hand, quess changing one if-condition into a switch-statement doesn't do much for the compiler. Lets hope that it works better with your suggestions, thats quite some performance that could be gained, other than that one check, there is no single line in the queue that takes up a noteworth amount of time...

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