Thursday, January 5, 2012

Dual Quaternions

Hello,

I've been reading through Game Engine Architecture, and must commend you on an excellent book. I'm a lead-programmer with just over 10 years experience making games, so I'm not directly in the target audience, but I've still found it a very interesting read. I just wish it had existed a few years earlier!

Anyway, the section on Dual Quaternions was something that interested me - I hadn't heard of them before, and the possibility of representing rotation, translation and scale in 8 floating point numbers sounded useful - to good to be true really! However, having skimmed through the cited .pdf file, I can find no mention of them representing scale, and this resource http://isg.cs.tcd.ie/kavanl/papers/sdq-tog08.pdf suggests also that they cannot represent scale. Quote:

"Dual quaternions cannot represent non-rigid transformations, such as scale and shear. This means that dual quaternion skinning, unlike linear blend skinning, is restricted only to rotating and/or translating joints."

Anyway, thought I'd mention it. If I've misunderstood them and scale is possible, then apologies. If not, then perhaps a section for corrections on the website would be useful.

Kind Regards,

Andy Weinkove.


____________


Hi Andy,


You are quite right, that is an error.  Dual quats represent a combined rotation and translation... basically a "screw motion" and are incapable of describing any kind of scaling transform.


Cheers,
J

Advice on Learning Assembler


From: Thomas Amundsen

Hi Jason,

I was introduced to your book “Game Engine Architecture” through CSCI 522: Game Engine Development at USC. First, I’d like to say that it is a GREAT book, and can’t thank you enough for writing it!

The biggest question I had after working through the some of the chapters and then reading your blog is, how do you go about learning assembler? I majored in computer science during my undergrad, so I spent about half of a semester learning assembler for some fake architecture. But that didn’t transfer over to the real world, and whenever I see the disassembly in Visual Studio, I have no idea what’s going on. So, I think it would help me a lot to learn this stuff. Do you have any book recommendations, or other general recommendations?

Thanks so much!

- Tom

____________________

Hi Tom,

The best way to learn assembler is to pick a CPU that you have access to (the Intel Pentium or P6 family are probably the most accessible, since they live in most PCs and the latest Macs as well). Then pick up a book on the Intel P6 ISA (instruction setarchitecture), and try coding something!

One way (a not very friendly way, mind you) is to grab the actual Intel docs and pour through them.  You could start here:
  http://download.intel.com/design/PentiumII/manuals/24400101.pdf
and then move on to the manuals that describe the actual instructions etc.

But really that gives you too much detail.  What you want is to learn the basic concepts of registers, aritmetic/logic units (ALU), and assembly instructions... then start learning the basic instructions and what they do.  I don't know of a book offhand that teaches this, but here are a few ideas I found on Amazon:

http://www.amazon.com/Introduction-Assembly-Language-Programming-Processors/dp/0387206361/ref=sr_1_1?ie=UTF8&qid=1296888434&sr=8-1

http://www.amazon.com/Assembly-Language-Intel-Based-Computers-5th/dp/0132383101/ref=sr_1_2?ie=UTF8&qid=1296888491&sr=8-2

http://www.amazon.com/Introduction-Assembly-Language-Computer-Architecture/dp/0763772232/ref=sr_1_3?ie=UTF8&qid=1296888515&sr=8-3

You could also go old-school and learn the way I did, by reading about a much older... and therefore much simpler... CPU.  For example, the 6502 -- the CPU in the old Apple // computers.  It is very simple, and therefore a great way to learn the concepts without getting bogged down in details and modern optimizations.  Here's one:

http://www.amazon.com/Assembly-Language-Programming-Lance-Leventhal/dp/007881216X/ref=sr_1_1?s=books&ie=UTF8&qid=1296888579&sr=1-1

Give that a whirl and let me know how it goes.
J

Rendering Performance

From: Manuel Fernandez


Hello, 

My name is manny, and I'm a big fan of your book. I think it's great how all the main parts of a game engine interact with each other. Most of the time you can probably find good resources online that teach you how to do one thing or the other, but bringing things together to make a game engine is a topic all on its own. I think you book does a great job at detailing all the different parts of a game engine and how they work together. 

I bought your book after I read an article in gamasutra about it, i thought it would be useful. I have a Quake 3 BSP file loader which works on both Direct X and Open gl. I'm pretty proud of it because it implements Quake 3's PVS and AABB to frustum culling, and bezier patches. What surprises me is that when i get out of the BSP comfort zone, like rendering from outside the bsp, or simply rendering the whole virtual world, it's a pretty big hit in performance. I'm pretty sure game engine's like UDK, or source can handle drawing a quake 3 level without a problem. I guess my question is what makes industry strength game engines perform so much better than average? I think it's more than just smart culling, or better thread management. 

