# Instance Rendering dosen't work :(

This topic is 864 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello Guys,

i have a big problem, i want to create an example for Instance Rendering, but the World Matricen dosen't work, only the Scale Informations comes to the Shader, the Translation and Rotation Values not :(

Here is my Code:

matrix World      : WORLD;
matrix View       : VIEW;
matrix Projection : PROJECTION;

float3 DiffuseColor = {1.0f, 0.0f, 0.0f};

struct Vertex
{
float3 Position : POSITION;
matrix instance : INSTANCE;
};

struct Pixel
{
float4 Position : SV_POSITION;
};

float4 TransformVertex(Vertex input)
{
float4 objectSpace = float4(input.Position.xyz, 1.0f);
float4 worldSpace = mul(objectSpace, (World * input.instance));
float4 viewSpace = mul(worldSpace, View);
float4 projectionSpace = mul(viewSpace, Projection);

return projectionSpace;
}

{
Pixel result = (Pixel) 0;
result.Position = TransformVertex(input);

return result;
}

{
return float4(DiffuseColor.rgb, 1.0f);
}

technique11 Main
{
pass p0
{
}
}


            this.simpleShader = new ShaderDescription(this.deviceEngine.Device, "simpleShader", "SimpleShader", @"C:\Path\SimpleShader.fx", "fx_5_0", new[]
{
new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
new InputElement("INSTANCE", 0, Format.R32G32B32A32_Float, 0, 1, InputClassification.PerInstanceData, 1),
new InputElement("INSTANCE", 1, Format.R32G32B32A32_Float, 16, 1, InputClassification.PerInstanceData, 1),
new InputElement("INSTANCE", 2, Format.R32G32B32A32_Float, 32, 1, InputClassification.PerInstanceData, 1),
new InputElement("INSTANCE", 3, Format.R32G32B32A32_Float, 48, 1, InputClassification.PerInstanceData, 1),
});


Write Matrix in Buffer:

                this.device = device;

int sizeInBytes = 64;

int totalSizeInBytes = 1 * sizeInBytes;

Matrix world = Matrix.Scaling(2, 1, 1) * Matrix.RotationYawPitchRoll(50, 0, 0) * Matrix.Translation(3, 0, 0);

DataStream vertexStream = new DataStream(totalSizeInBytes, true, true);

vertexStream.WriteRange(world.ToArray());

vertexStream.Position = 0;

this.buffer = new Buffer(device, vertexStream, new BufferDescription(totalSizeInBytes,
ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));

this.bufferBinding = new VertexBufferBinding(this.buffer, sizeInBytes, 0);


DrawInstanced:

                        this.device.SetVertexBuffer(0, this.vertexBuffer.BufferBinding);
this.device.SetVertexBuffer(1, this.matricenBuffer.BufferBinding);

this.device.DeviceContext.InputAssembler.PrimitiveTopology = mesh.VerticesType;

this.Apply(mesh.Material, world, view, projection);
this.device.DeviceContext.DrawInstanced(mesh.Vertices.Count, 1, this.startPosition, 0);


I don't get it what i make wrong, hope you guys can help me!?

Greets

John

Edited by MasterChief117

##### Share on other sites
float4 worldSpace = mul(objectSpace, (World * input.instance));

I'm not sure you wanted to do component-wise matrix multiplication. You probably meant to use 'mul' which is standard matrix multiplication.

##### Share on other sites

but "mul" do only work with a float4 and a matrix, not a matrix and a matrix, for that i have the operator * !?

I want that the new World matrix is the result of the standart World matrix (Identity) and the with given matrix from my Instance Buffer.

Greets

John

Edited by MasterChief117

##### Share on other sites

Are you telling me it doesn't work, or that you haven't tried it and you /think/ it won't work?

https://msdn.microsoft.com/en-gb/library/windows/desktop/bb509634(v=vs.85).aspx

float3x3 mat3 = mat1*mat2;

The result is a per-component multiply of the two matrices (as opposed to a standard 3x3 matrix multiply).

Overloaded versions of the multiply intrinsic function handle cases where one operand is a vector and the other operand is a matrix. Such as: vector * vector, vector * matrix, matrix * vector, and matrix * matrix.

##### Share on other sites

I had try it with "mul(World, input.Instance)" and i get an exception and I want a 4x4 matrix, not a 3x3 matrix!?

Greets

John

Edited by MasterChief117

##### Share on other sites

An exception? Where? Doing what?

Your "instance" vertex attribute doesn't have a capital I as you've written it above, so I'm going to guess you haven't checked whether the shader still compiles.

##### Share on other sites

I don't know exactly what you mean, but here the error message!?

Edited by MasterChief117

##### Share on other sites

ps. I had checked it befor with:

float3 pos =  float3(input.instance[3][0], input.instance[3][1], input.instance[3][2]);
float4 objectSpace = float4(input.Position.xyz + pos, 1.0f);


And the result was zero (the Mesh location was 0, 0, 0), but that makes no sense, because i had wrote in the Instance Buffer my Matrix with the Translation of (3, 0, 0)!?

Greets

John

##### Share on other sites

I had try it with "mul(World, input.Instance)" and i get an exception

That's not what you have written in your screenshot. You've written:

float4 worldSpace = mul(objectSpace, mul(World * input.instance));


mul is a function that takes two matrices, you've passed only one and again done per-component wise multiplication on two matrices.

What you want is:

float4 worldSpace = mul(objectSpace, mul(World, input.instance));


Note the comma denoting the fact that "mul" is an intrinsic that takes two arguments.

##### Share on other sites

sry, i thought I had wrote a comma, don't saw that still a *

 float4 worldSpace = mul(objectSpace, mul(World, input.instance));


And the result with my Cube is horrible :(

Greets

John

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632665
• Total Posts
3007709
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!