Problems passing an XMFLOAT4 to a shader

Started by
9 comments, last by DividedByZero 9 years, 9 months ago
Hi Guys,

I am trying to pass a float to a shader to control the alpha channel.

The shader works fine if I hard code a value, but I am having trouble getting the float to the shader via my application.

I have setup an XMFLOAT4 to a constant buffer and have set all of the values XMFLOAT4 to 0.5 (although I should only have to set the first one).

But for some reason it isn't working.

This is the shader (snipped a bit)


cbuffer cbChangesAlpha : register(b3)
{
	float4 image_alpha;
}

float4 PShader( PS_Input frag ) : SV_TARGET
{
    float4 fRet = colorMap_.Sample( colorSampler_, frag.tex0 );
    fRet.a = image_alpha.w;		// works if I hard code a value
    return fRet;
}
And this is my in-app code


// setting up the XMFLOAT4 constant buffer

	ZeroMemory(&constDesc,sizeof(constDesc));
	constDesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
	constDesc.ByteWidth=sizeof(XMFLOAT4);
	constDesc.Usage=D3D11_USAGE_DEFAULT;

	if(FAILED(d3dDevice->CreateBuffer(&constDesc,0,&floatCB)))
		return 11114;
and part of my render loop


	XMFLOAT4 image_alpha;
	image_alpha.w=0.5;
	image_alpha.x=0.5;
	image_alpha.y=0.5;
	image_alpha.z=0.5;

	d3dContext->UpdateSubresource(floatCB,0,0,&image_alpha,0,0);
	
	d3dContext->VSSetConstantBuffers(3,1,&floatCB);

All complies and executes ok but I just get a black screen. If I hard code the shader to be a value between 0 & 1 it works correctly. So, it seems that the image_alpha values are not getting to the shader correctly.

Any advice would be awesome smile.png
Advertisement

I think the reason is you are not adding D3D11_CPU_ACCESS_WRITE flag to your constant buffer. Since you are writing data from CPU you need to use that flag.

The buffer does not have CPU-write access.
constDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
The buffer cannot be written because it is in the default pool.
constDesc.Usage=D3D11_USAGE_DYNAMIC;

L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Thanks guys,

I now have this but I get the same result (hard coded in shader is fine though).


	ZeroMemory(&constDesc,sizeof(constDesc));
	constDesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
	constDesc.ByteWidth=sizeof(XMFLOAT4);
	constDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
	constDesc.Usage=D3D11_USAGE_DYNAMIC;
I just noticed in debug mode I am getting this

Can only invoke UpdateSubresource when the destination Resource was created with D3D11_USAGE_DEFAULT and is not a multisampled Resource. The destination Resource has (1) samples. [ RESOURCE_MANIPULATION ERROR #289: UPDATESUBRESOURCE_INVALIDDESTINATIONSTATE]

[edit]
Actually, I just checked with my original code and debug mode doesn't throw any errors (just doesn't work though).

cbuffer cbChangesAlpha : register(b3)


What's register "b3"? Which shader model are you using? All I could find was a constant boolean register, in shader model 3. I think "cb#" is used for constant buffers.

Anyway, why are you starting at 3? I have only played around with shaders a bit as a hobby, but I generally just leave out the register stuff and stick the constant buffer in slot 0.

I'd try changing this register thing, setting the CPU access flag, and leaving it as default usage.
I am using b3 as registers 0-2 are being used

Texture2D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );

cbuffer cbChangesEveryFrame : register(b0)
{
	matrix worldMatrix;
};

cbuffer cbNeverChanges : register(b1)
{
	matrix viewMatrix;
};

cbuffer cbChangeOnResize : register(b2)
{
	matrix projMatrix;
}

cbuffer cbChangesAlpha : register(b3)
{
	float4 image_alpha;
}
Shortly, I'll be combining them into one register (once I get things working as planned).

This is one of the few areas in DX11 where the same operation can be performed in 2 ways:

UpdateSubresource

You need these flags:

cbDesc.Usage = D3D11_USAGE_DEFAULT;
cbDesc.CPUAccessFlags = 0;
Map/Unmap
cbDesc.Usage = D3D11_USAGE_DYNAMIC;
cbDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
I have benchmarked these 2 and got no substantial difference.. so in my code I am using Map/Unmap simply because it looks more clear to me, especially the buffer creation flags.
The code is something like:
D3D11_MAPPED_SUBRESOURCE MappedResource;
context->Map(buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource);
memcpy(MappedResource.pData, bdata, bsize);
context->Unmap(buffer, 0);
Instead of a single call to UpdateSubresource.
I think your problem might be that you are binding the CB for the VS, if you are using it in your PS, you need PSSetConstantBuffers.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

Thanks kunos,

At the moment I am using the former method.

It is strange as I am able to send Matrix information to the shader happily enough (for positioning etc), but sending the XMFLOAT4 just seems to refuse to work sad.png

This is what I have right now.


ZeroMemory(&constDesc,sizeof(constDesc));
constDesc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
constDesc.ByteWidth=sizeof(XMFLOAT4);
constDesc.CPUAccessFlags = NULL;
constDesc.Usage=D3D11_USAGE_DEFAULT;

if(FAILED(d3dDevice->CreateBuffer(&constDesc,0,&floatCB)))
	return 11114;

	XMFLOAT4 image_alpha;
	image_alpha.x=0.5;
	image_alpha.y=0.5;
	image_alpha.z=0.5;
	image_alpha.w=0.5;

d3dContext->UpdateSubresource(worldCB,0,0,&worldMat,0,0);
d3dContext->UpdateSubresource(viewCB,0,0,&viewMatrix,0,0);
d3dContext->UpdateSubresource(projCB,0,0,&projMatrix,0,0);
d3dContext->UpdateSubresource(floatCB,0,0,&image_alpha,0,0);
	
d3dContext->VSSetConstantBuffers(0,1,&worldCB); // THESE WORK
d3dContext->VSSetConstantBuffers(1,1,&viewCB); // THESE WORK
d3dContext->VSSetConstantBuffers(2,1,&projCB); // THESE WORK
d3dContext->VSSetConstantBuffers(3,1,&floatCB); // THIS DOESN'T

Texture2D colorMap_ : register( t0 );
SamplerState colorSampler_ : register( s0 );

cbuffer cbChangesEveryFrame : register(b0)
{
	matrix worldMatrix;
};

cbuffer cbNeverChanges : register(b1)
{
	matrix viewMatrix;
};

cbuffer cbChangeOnResize : register(b2)
{
	matrix projMatrix;
}

cbuffer cbChangesAlpha : register(b3)
{
	float4 image_alpha;
}

struct VS_Input
{
	float4 pos  : POSITION;
	float2 tex0 : TEXCOORD0;
};

struct PS_Input
{
	float4 pos  : SV_POSITION;
	float2 tex0 : TEXCOORD0;
};

PS_Input VShader( VS_Input vertex )
{
	PS_Input vsOut = ( PS_Input )0;

	vsOut.pos=mul(vertex.pos,worldMatrix);
	vsOut.pos=mul(vsOut.pos,viewMatrix);
	vsOut.pos=mul(vsOut.pos,projMatrix);

	// vsOut.pos = vertex.pos;
	vsOut.tex0 = vertex.tex0;

	return vsOut;
}

float4 PShader( PS_Input frag ) : SV_TARGET
{
    float4 fRet = colorMap_.Sample( colorSampler_, frag.tex0 );
    fRet.a = image_alpha.x;		// works if I hard code a value
    return fRet;
}
Still very confused as to why this doesn't work.

And debug mode doesn't show any errors in this case either.

Oh maybe you have missed my edit.

You need to bind the CB to the PS with PSSetConstantBuffers because that is where you are using image_alpha.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

cbuffer cbChangesAlpha : register(b3)


What's register "b3"? Which shader model are you using? All I could find was a constant boolean register, in shader model 3. I think "cb#" is used for constant buffers.

Anyway, why are you starting at 3? I have only played around with shaders a bit as a hobby, but I generally just leave out the register stuff and stick the constant buffer in slot 0.

I'd try changing this register thing, setting the CPU access flag, and leaving it as default usage.

"cb" is only used for constant buffers when shader compatibility mode is enabled for D3D10+ shaders. Otherwise "b" registers are used for constant buffers.

This topic is closed to new replies.

Advertisement