You are currently browsing the archives for the Radiant category.
- 3D Stuff (12)
- AI (1)
- BumpTop (5)
- C/C++ (22)
- Open Source (8)
- Radiant (11)
- Seneca (6)
- Stuff (17)
- The Mortal Realm (2)
- Uncategorized (14)
- win32 (12)
- July 9, 2010: On Visual Studio 2010...
- December 22, 2009: Efficient Rendering, A La Mark.
- December 3, 2009: A Simple Opponent
- December 3, 2009: Blog++
- September 7, 2009: Food Budgeting
- July 3, 2009: Too Busy...
- March 26, 2009: How Do Patents Apply To Me?
- February 27, 2009: U.S. And Human Rights
- February 7, 2009: I've Been Busy...
- November 10, 2008: Radiant Update
Archive for the Radiant Category
Efficient Rendering, A La Mark.
December 22, 2009 by Mark.
Rendering efficiently is one of those topics that is widely spoken about in the world of 3D graphics. Asking a question like ‘What is the best way to render a bunch of Objects’ is as open ended as asking ‘What is the best way to cook chicken soup.’ It is all based on application and preference and in all likelihood, there is no universal answer to this question. However, there are a series of specific solutions to this problem that can help in creating a mechanism that is best for the particular situation.
My problem is rather generic and will require a generic solution. I have a bunch of objects that need to be sorted by certain criteria in order to minimize state changes. It has to also support Shaders (Cg in my case) and it should minimize Shader state change between object rendering. Furthermore, an object must be generic enough to support complex models with bones and animations. On top of that, it should be easy to use. To start, we might need to break this down into smaller parts.
Objects:
For the time being, lets refer to an Object as a list of vertices inside a vertex buffer. It may or may not be accompanied by an index buffer, but in most cases it will. This Object will be shuffled to the graphics card to be rendered for each Object that exists in our world. This is inevitable until we support something complex like hardware based instancing.
State Changes:
Unless you want all objects to be rendered in the same way, in the same spot, and with the same vertices, you probably want some sort of state change. A state change is a change in any part of the system, whether it is the position of the camera, a new Object to be drawn, or a new effect. To quantify a state change, it is best to organize it into the types of state changes: swapping Render Targets, Shaders, Technique, Shader parameters, and using a different vertex or index buffer to draw an object. The order of the state changes, as listed above, matters because the changes at the beginning of the list are the most expensive and the changes at the end of the list are least expensive.
The RenderGraphNode:
This is a generic interface to which many types of nodes will be derived from. Each derivation of the node will be the embodiment of the changes listed above. In addition to the state change, the node will also be a container for child nodes. Usually the node will be generic enough to contain any type of node. However, in our case, we want to preserve an order to our nodes so that we optimize the state changes. The root of our tree will be a change in Render Targets. For the most part, there will only be one Render Target, the backbuffer (our screen). When a child node is added, it will automatically be sorted into the correct place in order to minimize the state change. This is especially important for Shader parameter changes because there can be multiple parameters in one Shader.
The RenderGraph:
In order to encapsulate all this, I need a class that will be the owner of Render Targets. It will be the only thing passed into the Renderer for drawing. At that point it will traverse the tree and render.
Sounds simple right? Yeah, but something doesn’t feel good about this design.
If we leave the design at this point, we are left with a bunch of nodes in which the user has to put together. This design is acceptable by some. In fact, OpenSceneGraph uses such a design for its SceneGraph. It is a bunch of classes that fit together in a tree fashion. Throw in a Visitor pattern into the mix for easy iteration and you have an engine. I’m not quite as happy with that design as my OpenSceneGraph counterparts are. The problem is, in my eyes, that it’s very verbose. Putting together a simple scene with an airplane in it was quite lengthy. You have to add a GeometryNode to a TechniqueNode to a ShaderNode to a RenderingTarget, and so on.
So back to my original question, what is the best way to implement something like this? When I figure it out, I’ll write about it.
Posted in Radiant, 3D Stuff, C/C++, Stuff | No Comments »
Blog++
December 3, 2009 by Mark.
I’m converting my blog to something a bit more useful. My long rants about my game engine were all leading towards a game of some sort. In the process I have recruited a friend to help me realize that dream. So, give a kind welcome to Alex.
Our first title will be a strategy turned-based war game by the name of ‘The Mortal Realm.’ It will feature my 3D engine and a robust battle system. As far as complexity goes, this game is one of the simplest we have come up with. It’s a simple point and click style of game with very minimal artwork. I’m hoping it will be a great test bed for my engine as well as Alex’s AI.
Posted in The Mortal Realm, Radiant, 3D Stuff, C/C++, Stuff | No Comments »
How Do Patents Apply To Me?
March 26, 2009 by Mark.
I’ve been diligently working on a scene partitioning system which combines an Octree with a Uniform Grid. Basically, the way it works is that you build a loose Octree, which starts 4 levels deep. When each node reaches a critical mass, it subdivides into another level. The max levels you can have (While still being optimal) is 7. So, let’s assume that we have a detailed scene with a 7 level Octree. At the bottom of the Octree, each node is 128th the size of the entire area you are encompassing and it is also uniformly proportional to the entire area. You can build a Uniform Grid out of the bottom most nodes giving you the best of both worlds.
When all this is built, to add and remove items from the grid is a matter of doing simple division of finding out the exact spot in the Uniform Grid where the object belongs. Since pointers are being shared between Octree Nodes and Uniform Grid Nodes, you essentially add an item to the Octree in O(1) time (Adding to a Tree structure usually Takes O(Log n) time). Collision detection with simple objects is O(1) time while with complex objects it is O(Log n) time. What I have done is made the Octree a bit faster in some areas. Good idea, isn’t it?
Here is my problem. While randomly googling on this topic, I found a patent for this idea. The patent is very similar to what I just described. What I want to know (For all you Law junkies out there) is how does this effect me? Can I get sued? Does it matter that my implementation is my own and not copied from the patent? Does it matter that my implementation is Open Source? Are there ways to get around this patent (My implementation is different but algorithmically similar)?
Any suggestions are welcome.
Posted in Radiant, Open Source, 3D Stuff, C/C++, Stuff | No Comments »
Radiant Update
November 10, 2008 by Mark.
I held a bet with my brother to see who can guess the number of lines of code (or at least, close to it) my rendering engine is comprised of. Neither of us were close but I got a good sense of how much work went into this giant mash of code. About 25,000 lines is the total. That total is comprised of approximately 12,000 lines of computational code and about 13,000 lines of comments. This number does not include blank lines and such things. Regardless, it’s a staggering number for a project that is being worked on by one person, part time.
The engine itself is about 45% complete, with the majority of planning done. One of the components, my math module (it handles math stuffs and collision detection), is finally finished. I can breath a sigh of relief, it’s not easy stuff. Unfortunately I didn’t write any unit tests for it so I don’t know if it actually works or not. The project is open source. So, if anyone is masochistic enough to write a tests for my math module, by all means :).
Posted in Radiant, 3D Stuff, C/C++, Stuff, win32 | No Comments »
Primary Export: Pain
October 28, 2008 by Mark.
Everyone knows (or should know) that when you put together a DLL, you need to export functionality so that programs using your DLL know where to find your functions. This is usually done by prefixing classes or functions with __declspec(dllexport) or manually writing a definition file. Straight forward and right to the point. But what happens when you need to export something that does not have a name. Say for example, an overloaded new operator. What the hell does a new operator look like as a definition symbol?
I’ll give you a hint: its not human readable!
So, before I actually spoil the beans and tell you what I did, I have to explain why I did it, because its rather interesting. Radiant, my game engine, is split into 6 DLLs, all of which touch and create dynamically allocated memory somewhere. The problem with allocating memory all over the place is that you need to delete it in the same address space (or DLL) as you allocated it in. With that said, to make it more complex, I had to overload the new and delete operators in order to wrap around _aligned_malloc() and _aligned_free() calls. This is a special type of allocation that allows you to align dynamic memory to an address that is divisible by 16 (or any other value). This is crucial if your using SSE or any special instruction set because all values need to be aligned to at least 16 bytes.
Anyway, going back to the problem at hand, I have a bunch of overloaded operator functions that cannot be exported because if I try to add the __declspec(dllexport) prefix to them, the compiler will scream and tell you that the declarations of the functions do not match up with what is defined internally. Basically, what I am stuck with are a handful of functions that cannot exported programmatically. This is where the definition file comes into play. Exporting a function or class is as easy as entering its name in the definition file under the heading of EXPORTS. But here is the kicker, the overloaded operators of ‘new’ and ‘delete’ do not have a name! They are declared internally in a header that exists in the compiler’s own static data, and there’s no way to override inclusion of that header. Therefore, the only way is to manually enter the function’s mangled name into the definition file.
The mangled name looks something like the following:
| ??2@YAPAXI@Z | (void * __cdecl operator new(unsigned int)) |
| ??3@YAXPAX@Z | (void __cdecl operator delete(void *)) |
| ??_U@YAPAXI@Z | (void * __cdecl operator new[](unsigned int)) |
| ??_V@YAXPAX@Z | (void __cdecl operator delete[](void *)) |
In fact, those mangled names are fairly generic and may not match up correctly. But their names are very similar to what they should be. A more specific example of the name would be located in the Visual Studio directory under VC\crt\src\intel\_sampld_.def. This file contains a slew of definitions. What your looking for are the first four definitions that look very similar to the ones posted above. If you are running under a x64 or Itanium architecture, there are definition files for those architectures as well.
After a successful compile, all dynamic memory is allocated and deallocated in a single DLL’s memory space. This prevents the Heap Corruption errors I was getting before and allows me to further enhance the allocation and deallocation of dynamic memory. Woot!
I would suggest reading the following link because it contains a very good description of how this process works. Unfortunately, I stumbled upon this file AFTER I already fixed this problem. Alas, C’est La Vie.
Posted in Radiant, C/C++, Stuff, win32 | No Comments »
Results
September 7, 2008 by Mark.
I finished writing my own Thread Pool subsystem and then put it through its paces. The results are obviously not that shocking because anything that is multi threaded is more efficient on dual core machines. The machine I was running it on has a AMD Turion 64 X2 CPU and sports two 2.0Ghz cores. It’s not the best machine but its quite speedy for what it is.
Anyway, as I mentioned in the previous entry, threads are great and run best when they have a dedicated core. When there are more threads then cores, the OS then has to time-slice in order to give the threads equal time on the CPU. Having a dual core machine, the optimal amount of threads that I can handle is 2. The following is a graph of the time it took to process 50,000 square root calculations, 100 times in succession:

On the first run, I did not use any threads. Rather, I let the calculations run in sequential order on the main process. The blue bar represents the thread priority at normal and the purple bar represents the thread priority at Highest. I did this in order to measure the speed difference between the normal and highest priority setting. I should also note that even though the thread priority can be manipulated, it is up to the OS to enforce that. In some cases the OS may decide not to enforce the request for higher priority and give the processing power to other threads.
The second and subsequent runs were using the Thread Pool with an increasing allotment of threads. Each 50,000 calculations were plugged into its own Job and sent off to a worker thread. Having only one thread was not much of a boost over a single process (Took about 1920ms). The main process was put to sleep using the WaitForSingleObject() function call while the worker thread did the processing. At two threads there is a massive increase in time. Just as one would assume, the amount of work was spread over two cores and the time it took to process it all was cut in half (About 950ms). This is the optimal amount of work my CPU can handle because each thread has a dedicated CPU core. Increasing the amount of threads actually increased the amount of time it took to process the calculations. The reason being is that the OS had to time-slice the different threads on both cores. The time increased by about 20ms each time I increased the number of threads.
Thread Pools are very efficient at what they do. As mentioned in the previous entry, creating a thread and destroying a thread could have increased the amount of time required to process a batch of 100 calculations. Regardless, the work I done is Open Source and as is my engine. You can download it here (I’m not responsible for unexplained fires, deaths, or alien abductions due to using this code).
Posted in Radiant, Open Source, 3D Stuff, C/C++, win32 | No Comments »
SIMD And Randomness
May 20, 2008 by Mark.
Alright, I ended my zealous trek through the inner workings of SIMD just because I wasn’t using it enough in my project. Don’t get me wrong, its a really nice piece of technology to dabble in, especially when doing math on a large scale (say, Matrices). But for the day-to-day programmer in me, I really don’t get to use it as much as you might think.
But, I’ve done some detective work and most compilers nowadays optimize the code in such a way that SIMD gets incorporated automatically. Therefore, learning SIMD should be put in the same category as learning Assembly. Its nice to have but in reality it will be rarely used, if ever. I have a feeling that I’ll run into a situation where SIMD is required and its the only thing that can save the world from total destruction. Well, until then, I’ll just shelve it.
On the bright side, I finally gave my game/rendering engine a real name: ‘Radiant.’ I don’t know why I named it that, and for some reason, I never second guessed it. It was one of those spur of the moments that I hit F2 and started typing. Don’t ask me what or why, I’m still in the wilderness on that one. Anyway, progress on the engine is a bit slow just because the only time I get to work on is during bus trips to and from Hamilton, or other places. If there were more hours in the day, I could actually get something done.
I should also say that I’ve re-worked the way the engine is laid out because all my singletons were not working well with DLLs (I think I wrote about that, somewhere). This quickly because a mess. SVN likes things to be done in order and in small chunks, else it starts bugging you about cleaning up the repository. I like to do things in big chunks, literally taking a +1 Battle Axe to the tree and then rebuilding it from scratch. Bad tactic, I know. Lets just say, SVN was in pain. Poor thing.
As a side note, I found out something interesting today about the MSVC build system; It supports multi-core compilation! Its like your own distributed build but on your own machine, Hooray. More on this later.
Posted in Radiant, Open Source, 3D Stuff, C/C++ | No Comments »
Some GameDev Math Resources.
March 26, 2008 by Mark.
I’ve been scouring the internet for some decent resources on math, collision detection, and physics, so here are some:
- Essential Math
- Game Physics Resources
- Real-Time Collision Detection
- Advanced Character Physics
- Simple Math Primer
- Flipcode’s Articles
- Harvey Cartel’s Collision Detection Site (Thanks Miksago)
More to come, later.
Posted in Radiant, 3D Stuff, C/C++, Stuff, Uncategorized | 1 Comment »
To OSG, or not to OSG.
March 17, 2008 by Mark.
I have been working on my game engine (Which still does not have a name; Suggestions are appreciated) for a while now. It has seen its share of refactors ever since I started on it back in 2006. But recently, I have come to a fork in the road. I am overwhelmed with the amount of things that I need to keep track of which makes me second guess some of my design decisions that I have made. One of which, is weather or not to write my own scenegraph or to use a stock one, say, OpenSceneGraph.
From experience, I can tell you that the amount of time that it would require to learn, implement, and test OSG’s implementation would be comparable to the amount of time it would take me to write one from scratch. I have been plagued with these sorts of decisions ever since I started the rewrite of my code. The benefits of using a library over writing your own can only be determined by the ease of use of the library. For example, if its easy to build, incorporate and debug then it probably is better to choose it then to write one from scratch. In the case of OSG, the library is big. No, its Massive!
I am still not convinced that OSG would be worth using over my own implementation, simply because of the learning curve and the work required to properly incorporate it into my engine. I would literally have to wrap my engine around OSG, rather then have it work symbiotically. That just does not sit well with me. Regardless, I will still poke around inside OSG and learn how their scenegraph implementation works.
Where is my trusty Bearded Axe, its time for some hacking!
Posted in Radiant, Open Source, 3D Stuff, C/C++, Stuff | No Comments »
Et Tu Singletone.
February 12, 2008 by Mark.
Seems that my favorite pattern is also the bane of my current Game Engine development. Turns out that a singleton pattern doesn’t like to play nice with Dynamically Linked Libraries. The whole notion of being dynamic is counter productive to static members inside functions or classes. Looks like the only way to actually fix this is to wrap it all up in a big handler of some sort and have the calling code manage it. Either that or create a section of shared memory in the DLL and allow it to manage the singletons. Seems like a bit of a pain in the butt just to avoid a global variable. Hmm, unless I can wrap it in another creational pattern, possibly Builder or Abstract Factory…
More research is required. And research requires Tea. Therefore more Tea is required.
Posted in Radiant, Open Source, 3D Stuff, C/C++ | No Comments »