Thank you for your time. 

______________________________

Hi Manny, I'm sorry I didn't get back to you sooner, but your email fell thru the cracks.

I'm not a deep rendering guru, more of a generalist.  But here are a few ways in which industrial strength rendering engines attain their high performance:

1. Rendering data is often packed into (relatively) small and contiguous data packets, to improve cache coherency.  The real key here is to eliminate all pointer chasing during rendering. This means duplicating data, but it is worth it. We did this at Naughty Dog in order to optimize our rendering of dynamic objects (i.e. everything that is not static background geometry) and it yielded immense improvements. So as a (contrived) example, instead of a MeshInstance struct that contains a pointer to a material, which in turn contains pointers to lighting data etc. we would now have a RenderableInstance struct that contains all the data that you would have found by following down all the pointers (and ONLY the data that is actually needed for rendering). (Or maybe it would be multiple arrays of structs, each one containing one type of data... but the key is to avoid the use of pointers to refer to data that lives elsewhere in memory.)  These structs aren't tiny, but at least we can iterate thru them in one linear swoop, and gain the benefits of precacheing the memory as we go. Any time you jump around in memory you're going to flush your L1/L2 cache and hurt your performance.

2. Pre-sorting to reduce render state changes is a great idea.  That includes which vertex and index buffers you're rendering from (i.e. which mesh), which vertex and fragment/pixel shader programs are loaded, your render constants (transformation matrices, lighting vectors, animation matrix palette, etc.), which texture units you're rendering from, and all the other "render state" of the GPU.  Most high-performance rendering engines divide the geometry up into two big categories: opaque and translucent. For opaque, you sort to minimize render state changes as I said: set up the render state for a particular "bin" and then render everything in that bin, then change state, and go again.  For translucent, you typically have to sort the geometry in back-to-front order so it looks right.  So you lose the benefits of sorting by render state changes. You do it after the opaque stuff, and try to limit how much translucent geometry you render.

3. Z pre-pass can be a savior, especially for GPU hardware on which fill rate (pixel shade, blend and store rate) is limited (like the PS3).  I think I describe it in the book, but the basic idea is to put the GPU into a special z-write-only mode, then render the scene with the simplest possible pixel shader (since only the depth/z matters anyway). This is very fast. Then you render the scene again, this time with real pixel shaders enabled (much slower), but without writing to the z-buffer... onlyreading the depth values you deposited before. The GPU is pretty good at early-ing out on fragments whose z values are larger than what's already in the z buffer, so you don't pay the cost of shading them.

4. Eliminate the middle man. DirectX and OpenGL are actually wrappers around a lower-level "raw" GPU hardware protocol called a command list. This is basically the stream of data that the GPU actually consumes, in order to know what to render, what render state changes to make, etc. On the PS3, Sony provides a library called libgcm that allows games to build up their own GPU command lists directly, rather than going through the OpenGL layer. This is really the only way to render fast on the PS3. And actually, Naughty Dog's rendering team wrote an optimized interface, similar to libgcm, but even faster, and customized to the specific compressed data formats that we use... That's what Uncharted uses to render.  So the lesson here is: if you have access to a library like libgcm (or want to reverse-engineer one!!!), that can be the best way to wring the last ounces of performance out of your rendering engine.

That said, the only way to be sure about performance is to code it and profile it. So once you have a basic mesh rendering working, you can try optimizing it by (a) keeping all the render data in one or more contiguous arrays -- i.e. eliminate pointer-chasing in your render loop, and (b) sort by render state changes for maximum thruput. Then profile it, and tweak from there.

I hope this helps a little.
J

From Tokyo

From: Joseph Conley


Hi Jason,

I'm also a programmer in the game industry and I've recently come across your book.  I recently started a new job in Tokyo at Namco Bandai Games (I had previously worked at Activision Blizzard's Vicarious Visions studio(boy that's a mouthful)), and a number of people here recommended your book, and work bought me a copy that I hope to read soon, it looks very interesting and informative.  

On an interesting note, the reason why my coworkers here have your book is because they are actually the ones reviewing the translation work for Softbank, the publisher of the book here. I guess they are hoping to wrap that up asap, as the book is due out in this country November 30th I believe.

I have also been a huge fan of the Uncharted Series, and I attended the panel "Game Design with the Dogs" at PAX East earlier this year where Richard Lemarchand, Jacob Minkoff, and Justin Richmond lead us through a demonstration of creating a level in Uncharted 2.  The narrative in that game and the way that everything is designed with considerations to narrative in mind is just amazing.  I would love to work on a game like that some day.

