Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Problems passing an XMFLOAT4 to a shader


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
10 replies to this topic

#1 DarkRonin   Members   -  Reputation: 614

Like
0Likes
Like

Posted 01 July 2014 - 07:05 PM

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

Sponsor:

#2 ekba89   Members   -  Reputation: 468

Like
2Likes
Like

Posted 01 July 2014 - 07:16 PM

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.



#3 L. Spiro   Crossbones+   -  Reputation: 14027

Like
2Likes
Like

Posted 01 July 2014 - 07:17 PM

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
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#4 DarkRonin   Members   -  Reputation: 614

Like
0Likes
Like

Posted 01 July 2014 - 07:23 PM

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, 01 July 2014 - 07:34 PM.


#5 Pink Horror   Members   -  Reputation: 1213

Like
0Likes
Like

Posted 01 July 2014 - 08:27 PM

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.

#6 DarkRonin   Members   -  Reputation: 614

Like
0Likes
Like

Posted 01 July 2014 - 08:32 PM

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).

#7 kunos   Crossbones+   -  Reputation: 2207

Like
4Likes
Like

Posted 01 July 2014 - 08:41 PM

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.
 

Edited by kunos, 01 July 2014 - 08:50 PM.

Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#8 DarkRonin   Members   -  Reputation: 614

Like
0Likes
Like

Posted 01 July 2014 - 08:49 PM

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, 01 July 2014 - 08:50 PM.


#9 kunos   Crossbones+   -  Reputation: 2207

Like
3Likes
Like

Posted 01 July 2014 - 08:54 PM

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, 01 July 2014 - 08:55 PM.

Stefano Casillo
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#10 MJP   Moderators   -  Reputation: 11590

Like
0Likes
Like

Posted 01 July 2014 - 10:54 PM

 

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.



#11 DarkRonin   Members   -  Reputation: 614

Like
0Likes
Like

Posted 01 July 2014 - 11:18 PM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS