• 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
Christoffer Enne

DirectX9 Hardware Instancing. Draws only one instance?

7 posts in this topic

Hi

This is my first post on this site. I used to be at App Hub but I resently started working directly to DirectX and the DirectX activity there is close to none thats why I joined here so that I might have someone to talk to. But enoght about that and on to my question.

For a few days I have tried to get hardware instancing to work but with no luck. I have googled a lot and read everything on the topic that I can find. Everything compiles and It draws one of the instances where only one of the vertrices are affected by the world coordinate. I'm at the point where I want to throw the computer out the window so please have a look at this and help me if you can!

Here is the instancing parts of my code.

private int _instanceCount;

private InstanceFormat[] _instances;

private VertexBuffer _instanceBuffer;

private int _vertexCount;

private VertexFormat[] _vertices;

private VertexBuffer _vertexBuffer;

private int _indexCount;

private uint[] _indices;

private IndexBuffer _indexBuffer;

private D3D.Effect _instancingEffect;

private VertexDeclaration _vertexDeclaration;

int vertexSize;

int instanceSize;

struct VertexFormat

{

public Vector3 Position;

public Vector2 Texture;

public VertexFormat(Vector3 position, Vector2 texture)

{

Position = position;

Texture = texture;

}

}

struct InstanceFormat

{

public Vector3 World;

public InstanceFormat(Vector3 world)

{

World = world;

}

}


This is where I create my Declaration and fill the buffers:
#region Set up vertex declaration

vertexSize = 20;

instanceSize = 12;

VertexElement[] vertexElementsArray = new VertexElement[]

{

new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default, DeclarationUsage.Position, 0),

new VertexElement(0, 12, DeclarationType.Float2, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 0),

new VertexElement(1, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 1),

VertexElement.VertexDeclarationEnd

};

// Create a vertex declaration based on the vertex elements.

_vertexDeclaration = new VertexDeclaration(_device, vertexElementsArray);

#endregion

#region Set up vertex buffer for vertices

_vertexCount = 4;

_vertexBuffer = new VertexBuffer(_device, vertexSize*_vertexCount, Usage.Dynamic | Usage.WriteOnly, VertexFormats.Position | VertexFormats.Texture0, Pool.Default);



_vertices = new VertexFormat[_vertexCount];

_vertices[0] = new VertexFormat(new Vector3(0, 0, 0), new Vector2(0, 0));

_vertices[1] = new VertexFormat(new Vector3(0, 1, 0), new Vector2(0, 1));

_vertices[2] = new VertexFormat(new Vector3(1, 0, 0), new Vector2(1, 0));

_vertices[3] = new VertexFormat(new Vector3(1, 1, 0), new Vector2(1, 1));

_vertexBuffer.SetData(_vertices, 0, LockFlags.None);

#endregion

#region Set up index buffer

_indexCount = 6;

_indexBuffer = new IndexBuffer(_device, sizeof(uint)*_indexCount, Usage.WriteOnly, Pool.Default, false);

_indices = new uint[_indexCount];

_indices[0] = 0; _indices[1] = 1; _indices[2] = 2;

_indices[3] = 1; _indices[4] = 3; _indices[5] = 2;

_indexBuffer.SetData(_indices, 0, LockFlags.None);

#endregion

#region Set up vertex buffer for instances

_instanceCount = 10;

_instanceBuffer = new VertexBuffer(_device, instanceSize * _instanceCount, Usage.Dynamic | Usage.WriteOnly, VertexFormats.None, Pool.Default);



_instances = new InstanceFormat[_instanceCount];

for (int i = 0; i < _instanceCount; i++)

_instances[i] = new InstanceFormat(new Vector3(i, 0, i));

_instanceBuffer.SetData(_instances, 0, LockFlags.None);



#endregion


And this is my draw method:
_device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.DarkSlateBlue, 1.0f, 0);

_device.BeginScene();

_device.VertexDeclaration = _vertexDeclaration;

_device.Indices = _indexBuffer;

_device.SetStreamSource(0, _vertexBuffer, 0, vertexSize);

_device.SetStreamSource(1, _instanceBuffer, 0, instanceSize);

// Specify how many times the vertex stream source and the instance stream source should be rendered.

_device.SetStreamSourceFrequency(0, _instanceCount);

_device.SetStreamSourceFrequency(1, 1);

_instancingEffect.Technique = "Instancing";

_instancingEffect.SetValue("WVP", _camera.view * _camera.projection);

_instancingEffect.SetValue("cubeTexture", _textures[1]);

int numpasses = _instancingEffect.Begin(0);

for (int i = 0; i < numpasses; i++)

{

_instancingEffect.BeginPass(i);

_device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _vertexCount, 0, _indexCount / 3);

_instancingEffect.EndPass();

}

_instancingEffect.End();

_device.SetStreamSourceFrequency(0, 1);

_device.SetStreamSourceFrequency(1, 1);

_device.EndScene();

_device.Present();



And here is my shader:
float4x4 WVP;

texture cubeTexture;

sampler TextureSampler = sampler_state

{

texture = <cubeTexture>;

mipfilter = LINEAR;

minfilter = LINEAR;

magfilter = LINEAR;

};

struct InstancingVSinput

{

float4 Position : POSITION0;

float2 TexCoord : TEXCOORD0;

float4 World : TEXCOORD1;

};



struct InstancingVSoutput

{

float4 Position : POSITION0;

float2 TexCoord : TEXCOORD0;

};



InstancingVSoutput InstancingVS(InstancingVSinput input)

{

InstancingVSoutput output;

// Transform the vertex into world coordinates.

float4 pos = input.Position + input.World;

// Transform the vertex from world coordinates into screen coordinates.

output.Position = mul(pos, WVP);

output.TexCoord = input.TexCoord;

return output;

}



float4 InstancingPS(InstancingVSoutput input) : COLOR0

{

return float4(1,1,1,1);// tex2D(TextureSampler, input.TexCoord);

}



technique Instancing

{

pass Pass0

{

VertexShader = compile vs_3_0 InstancingVS();

PixelShader = compile ps_3_0 InstancingPS();

}

}


Edit: I should probably mention that I have a Radeon HD 5770.
0

Share this post


Link to post
Share on other sites
I think i know what the issue is. In your vertex shader input you are defining you world matrix as your input.
This is an array that you build - for example it contains your position, scale, and rotation for each "instance" in your world.

What you need to do is input it like this.



[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] Position [/size][/font][/size][/font][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"]: POSITION[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]; [/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float2[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] TexCoord [/size][/font][/size][/font][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"]: TEXCOORD0[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]; [/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] W0 [/size][/font][/size][/font][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"]: TEXCOORD1[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]; [/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] W1 [/size][/font][/size][/font][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"]: TEXCOORD2[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]; [/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] W2 [/size][/font][/size][/font][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"]: TEXCOORD3[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]; [/size][/font][/size][/font]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] W3 [/size][/font][/size][/font][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"][font="Consolas"][size="2"][color="#008080"]: TEXCOORD4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"];[/size][/font][/size][/font]


[font="Consolas"][size="2"][font="Consolas"][size="2"]and of course you must have a compatable vertex declaration and you can construct the world positions like this[/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4x4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] object = [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4x4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](input.W0, input.W1, input.W2, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](input.W3.xyz, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#000080"][font="Consolas"][size="2"][color="#000080"][font="Consolas"][size="2"][color="#000080"]1.0f[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]));[/size][/font][/size][/font][/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4x4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] objectworld = [/size][/font][/size][/font][b][font="Consolas"][size="2"][color="#800000"][font="Consolas"][size="2"][color="#800000"][font="Consolas"][size="2"][color="#800000"]mul[/color][/size][/font][/color][/size][/font][/color][/size][/font][/b][font="Consolas"][size="2"][font="Consolas"][size="2"](object, world); [/size][/font][/size][/font][/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4x4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] WVP = [/size][/font][/size][/font][b][font="Consolas"][size="2"][color="#800000"][font="Consolas"][size="2"][color="#800000"][font="Consolas"][size="2"][color="#800000"]mul[/color][/size][/font][/color][/size][/font][/color][/size][/font][/b][font="Consolas"][size="2"][font="Consolas"][size="2"](objectworld, [/size][/font][/size][/font][b][font="Consolas"][size="2"][color="#800000"][font="Consolas"][size="2"][color="#800000"][font="Consolas"][size="2"][color="#800000"]mul[/color][/size][/font][/color][/size][/font][/color][/size][/font][/b][font="Consolas"][size="2"][font="Consolas"][size="2"](view, proj)); [/size][/font][/size][/font][/size][/font][/size][/font]
[font="Consolas"][size="2"][font="Consolas"][size="2"][font="Consolas"][size="2"][font="Consolas"][size="2"]output.Position = [/size][/font][/size][/font][b][font="Consolas"][size="2"][color="#800000"][font="Consolas"][size="2"][color="#800000"][font="Consolas"][size="2"][color="#800000"]mul[/color][/size][/font][/color][/size][/font][/color][/size][/font][/b][font="Consolas"][size="2"][font="Consolas"][size="2"]([/size][/font][/size][/font][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]float4[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"](input.Position.xyz, [/size][/font][/size][/font][font="Consolas"][size="2"][color="#000080"][font="Consolas"][size="2"][color="#000080"][font="Consolas"][size="2"][color="#000080"]1.0f[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]), WVP);[/size][/font][/size][/font][/size][/font][/size][/font]

[font="Consolas"][size="2"][font="Consolas"][size="2"]This is probably the reason behind the reason its "working" but you only see the original mesh and not any of its children.[/size][/font][/size][/font]
0

Share this post


Link to post
Share on other sites
It's been a while that I have worked with D3D9 level stuff but here's another thing:


[color=#000000] _device[/color][color=#666600].[/color][color=#660066]SetStreamSourceFrequency[/color][color=#666600]([/color][color=#006666]0[/color][color=#666600],[/color][color=#000000] _instanceCount[/color][color=#666600]);[/color]
[color=#000000] _device[/color][color=#666600].[/color][color=#660066]SetStreamSourceFrequency[/color][color=#666600]([/color][color=#006666]1[/color][color=#666600],[/color][color=#000000] [/color][color=#006666]1[/color][color=#666600]);[/color]

shouldn't this be

_device.SetStreamSourceFrequency(0, D3DSTREAMSOURCE_INDEXDATA | _instanceCount);
_device.SetStreamSourceFrequency(0, D3DSTREAMSOURCE_INSTANCEDATA | 1);


Remember to restore the correct values after using instancing as you are already doing.

Best regards! Edited by kauna
2

Share this post


Link to post
Share on other sites
David w: I don't really have the need of a matrix because there is no scaling and rotation done to the model. I only want to place it on different coordinates and thats why I only input a vector3.

kauna: Well my program does not recognise D3DSTREAMSOURCE_INDEXDATA or D3DSTREAMSOURCE_INSTANCEDATA. What reference and using do I need for this? Edited by CptBonex
0

Share this post


Link to post
Share on other sites
Oh sorry, seems that XNA instancing works differently than instancing under D3D9.

Cheers!
0

Share this post


Link to post
Share on other sites
I have done Instancing in xna and I dont remember using them and I have seen a few examples where those are used but I cant access them.
0

Share this post


Link to post
Share on other sites
Kauna's right, this is the way to enable instancing in D3D 9.
These constants are defined in d3dtypes.h:

[code]
#define D3DSTREAMSOURCE_INDEXEDDATA (1<<30)
#define D3DSTREAMSOURCE_INSTANCEDATA (2<<30)
[/code]

Whether Managed DirectX exposes these values somewhere eludes me. Other managed libraries like XNA/SharpDX/SlimDX either do or come with some convenience functions for instancing.

If you want to stick with Managed DirectX and don't find these constants you could as well define them yourself.
I think that [i]should[/i] work then.
1

Share this post


Link to post
Share on other sites
Yes it worked!!! Thank you guys so mutch!

I did this:
[CODE]
static class Constants
{
public const int D3DSTREAMSOURCE_INDEXEDDATA = (1 << 30);
public const int D3DSTREAMSOURCE_INSTANCEDATA = (2<<30);
}
[/CODE]

and used them like this:
[CODE]
_device.SetStreamSourceFrequency(0, Constants.D3DSTREAMSOURCE_INDEXEDDATA | _instanceCount);
_device.SetStreamSourceFrequency(1, Constants.D3DSTREAMSOURCE_INSTANCEDATA | 1);
[/CODE]
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