Sign in to follow this  
brekehan

Constant Buffer

Recommended Posts

I am not understnading what this is even after reading the MSDN description. How do I identify one in HLSL code? What makes it a constant buffer vs some other type? Why am I getting a constant buffer name of "$GLOBALS" in the following code? I am confused, because they give an example of putting the WVP matrix in a constant buffer. So, on one hand I think it might be variables that change every frame. However, they set it like a vertex buffer, which I assume is expensive, so I think it might be varibales that never change, or at least not unless we are in an initializing stage in the application. I am also unsure, if I should even be querying like this for the constant buffer at all. My goal is to get names, types, and annotations for all my effect variables. --- Why does it have a bad pointer for the semantic and crash? Edit: Found where MSDN said it would return NULL, if one is not present --
// Query the effect for variable information
   D3D10_EFFECT_DESC effectDesc;
   m_effect->GetDesc(&effectDesc);

   BOOL isChild                  = effectDesc.IsChildEffect;
   UINT numConstantBuffers       = effectDesc.ConstantBuffers;
   UINT numSharedConstantBuffers = effectDesc.SharedConstantBuffers;
   UINT numGlobals               = effectDesc.GlobalVariables;
   UINT numTechniques            = effectDesc.Techniques;

   // Query the effect for its variables
   for(unsigned i = 0; i < numConstantBuffers; ++i)
   {
      ID3D10EffectConstantBuffer * constantBuffer = m_effect->GetConstantBufferByIndex(i);
      
      D3D10_EFFECT_VARIABLE_DESC effectVariableDesc;
      constantBuffer->GetDesc(&effectVariableDesc);

      std::string name     = effectVariableDesc.Name;
      std::string semantic = effectVariableDesc.Semantic;


   }

   // Query the each technique for information
   for(unsigned i = 0; i < numTechniques; ++i)
   {

      ID3D10EffectTechnique * technique = m_effect->GetTechniqueByIndex(i);

      D3D10_TECHNIQUE_DESC techniqueDesc;
      technique->GetDesc(&techniqueDesc);

      std::string name    = techniqueDesc.Name;
      UINT passes         = techniqueDesc.Passes;
      UINT numAnnotations = techniqueDesc.Annotations;




Here is the fx file
//--------------------------------------------------------------------------------------
// File: default.fx
//
//--------------------------------------------------------------------------------------

Texture2D textureDiffuse;

SamplerState samplerLinear
{
   Filter = MIN_MAG_MIP_LINEAR;
   AddressU = Wrap;
   AddressV = Wrap;
};

//--------------------------------------------------------------------------------------
// Constant Buffer Variables
matrix World;
matrix View;
matrix Projection;

float4 LightDirs[2];
float4 LightColors[2];

//--------------------------------------------------------------------------------------
struct VS_INPUT
{
   float4 Pos : POSITION;
   float3 Norm : NORMAL;
   float2 Tex : TEXCOORD;
};

struct PS_INPUT
{
   float4 Pos : SV_POSITION;
   float3 Norm : NORMAL;
   float2 Tex : TEXCOORD0;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Simply transforms positions and normals
// 
PS_INPUT VS( VS_INPUT input )
{
   PS_INPUT output = (PS_INPUT)0;
   
   // Transform the postion
   output.Pos   = mul( input.Pos, World );
   output.Pos   = mul( output.Pos, View );
   output.Pos   = mul( output.Pos, Projection);
   
   // Transform the normal
   output.Norm  = mul( input.Norm, World );
   normalize(output.Norm);
   
   // Copy the UV 
   output.Tex   = input.Tex;
   
   return output;
}


//--------------------------------------------------------------------------------------
// Pixel Shaders
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Shades an object using 2 colored directional lights
//
float4 PS( PS_INPUT input ) : SV_Target
{
   float4 finalColor = 0;
   
   // Do N dot L lighting for 2 lights
   for(int i = 0; i < 2; i++)
   {
      finalColor += saturate( dot( (float3)LightDirs[i], input.Norm) * LightColors[i] );
   }
   
   finalColor *= textureDiffuse.Sample(samplerLinear, input.Tex);
   finalColor.a = 1;
   
   return finalColor;
}

//--------------------------------------------------------------------------------------
// Techniques
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Renders an object using 2 directional lights
//
technique10 Render
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
    }
}


[Edited by - brekehan on October 9, 2008 8:28:12 PM]

Share this post


Link to post
Share on other sites
You're getting a constant buffer name of "$GLOBALS" because you haven't placed any of your variables in a constant buffer! Therefore, they are all implicitly placed in the global constant buffer.

You should separate out constant buffers by update frequency. Have a buffer for variables which are set less than once per frame, once per frame, multiple times per frame, etc. When a variable in a constant buffer is updated, the entire constant buffer is reuploaded to the graphics card.

Before continuing further, you should read up on constant buffers at MSDN, which includes instructions on how to use constant buffers in your shader.

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