1
\$\begingroup\$

I have a problem wherein I'm trying to set two simple float variables in my shader. I have a dozen other shaders in this project where it works fine, but for some reason, I've got a bug in this one that I just can't spot.

Here's what I'm doing; I'll edit it down for brevity but try to stay complete. Any suggestions are very welcome!

First, I store the video width and height in the class that represents my shader, and then call functions that set the variables.

    _hVideoWidth           = _ptrEffect->GetParameterByName(0, "videoWidth");
    _hVideoHeight          = _ptrEffect->GetParameterByName(0, "videoHeight");

    pShader->_videoWidth  = pCameraManager->_backBufferWidth;
    pShader->_videoHeight = pCameraManager->_backBufferHeight;
    pShader->SetActive();

    _ptrEffect->SetFloat(_hVideoWidth,  _videoWidth);
    _ptrEffect->SetFloat(_hVideoHeight, _videoHeight);

Then I do my drawing:

    UINT cPasses;
    pShader->GetEffect()->Begin(&cPasses, 0);
              // Drawing code here
    pShader->GetEffect()->End();

My shader will use the videoHeight/Width in order to scale it to the viewport. If I hardcode the width and height, it all works perfectly.

cbuffer VideoData : register(b0)                
{
    float    videoWidth;
    float    videoHeight;
}

VertexShaderOutput UILineVertexShaderFunction(VertexShaderInput input)
{
        VertexShaderOutput output;

    output.Position = input.Position; 

    // Convert pixel coords to (-1, +1)

    output.Position.x = output.Position.x / videoWidth  * 2.0f - 1.0f;
    output.Position.y = output.Position.y / videoHeight * 2.0f - 1.0f;
    output.Color    = input.Color;

    return output;
}

The mystery, however, is that at this point the shader has videoWidth and videoHeight still as zero. If I just hardcode those two expressions to 1280 and 1024 respectively, it works perfectly.

So, there's SOMETHING about the way I'm setting the videoWidth and videoHeight. I've confirmed that at the point I call SetFloat() they are in fact passing in the right values, but they just never seem to make it to the shader variables.

Any suggestions or anything obvious? Even a guess would help, I've been looking at this one for a while!

Thanks, Dave

\$\endgroup\$
  • \$\begingroup\$ Your question is tagged directx9 and it looks like you're using the DX9 effect interface, but your HLSL has a constant buffer which is a DX10+ feature? I'm confused. And really surprised that you're not getting tons of errors either when compiling or loading the shader. \$\endgroup\$ – Nathan Reed Feb 9 '14 at 2:47
  • \$\begingroup\$ The HLSL is compiled with Shader Model 2.0 and (the other shaders in the project) works perfectly with the rest of the code (DX9). I have cbuffer sections in all the other shaders, and have tried this with and without them at global scope or a cbuffer, no difference unfortunately. \$\endgroup\$ – Dave Feb 9 '14 at 20:42
  • \$\begingroup\$ You show how you're calling Begin() and End(), but are you also calling BeginPass() and EndPass()? \$\endgroup\$ – Nathan Reed Feb 9 '14 at 22:11
  • \$\begingroup\$ Nathan - you are correct. I just figured it out and came back to update the question with the answer, but since you came up with it if you'd like to post it as the answer (instead of a comment) I'll accept it! \$\endgroup\$ – Dave Feb 9 '14 at 22:37
1
\$\begingroup\$

Besides calling Begin() and End() around the draw calls, you have to also call BeginPass() and EndPass() for the effect system to set up all the shader parameters and state.

\$\endgroup\$
  • \$\begingroup\$ This was indeed my issue. Sadly it's not the first time I've made this same mistake, and I'm not sure how I missed it again, but thanks! \$\endgroup\$ – Dave Feb 10 '14 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.