Constant buffer mapping problem.

Started by
7 comments, last by Jason Z 11 years, 2 months ago

RESOLVED : For the newbie's purpose, the problem was simply that i thought padding could occur anywhere (in this case at the end of the struct). But the packing has to be done every 16 bytes involving padding in the middle of the struct.

Hi,

I'm new to d3d11 so i apologies for this newbie question.

I'm trying to map data to several cbuffer but it seems that only the first (register(cb0)) cbuffer is actually getting correct data. I have no control what so ever on what is written in the next ones.

The problem appears only in the pixel shader. I've tried to add an extra cbuffer in the vertex shader and there's i no problem.

I think i have correctly aligned data to 16 bytes packing.

I use the same MatrixBuffer for both vertex shader and pixel shader.

Map() call sequence order is irrelevant and doesn't change anything.

EDIT : I've managed to reduce code to the following snippet. I'm using same buffer for cbuffer TestBuffer. And it works in the vertex shader but not in the pixel shader.

EDIT 2 : If i set TestBuffer : register(cb0) and MatrixBuffer : register(cb1) in the pixel shader i now get this strange compile error :

+ compile_error 0x001b80e8 "error X4567: maximum cbuffer exceeded. target has 14 slots, manual bind to slot 4294967295 failed\n"

VertexShader :



// Typedefs
//------------------------------------------------
struct VertexInputType
{
    float4 position : POSITION;
	float3 normal   : NORMAL;
};

struct PixelInputType
{
    float4 v_position : SV_POSITION;
	float3 w_normal   : NORMAL;
	float3 w_position : TEXCOORD0;
};

// Globals
//------------------------------------------------
cbuffer MatrixBuffer
{
	float4x4 world;
	float4x4 view;
	float4x4 projection;
};

cbuffer TestBuffer
{
	float4 test;
}

// Shader
//------------------------------------------------
PixelInputType SPMLVShader(VertexInputType input)
{
    PixelInputType output;
	output.v_position = input.position;
	output.w_normal = input.normal;

    // Change the position vector to be 4 units for proper matrix calculations.
	input.position.w = 1.0f;
	
	// Calculate the position of the vertex in world coordinates.
	output.w_position = mul(input.position, world);

    // Calculate the position of the vertex against the world, view, and projection matrices.
    output.v_position.xyz = output.w_position.xyz;
    output.v_position = mul(output.v_position, view);
    output.v_position = mul(output.v_position, projection);
    
    // Calculate the normal vector against the world matrix only.
    output.w_normal = mul(input.normal, (float3x3)world);
	
    // Normalize the normal vector.
    output.w_normal = normalize(output.w_normal);
	
	// CORRECT OUTPUT
	if(test.x == 222 && test.y == 223 && test.z == 224 && test.w == 225)
	{
		return output;	
	}
	
	// DO NOT DISPLAY ANYTHING
	output.v_position = float4(0, 0, 0, 0);
	return output;
}

PixelShader :



//	Typedefs
//------------------------------------------------
struct PixelInputType
{
    float4 v_position : SV_POSITION;
	float3 w_normal   : NORMAL;
	float3 w_position : TEXCOORD;
};

//	Globals
//--------------------------------------
cbuffer MatrixBuffer
{
	float4x4 world;
	float4x4 view;
	float4x4 projection;
};

cbuffer TestBuffer
{
	float4 test;
};

float4 SPMLPShader(PixelInputType input) : SV_TARGET
{
	if(test.x == 222 && test.y == 223 && test.z == 224 && test.w == 225)
	{
		return float4(0.0f, 1.0f, 0.0f, 1.0f);
	}
	else
	{
		return float4(1.0f, 0.0f, 0.0f, 1.0f);
	}
}
 

I've been searching a lot, but i have no clue yet on where it comes from.

Best regards.

Advertisement

You didn't specify registers for your cbuffers in shader code, so they probably all went into cb0.

No,

I did try with registers and yet it didn't worked. I even swapped first and second cbuffer to ensure that apparition order is irrelevant.

And PSSetConstantBuffers call sequence is irrelevant too.

Sorry, my mistake. Constant buffers go into b0 - bsomething registers.
Therefore:


cbuffer MatrixBuffer : register(b0) { ... };
cbuffer TestBuffer : register(b1) { ... };

I typically don't specify exact registers for my constant buffers. If you remove the register declarations, and then compile the shaders using FXC.exe, you can output a text listing of the results. This will show you precisely which registers are expected, as well as which type. Try this out and I think you will see where the issue lies.

I think i've reduced the problem to that :

It is simply not possible to use an array of custom struct types in a cbuffer. It doesn't seems to have anything to do with the register order.

Btw : i'd like to try the fxc.exe but i've never used it before. Could you just post the standard compile command ?

Thank you.

It depends on the name of the file, the shader main function name, the shader model being targetted, and the output file name. Something like this on the command line should do the trick:

fxc AmbientOcclusion.hlsl /T cs_4_0 /E CSMAIN /Zi /Cc /Fc AmbientOcclusion.html

That will produce an output html file that you can see the details of your shader from. I normally put this command into a batch file, since retyping it over and over gets old...

EDIT: Where are you using an array of custom structs in your CB's? I don't see that anywhere...

I get this :

error X3084: "numthreads(X, Y, Z)" attribute expected, where "X, Y, Z" are the dimensions of the thread group.

And yes, i've been turning around so much the shader's code that i simply forgot what i started with. After checking every single variable in several cbuffers i've noticed that only those with custom struct was unable to map correctly.

Btw : Thank you for your concern. I've been stuck on this for days.

That error means that you didn't declare the number of thread groups within your compute shader. Since you aren't dealing with compute shaders, that means you probably need to change the parameters on the command line that I listed above to match the type of shader you are targetting. For example, if it is a vertex shader, then you pass vs_5_0 instead of cs_4_0 from above. Also be sure to update the function's entry point name as well to match what you are targetting!

This topic is closed to new replies.

Advertisement