Thursday, December 16, 2010

HLSL editing with Intellisense like behavior

HLSL and the IDE
I do all my programming for the GfxRacer XNA project using Visual Studio 2010.  At some point early in the process I bumped into effect (.fx) files.
I half expected VS2010 to have basic support for hlsl/.fx files.  I was unpleasantly surprised to find no built in support, and was unable to find any plugins/addons to give even basic syntax highlighting.






I dug around and found I could tell c# that the .fx file is really a "C# type" file, and I get back *decent* syntax highlighting (but not that great).


The main downside though is the errors that are generated at compile time.  The build will still compile fine when there are no errors.  But as soon as a real error creeps in (in the .fx file or any other files) it's obscured by the dozens of false-errors.






HLSL Editors: The Usual Suspects
Now, I have fxComposer and RenderMonkey, but I find downsides to the huge footprint, and propriatary file formats wrapped around what I really need (just the bare .fx file).
When I'm experimenting they're great though.


Notepad++
I won't go into details, but it's a great, lightweight text editor tool.
Now, natively it doesn't have specific HLSL support.  However, it's simple enough to add what Notepad++ calls a UserLanguage, and this can do a rather good job.


HLSL in Notepad++
I downloaded the HLSL UserLanguage from here:
http://www.enchantedage.com/node/97
and added the .xml file contained in the zip to my %appdata%\Notepad++\ folder






And the language will show up in Notepad++ under Languages




Lastly, you'll want to turn on Autocomplete in Notepad++, I'm using the following settings


Settings->Preferences


Now as you're editing or creating a .fx file, Notepad++ will suggest completions as you type:






That's it for Notepad++, opening a .fx file should now autodetect as a HLSL language format file and give you autocompletion tips as you type!


Back to the IDE
So that's all well and good you're saying, I have a decent HLSL editor, but what about my IDE?
Good question.  Fortunately VS2010 knows it can't do the best on everything for you, so it allows you to override the way it opens particular file types.
Just right click a .fx file in your project, select Open With..., add Notepad++ and set it as the default tool to use to open files of this type.




That's it!  The last thing I did was set the font in Notepad++ to match what I'm using in my IDE.  If you're particular, you can probably find a color scheme to match what you have in VS2010, but I'm not that picky.


Settings->Style Configurator

Wednesday, December 15, 2010

Imposters - quick rendering for complex models

Blue Spruce Tree Asset

I've been looking to fill in some assets for the game as of late.  A key piece to adding "beauty" and complexity to the scenery is vegetation.
Vegetation is also the primary reason I was looking into geometry instancing.  Trees seem to weigh in at 1000+ (up to ~40k) polygons for trunk and leaves, and I don't think that load of polygons will scale well performance wise.
So I've been researching using Imposters to improve performance.


Imposter example (1)

"Dynamic" and "Static" Cached Imposters
Before I get too far, to clarify, I've seen primarily two flavors of imposters, I'm going to label them "dynamic cached" and "static cached".
An example of dynamic cached imposters can be found here: http://www.gamasutra.com/view/feature/2501/dynamic_2d_imposters_a_simple_.php?print=1
That said, I don't plan to use "dynamic cached", I plan to use "static cached" imposters, caching the generation of the imposters at a fixed resolution and regular angular intervals, so tha many instanced models can use the same imposter data.
The end result would be something similar to what's used here:
(example of "static cached" imposters)
http://blog.wolfire.com/2010/10/Imposters

Color, Normal, and Shadow, Packed Texture Data (2)

Differences:  I don't plan to pack any baked shadows/shading info.  But I am thinking of packing in the z-buffer value or the full 3d value, scaled, to try to use with Shadowmaps (not yet sure if it'd be accurate enough, given angle error of the imposter, see http://www.dgp.toronto.edu/~schubert/imposter.html for more information of error introduced by angle error.)


Reference (Correct) View (3)
Angle Error Stretching  (3)
Conclusion
The next step is to throw together a shader for creating the imposters, and another for rendering imposters.  (At the moment I'm not planning to transition between imposter and real object, it's either one or the other.)

It's probably not a bad idea to generate the imposter data offline, and have the shader for rendering imposters have inputs for the data.  This allows for use of an editor (e.g. 3dsMax) with explicit control over use of imposters in-scene, as imposters, with correct shader, etc.
(The model would be simply a quad, which I'm ok if it's not instanced.)

Sources:
1. http://www.gamasutra.com/view/feature/2501/dynamic_2d_imposters_a_simple_.php?print=1
2..http://blog.wolfire.com/2010/10/Imposters
3.http://www.dgp.toronto.edu/~schubert/imposter.html

Monday, December 13, 2010

Content Importing in XNA

Geometry Importers and Lights


So, I spent some time looking into the geometry content importers that come standard with XNA.
There are just two - the FBX importer (documentation says they support version 2006.11), and the .X importer.


The X importer in XNA
I've used exporters from 3dsMax 2011, and have tried both binary and ascii versions.  
Viewing the ascii data, it appears the .X file format has slightly fuller support in XNA, but it doesn't seem to support Lights at all (as a data format).  It wraps them in "frames", but doesn't export anything but a matrix, and a "comment", another matrix with no useful data in it.




So lights aren't imported in the .X format... but their transformations are.  Unfortunately that's *all* that's imported.  So you can have a position and direction, but no additional info e.g. color, intensity, attenuation, or shadowmap parameters.


The FBX Format in XNA
The .FBX file format seems to export a lot more information in a structured format (including designations of 'Light' vs object, and all the related properties, color, intensity, attenuation, shadowmap settings).  However XNA's content processor ignores any and all Light data.




(the closest thing to "light" data imported into XNA from FBX is target-type light-target's matrix loaded as one of the model's bones)


Conclusion: both the built in importers *suck* for trying to load in "complete scene" sort of data.


---
So back to looking for how to coax the information out.  I've found an example custom importer at 
http://create.msdn.com/en-US/education/catalog/sample/custom_model_importer
and a custom model format (I'll probably also want it, but maybe don't yet need it) at
http://create.msdn.com/en-US/education/catalog/sample/custom_model_class


So, I think I'll look around to see if anyone's created a more usable Custom Content Importer.  In the meantime, I'm starting to lean towards writing a custom exporter for 3dsmax (yuck!) if only to export Light and Entity information as, say, a xml file (XNA has a built in general purpose xml content importer).


Hmm, but that's all kind of backburner stuff anyway I guess, it kind of fell out of the "Model" class, and checking into instancing support.


Geometry Instancing
Btw, it looks like in theory you could have instancing of Meshes by pointing to the same vertex buffer, offset and length.  In practice the .X exporter for 3dsMax doesn't seem to support it (have to double check).  The FBX format seems to have support for it, but I'm not holding my breath that the XNA importer honors it/implements it correctly.