Jump to content
  • Advertisement
Sign in to follow this  
MasterChief117

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.

If you intended to correct an error in the post then please contact us.

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:

 

Shader:

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 vertexShader(Vertex input)
{
 Pixel result = (Pixel) 0;
 result.Position = TransformVertex(input);

 return result;
}

float4 pixelShader(Pixel input) : SV_Target0
{
 return float4(DiffuseColor.rgb, 1.0f);
}

technique11 Main
{
 pass p0
 {
  SetVertexShader(CompileShader(vs_5_0, vertexShader()));
  SetHullShader(NULL);
  SetDomainShader(NULL);
  SetGeometryShader(NULL);
  SetPixelShader(CompileShader(ps_5_0, pixelShader()));
 }
}

Initialize Shader:

            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 this post


Link to post
Share on other sites
Advertisement
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.

Edited by Adam Miles

Share this post


Link to post
Share on other sites

Hi Adam Miles,

 

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 this post


Link to post
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.
Edited by Adam Miles

Share this post


Link to post
Share on other sites

Hi Adam Miles,

 

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Edited by Adam Miles

Share this post


Link to post
Share on other sites

Hi Adam Miles,

 

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

 

Now i had wrote:

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

 

And the result with my Cube is horrible :(

 

01.png

 

Greets

John

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!