Jump to content
  • Advertisement
Sign in to follow this  
jodynek

Problem with cbuffer in Pixel shader

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

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
return m_textures[0].Sample( colorSampler_, frag.tex0 );
} else {
return m_textures[1].Sample( colorSampler_, frag.tex0 );
}
}

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

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

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

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
Advertisement
You're binding the constant buffer to the vertex shader, but not the pixel shader. You need to call PSSetConstantBuffers.

Share this post


Link to post
Share on other sites
Hi,

just pointing the obvious:

[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="#1c2837"]
if (iGameState == 0) {
d3dContext_->PSSetShaderResources(0, 1, m_textures[0]);
} else {
d3dContext_->PSSetShaderResources(0, 1, m_textures[1]);

[color="#1C2837"]
[color="#000000"]
float4 PS_Main( PS_Input frag ) : SV_TARGET
{
return m_textures[0].Sample( colorSampler_, frag.tex0 );
}

[color="#1C2837"]
[color="#1C2837"]
[color="#1c2837"]Cheers!

Share this post


Link to post
Share on other sites
Thanks a lot. Your answer solve my problem :-) I'm simply fool...

You're binding the constant buffer to the vertex shader, but not the pixel shader. You need to call PSSetConstantBuffers.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!