The technical achievements are just as impressive if not more so, and I have to commend you on you and your teams' work.  As I mentioned to Jacob when I talked with him after the panel, I work in the same industry as you and I still have no idea how you make such an awesome game.  Maybe your book will help me understand :-)

Thanks for all that you do, I'm eagerly anticipating Uncharted 3.
Joseph Conley


____________________________


Hi Joseph,

Thank you so much for all the kind words!  I'll be sure to forward your compliments on to the whole team.  I'm very pleased to hear that you're reading my book -- if you have questions, feel free to ask!
By the way, I checked out your blog.  I've always wanted to travel to Japan... spent some time in Chennai, India and Hong Kong, but never Tokyo (at least not yet).  Your ray tracer appears to have been a success, too -- some nice renders -- what a great project!  Anyhow, best luck!  Learn lots and have fun on the other side of the world!

All the best,
J



___________________________


Hey Jason,

I wanted to let you know that I am about halfway through your book now (finishing chapter 8), and I'm really enjoying it. Even though I've worked in the industry for about 2.5 years now, I feel like I have learned a whole lot, and that you have helped fill in a lot of gaps in my knowledge of game engines. And even the parts that I have learned before in various classes spread out over years of college or in previous work experience have been aggregated together and presented in a format that is exactly aimed at the sort of work I do, and for that reason it's incredibly useful.  I'm really looking forward to the sections on rendering and animation.

Also, while I have been reading, I've been keeping a log of any questions, suggestions, or typos I notice, and I'll send a link to share that google document with you, as I will continue to update it as I read.  If you want, I can also ask the technical supervisors for the Japanese version to send me any odd points they noticed, and I can add that to the document as well.

By the way, are you going to GDC next month in San Francisco?  Me and my coworker at Namco Bandai Games (and one of the technical supervisors for the Japanese version of GEA) Kazuhisa Minato will be going, and if you're there we'd love to get together and chat about Game Engine Architecture.  Also, I saw that Uncharted 3 was announced, and will have support for stereoscopic televisions, which is something else we could discuss, as the game we are working on ("Dead Storm Pirates 3D" for arcades) also is stereoscopic.

Well good luck with Uncharted 3!

___________________________

Hi Joseph,

Thanks for the compliments, and for taking the time to accumulate notes.  I know I'll find them invaluable as I start work on the second edition (which I plan to begin sometime soon).

I just got a copy of the Japanese version of GEA, and I must say I'm thoroughly impressed!  The folks that translated it clearly spent a lot of time and effort on it.  I appreciate your help in keeping me informed.

Sadly, I'm not attending GDC this year.  I would very much like to meet you and Minato-san.  Hopefully we'll be able to hook up at another industry event, such as E3 or the like.  And certainly, if either of you find yourselves in Los Angeles, drop me a line and I'll hook you up with a tour of Naughty Dog.

Cheers and many thanks again,
J


Wednesday, January 4, 2012

Course Materials

From: Farshad Kheiri


Dear  Dr. Gregory,


I like to develop a course based on your interesting book "Game engine Architecture" for a community college. I was wondering if you can provide me some materials for the class. We like to put our course based on your book. I really appreciate your help in this regard.

Kind regards,
Farshad

_____________________

Hi Farshad,

Well, I'm not a PhD, although it's fun to see Dr. in front of my name!  I'm pleased to hear you're using my book with your new course.  The book's website, http://www.gameenginebook.com, provides some course syllabi, lab assignment outlines, and some 3D math problems.  I hope this helps.  If you have other questions, feel free to ask!

Cheers,
J

Getting Hired

From: Anurag Patnaik


Hi Jason,

I recently discovered your book about game engine architecture at a friend's place and really liked it. Got a copy for myself from Amazon. Hoping to be able to use this along with my course curriculum soon.

I got admitted into Cornell university this year under their Grad school. I was hoping you could give me some pointers about what skills are expected within the industry so that I could spend my year at school actually learning something that would help me get into games. Other than C++, I have heard that knowing your way around Lisp and maybe Python are some useful skills. Is there anything in particular that you think would be useful for a grad like me who is trying to make it into game design ?

I hope I am not intruding .. but any advice from Naughty dog is worth a shot :)

Regards,
Anurag

_________________________

Hi Anurag,

I'm pleased you are finding the book helpful. Pretty much everything you need to know to break into the games industry as a programmer is in the book. 3D math is super important, so definitely focus on solving 3D problems, working with vectors, matrices and quaternions.  A little calculus doesn't hurt either!

