Sign in to follow this  
jodynek

Problem with cbuffer in Pixel shader

Recommended Posts

Hello,
I'm beginner in DirectX programming and I'm trying to solve following problem. I need to pass input parameter via cbuffer into pixel shader.

HLSL code:

cbuffer GameStateBuffer : register( b1 )
{
uint iGameState;
float padding;
float padding1;
float padding2;
};


float4 PS_Main( PS_Input frag ) : SV_TARGET
{
[color="#ff0000"]if (iGameState == 0) { // there is my problem[/color]
return m_textures[0].Sample( colorSampler_, frag.tex0 );
} else {
return m_textures[1].Sample( colorSampler_, frag.tex0 );
}
}

C++ render code:
.
[b][color="blue"]struct[/color] GameStateBufferType [/b]
[b]{ [/b]
[b][color="blue"] int[/color] iGameState; [color="blue"]float[/color] padding; [color="blue"]float[/color] padding1; [color="blue"]float[/color] padding2; [/b]
[b]};.[/b]

[color="blue"]float[/color] clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor );
[color="blue"]unsigned[/color] [color="blue"]int[/color] stride = [color="blue"]sizeof[/color]( VertexPos );
[color="blue"]unsigned[/color] [color="blue"]int[/color] offset = 0;d3dContext_->IASetInputLayout( inputLayout_ );
d3dContext_->IASetVertexBuffers( 0, 1, &vertexBuffer_, &stride, &offset );
d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

[b]data.iGameState = 1; [/b]
[b]data.padding = 1.0f; [/b]
[b]data.padding1 = .0f; [/b]
[b]data.padding2 = .0f; [/b]
[b]d3dContext_->UpdateSubresource( m_GameStateBuffer, 0, 0, &data, 0, 0 ); [/b]
[b]d3dContext_->VSSetConstantBuffers( 1, 1, &m_GameStateBuffer ); [/b]

d3dContext_->VSSetShader( solidColorVS_, 0, 0 );
d3dContext_->PSSetShader( solidColorPS_, 0, 0 );
d3dContext_->PSSetShaderResources(0, 2, m_textures);
d3dContext_->PSSetSamplers( 0, 1, &colorMapSampler_ );

Problem is with iGameState, which isn't passed into pixel shader. Sources are in included ZIP file.
I'm sorry for my beginner's question and thanks a lot for your answers...

Share this post


Link to post
Share on other sites
Hi,

just pointing the obvious:

[size="2"][color="#1c2837"]You can totally avoid passing game state to the pixel shader if you set the desired texture already in the program before calling the pixel shader. [/color][/size]
[size="2"] [/size]
[size="2"][color="#1c2837"][code]
if (iGameState == 0) {
d3dContext_->PSSetShaderResources(0, 1, m_textures[0]);
} else {
d3dContext_->PSSetShaderResources(0, 1, m_textures[1]);
[/code][/color][/size]
[color="#1C2837"] [/color]
[size="2"][size="3"][color="#000000"][code]
float4 PS_Main( PS_Input frag ) : SV_TARGET
{
return m_textures[0].Sample( colorSampler_, frag.tex0 );
}
[/code][/color][/size][/size]
[color="#1C2837"] [/color]
[color="#1C2837"] [/color]
[size="2"][color="#1c2837"]Cheers![/color][/size]

Share this post


Link to post
Share on other sites
Thanks a lot. Your answer solve my problem :-) I'm simply fool...
[quote name='MJP' timestamp='1313733725' post='4851073']
You're binding the constant buffer to the vertex shader, but not the pixel shader. You need to call PSSetConstantBuffers.
[/quote]

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