• Advertisement
Sign in to follow this  

Problems passing an XMFLOAT4 to a shader

This topic is 1335 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

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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). Edited by DarkRonin

Share this post


Link to post
Share on other sites

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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.

Edited by kunos

Share this post


Link to post
Share on other sites

 

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.

Share this post


Link to post
Share on other sites

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.


Nice job! That was exactly it. smile.png

I don't think I would have ever picked that one up.

Thanks again kunos!

Share this post


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

  • Advertisement