Archive

Archive for November, 2008

The SMOKE Framework finally released

November 26, 2008 Jendrik Illner 1 comment

Intel finally released the SMOKE Framework. SMOKE is a framework to support n-way threading.

For everybody not already knowing SMOKE already  have a look at the article at gamasutra here. For further insight have a look at the demo the gave at the xnagamefest you can download the recording of the presentation here. It’s really interesting and worth every bit.

They released the full source code, a demo application and documentation.
The documentation comes with fully doxygen generated class documentation. Also in the pack, the presentation of this project done at the difference conferences, just PowerPoint files no audio.

Project site:
http://software.intel.com/en-us/articles/smoke-game-technology-demo

They also released some new articles:

    Lot of interesting stuff to learn from, enough for weeks.
    I really was looking forward to this day since they first announced the SMOKE-Framework project.

    Categories: Uncategorized

    Streamline Vertex Buffer creation with Reflection

    November 22, 2008 Jendrik Illner Leave a comment

    When working with Vertex Buffers in the xna framework is end in a lot more work than it has to be.

    What is needed if we just want to create a simple vertex buffer using our own custom vertex structure?

    1. The vertex structure itself
      public struct PositionTextureTexture
      {
          public Vector3 Position;
          public Vector2 TexCoord0;
          public Vector2 TexCoord1;
      }
      
      

    2. An array of vertex elements describing the vertex structure
      VertexElement[] elements = new VertexElement[]
      {
          new VertexElement(0,0,
      VertexElementFormat.Vector3, VertexElementMethod.Default, 
      VertexElementUsage.Position,0),
          new VertexElement(0,12,
      VertexElementFormat.Vector2, VertexElementMethod.Default, 
      VertexElementUsage.TextureCoordinate,0),
          new VertexElement(0,20,
      VertexElementFormat.Vector2, VertexElementMethod.Default,
      VertexElementUsage.TextureCoordinate,0),
      };
    3. The vertex declaration

      VertexDeclaration declaration = new VertexDeclaration(
      device, elements);
    4. The vertex buffer
      VertexBuffer vertexBuffer = new VertexBuffer(
      device, sizeInBytesPerVertex * vertices.Length,
      BufferUsage.None);
      
      
      vertexBuffer.SetData(vertices);

    
    

    Is all this really necessary? Why do we have to explicitly have to define the elements of the structure a second time and have to deal with byte offsets?

    We don’t have to! .Net has the most powerful weapon of all, equipped by default,

    Reflection

    Yep reflection one of the most underestimated tools in the .net framework. When used correctly, reflection makes working at lot more easy. But we have to be aware of the performance penalty we get by using reflection, but we only create vertex buffers at load time so it doesn’t matter that much.
    So if we would use reflection what do we need to do the same as before?

    We need the the Vertex structure, of course :) .
    And one line of code

    VertexBufferFactory.CreateVertexBuffer<PositionTextureTexture>
    (this.GraphicsDevice, this.vertices, out vertexDeclaration,
     out vertexBuffer, out sizeInBytesPerVertex);
    
    
    Oh that’s magic :) 

    No it’s not. But how does it work?

    So here’s a small summary of the inner workings of the library. You can download the source and a small sample project at the bottom of the post.

    As we have seen at the begging we need a lot of information about the different vertex elements but those information are easy to get when using reflection.

    So lets start from the top. The first thing we need is the actual vertex structure. This structure doesn’t change when using this library.

    So the first step is to generate the array containing the usage of every vertex element.
    Lets take a look at what we need to know to generate one.

    new VertexElement(streamNumber, offset, elementFormat,
     VertexElementMethod.Default, elementUsage, usageIndex)

    streamNumber: it’s possible to use different vertex streams but that’s more complicated stuff so we just pass in 0.

    offset: the offset in bytes from the beginning of the stream, this is easy to calculate because reflection tells us what type is used for the field and based on this we can calculate the size.

    elementFormat: what format is used, mostly only float, vector2 vector3 or vector4, this is also based on the type used for the field in the vertex structure

    vertexElementMethod: is always set to default because it’s rarely used differently

    elementUsage: The element usage can be generated based on the name of the field. When the field name is “Position” the intended usage is clear, TexCoord0, Binormal, Tangent, and so on are also clear.

    usageIndex: The usage index is by default 0 but when the field name has a number at the end, this number is used as usage Index.

    When the generation engine was not able to generate the necessary data it throws an exception and we have to tell it what our indention is.

    This is done through the use of attributes. Also a underestimated feature in the .net framework.

    [VertexElementAttribute( VertexElementUsage.TextureCoordinate, 0)]
    public Vector3 someFactor;

    This tells the generation engine our intention.

    After we have generated all the elements successful we can just create the vertex declaration.

    The vertex buffer can be created as well because we know every element of the vertex structure, and so we know the size as well. We know the vertices so we just have to create it and set the data.

    Have a look at the code and see how it works, this library is not complete and doesn’t support everything but it’s written to be extensible so when a feature is missing for you just add it, maybe you would be so kind to send me what you changed so I can make it available for everybody to use.

    Download

    Creative Commons License
    VertexBufferFactoryLibary by Jendrik Illner is licensed under a Creative Commons Attribution 3.0 United States License.

    Categories: xna

    Further progress

    November 2, 2008 Jendrik Illner Leave a comment

    PDC time is hard, so much cool stuff and talks to listen. A new C#, .Net 4.0 and Visual Studio 2010 CTP to play with. This really is a lot of fun. Especially “statically typed to be dynamic”(Anders Hejlsberg) made my day. But I also made some progress on my small D3D10 Framework.
    I added a support for texture mapping, lightning and depth buffers.

    image

    Categories: D3D10, Jen3D, Uncategorized