Sign in to follow this  
Matthew Meeks

Constant Buffers Disappearing

Recommended Posts

I'm trying to make a particle system where I send a pointlist and expand quads from it.  I'd done this years ago but lost the code.  Everything seems to be fine except that the constant buffers (2 of them at 0 and 1) are somehow considered unreferenced for that draw call.  I've also noticed that the same happens when drawing sprites, though I had not noticed until now because I had no need of the constant buffers then.

 

I've tried taking out the geometry shader, using triangle lists, moving the order at which it is rendered, and setting the constant buffers to the vertex shader immediately before calling draw on the device context, and still no luck.  I'm using the Nsight plugin for Visual Studio to debug.  All the buffers contain the correct data, and every shader except the vertex shaders used for sprites and for the particles lists the constant buffers correctly.  I've checked the output with the Debug flag enabled and nothing appears between the time I prepare the particles to be rendered and after the call has been sent.

 

Only thing that I can think of is that it has something to do with the hlsl compiler not defining the buffers as referenced somehow.

 

Here's the code that renders (should render) the particles:

internal override void Render()
{
    //So I can tell where this begins in the output window
    System.Diagnostics.Debug.WriteLine("BEGIN PARTICLE");
    //base.Render is an empty method at the moment
    base.Render();

    if (buffer.Elements == 0)
        return;

    var o = Renderer.Context.InputAssembler.PrimitiveTopology;
    Renderer.Context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.PointList;

    Renderer.SetPSRes(ResLibrary.GetTex2D(Texture), 0);
    Renderer.Set(vs);
    Renderer.Set(fs);
            
    //storing the current geometry shader so that it can be set back after
    //probably will just remove this and set the geometry shader back to none
    var ogs = Renderer.Context.GeometryShader.Get();

    Renderer.Set(gs);

    //Setting the constant buffers.  These should already be set but just making sure.
    ShaderGlobals.SetWorld(Matrix.Identity);
    ShaderGlobals.UpdateCamera(Renderer.Scene.CurrentCamera.GetLocation(), Renderer.Scene.CurrentCamera.View, Renderer.Scene.CurrentCamera.Projection);

    Renderer.Set(buffer, 0, 0);
    Renderer.Draw(buffer.Elements, 0);

    Renderer.Context.GeometryShader.Set(ogs);

    Renderer.Context.InputAssembler.PrimitiveTopology = o;
    System.Diagnostics.Debug.WriteLine("END PARTICLE");
}

Here are the vertex shaders that cause problems:

 

Particle Vertex Shader:

cbuffer MatrixBuffer: register(b0)
{
    matrix G_View;
    matrix G_Projection;
    float3 G_CamPos;
    float G_gt;
};

cbuffer WorldBuffer: register(b1)
{
    matrix G_World;
    float3x3 G_World3x3;
};

struct VertexInputType
{
    float4 position : POSITION;
    float4 color : COLOR;
    float2 texcoord0 : TEXCOORD0;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
    float2 texcoord0 : TEXCOORD0;
};

PixelInputType VS_Shader(VertexInputType input)
{
    PixelInputType output;
    
    output.position = mul(input.position, G_World);
    output.position = mul(output.position, G_View);
    output.position = mul(output.position, G_Projection);
    output.position = input.position;
    
    output.texcoord0 = input.texcoord0;
	
    output.color = input.color;
    
    return output;
}

Sprite Vertex Shader:

cbuffer MatrixBuffer: register(b0)
{
    matrix G_View;
    matrix G_Projection;
    float3 G_CamPos;
    float G_gt;
};

cbuffer WorldBuffer: register(b1)
{
    matrix G_World;
    float3x3 G_World3x3;
};

struct VertexInputType
{
    float4 position : POSITION;
    float4 color : COLOR;
    float2 texcoord0 : TEXCOORD0;
};

struct PixelInputType
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
    float2 texcoord0 : TEXCOORD0;
};

PixelInputType VS_Shader(VertexInputType input)
{
    PixelInputType output;
    
    input.position.w = 1.0f;
    output.position = input.position;
    
    output.texcoord0 = input.texcoord0;
    output.color = input.color;
    
    return output;
}

Any help at all is appreciated.  I've looked at this for several hours now trying to figure anything out and I'm out of ideas.

Also if there's any more information I can give that might help let me know.

Edited by Matthew Meeks

Share this post


Link to post
Share on other sites
How are you setting up your constant buffer? Are you filling it with DeviceContext.MapSubresource and are you attaching it to the shader via DeviceContext.VertexShader.SetConstantBuffer?
Is it disappearing randomly during the program, or before the very first frame? Edited by fastcall22

Share this post


Link to post
Share on other sites

How are you setting up your constant buffer? Are you filling it with DeviceContext.MapSubresource and are you attaching it to the shader via DeviceContext.VertexShader.SetConstantBuffer?
Is it disappearing randomly during the program, or before the very first frame?

Yes to both of the first questions.  It shows as referenced for every draw call except for the specific ones with those vertex shaders set.  The buffers still show up as being set, just not referenced by the shaders.

 

Remove MatrixBuffer and WorldBuffer from the pixel shader. They aren’t referenced.


L. Spiro

I'm not exactly sure what you mean.  I didn't really mention anything about a pixel shader.  If it helps, I've tried adding the same constant buffers to the pixel shader and in the vertex, geometry, and pixel shader each shows unreferenced, yet in the calls directly before, using different shaders with the exact same code for declaring the constant buffers, everything is fine.

Share this post


Link to post
Share on other sites

I'm not exactly sure what you mean.  I didn't really mention anything about a pixel shader.

The last shader you posted does not reference MatrixBuffer or WorldBuffer.
That is exactly what the compiler is saying.
Remove them from that shader.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

Okay, but the first one does and it is unreferenced there as well.

No it’s not.
Both are referenced.

Show proof that you are getting that specific error on that specific file.
#1: Show the error (screenshot if necessary), don’t describe it.
#2: Show proof that the error is referring to that file and not some other.
#3: Show proof that VS_Shader() is referenced in that shader.

On an unrelated note—
Bad:
    output.position = mul(input.position, G_World);
    output.position = mul(output.position, G_View);
    output.position = mul(output.position, G_Projection);
    output.position = input.position;
Good:
    output.position = mul(output.position, G_WorldViewProjection);


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites


On an unrelated note—
Bad:
output.position = mul(input.position, G_World);
output.position = mul(output.position, G_View);
output.position = mul(output.position, G_Projection);
output.position = input.position;Good:
output.position = mul(output.position, G_WorldViewProjection);

 

Yeah, I had just changed that before posting and posted that before changing it back.  Anyways, I changed the name of the shader from VS_Shader to another name so that it wasn't the same as the other that didn't use them and now it works fine.  I didn't realize the name of the shader entry function would matter.

Share this post


Link to post
Share on other sites

If they are completely separate shader files and linker units (keeping it simple, both fully separate and contained compilations) it won’t matter.


L. Spiro

That's what I thought.  I must have changed something somewhere else and not realized it had fixed it.  Thanks for your help.

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