On the programming side, I strongly believe that learning multiple languages helps you to be a great programmer.  I like the Lisp variants, not because they are particularly useful in industry (except at Naughty Dog!), but because they really opened my mind to a completely different way of thinking about computer science. I also recommend Python and C# (both are used extensively in the industry).  Oh, and to stay ahead of the crowd, read a bit about Haskell, Ocaml and/or F# -- functional languages are probably the way of the future, given the move toward parallel computing hardware.  On that note, familiarizing yourself with parallel programming practices will prove super useful in the years to come.

You should also learn a little assembly language... the PowerPC ISA (instruction set architecture) is a great one to learn nowadays, given that this is the ISA used by Xbox360, PS3 PPU and PS3 SPUs, with minor variations.  Again, understanding assembly gives you insights into how higher-level languages like C, C++ or even Lisp actually work under the hood.

One final note of advice: Try to learn by doing, not just reading.  Do 3D math problems on paper, then try to implement the solution in C++ or the language of your choice.  Make some small tech demos and mini games.  The more you do, the more you'll be prepared to wow them at the job interview.

Best luck!
J

______________________

Hi Jason,

I am extremely sorry for such a delayed reply. I was in the middle of moving to Cornell when I had previously mailed you and upon my arrival in Ithaca, your reply was conveniently placed in my Spam Folder !

Thank you so much for such a detailed response. I am really happy to say that I do have a lot of bases covered here. I am actually building a game as a design project this semester, hopefully using my own C++ engine ! That should be an interesting experience. I have also started to tinker around with Python and am cursing myself for not having discovered this wonderful language earlier. Rounding up this semester would be a healthy project in AI and a course in Computer Graphics which I am hoping would help me understand 3D math better. I had no idea about functional languages, thank you so much for pointing me to this new paradigm.

Again, thank you so much for taking the time to write back such a prompt and detailed answer to my queries. I really cannot thank you enough. Your feedback has been of immense help.

Sincerely yours,
Anurag Patnaik

_____________________

My pleasure!  Best luck, and keep in touch.
J

Course Projects

From: Jeremy Walker


Hello Mr. Gregory,

I"m currently a grad student at Appalachian State University and I was wondering if I could ask you a few questions.  This fall six graduate students seeking their Masters in Computer Science (including me) will be taking an independent study on what we are calling The Design and Implementation of Game Making.  I'm contacting you because we decided to use your Game Engine Architecture book as our semester's text book.  We are planning on using the new Unreal Development Kit that is out now.

All of the students taking this class have had 4+ years of programming experience though none have done anything with Game Development.  We made up the class with the goal of getting a basic understanding of the game making process (from storyboard to actually playing something) and we wanted to learn this cool new Unreal Kit while we were at it.  By the end of the semester we would like to actually have a functioning game though it may not be the prettiest thing you've ever seen.  Also we were wanting to get a feel for the team aspect of game making.  In all our courses so far we've never worked in a team larger than 2 so a team of 6 sounds kind of exciting to me.  It would be great if we could have a real team where someone was a "specialist" at one aspect of the game making process, like 3d rendering or gameplay logic, but as of now no one is a specialist at anything.  We're all newbies when it comes to game making, though I'm sure we've all wasted many hours playing them.

So I guess my question is, Do you have any suggestions or advice on where or how we should start?  I've looked over your syllabi on your site already.  Could we condense that into one semester somehow?  Or are we reaching too high?  I just want to know if you have any pointers on how we should go about this.

Thanks!

_____________________________________________

Hi Jeremy,

I'm very pleased to hear that you're considering using my book as your course text.  Your plan of having the students build a game using the UDK is a great one, similar in many respects to what I did in my course.

A 6-person team shouldn't be too hard to manage.  (I've done this kind of project with as many as 20 students...)  That said, I always liked making sub-teams of no more than 2 or 3 people.  (I recall my days as a student -- it was almost impossible to get 4 or more people to agree on a time and place to meet to work on our projects!)  So you'd be best off dividing the work into three major categories with sub-teams of 2 each.  Perhaps one team could focus on the player mechanics and camera, one team on enemies/AI, and the third on level design/layout and visuals.  Or whatever makes sense for your specific project.

I'd also recommend approaching your design like the layers of an onion.  Focus on the most basic, core and crucial functionality first.  If and only if you make good progress on that, then proceed to the next layer of polish and less-important features, and so on.  That way, by the end of the semester, you'll have the highest chance of arriving at something that actually works and has all the core features you need.  (At Naughty Dog, we're big on getting something functional, but not pretty, up on screen as quickly as possible. This keeps us focused on what's important, and informs us of what is and is not working as early in the process as possible.)

Best luck, and do keep me posted on how it goes.