Direct3D 11 (效果變數語法)

效果變數會使用本節所述的語法來宣告。

Syntax

基本語法:

DataTypeVariableName [ : SemanticName ] <Annotations> [ = InitialValue ];

如需完整 語法,請參閱 DirectX HLSL) 變數語法 (。

名稱 描述
DataType 任何 基本、 紋理、未排序的存取檢視、著色器或狀態欄塊類型。
VariableName 可唯一識別效果變數名稱的 ASCII 字串。
SemanticName ASCII 字串,表示應如何使用變數的其他資訊。 語意是 ASCII 字串,可以是預先定義的系統值或自訂使用者字串。
註解 一或多個使用者提供的資訊 (中繼資料) ,效果系統會忽略該資訊。 如需語法,請參閱 注釋語法 (Direct3D 11) 。
InitialValue 變數的預設值。

 

在所有函式之外宣告的效果變數會被視為範圍中的全域變數;函式內宣告的變數是該函式的區域變數。

範例

此範例說明全域效果數值變數。

float4 g_MaterialAmbientColor;      // Material's ambient color
float4 g_MaterialDiffuseColor;      // Material's diffuse color
float3 g_LightDir[3];               // Light's direction in world space
float4x4 g_mWorld;                  // World matrix for object

此範例說明著色器函式區域變數的效果變數。

VS_OUTPUT RenderSceneVS( ... )
{
    float3 vNormalWorldSpace;
    float4 vAnimatedPos;

    // shader body
}

此範例說明具有語意的函式參數。

VS_OUTPUT RenderSceneVS( float4 vPos : SV_POSITION,
                         float3 vNormal : NORMAL,
                         float2 vTexCoord0 : TEXCOORD0,
                         uniform int nNumLights,
                         uniform bool bTexture,
                         uniform bool bAnimate )
{
  ...
}

此範例說明如何宣告全域紋理變數。

Texture2D g_MeshTexture;            // Color texture for mesh

使用紋理取樣器來取樣紋理。 若要在效果中設定取樣器,請參閱 取樣器類型。

此範例說明宣告全域未排序的存取檢視變數。

RWStructuredBuffer<uint> bc : register(u2) < string name="bc"; >;
RWBuffer<uint> bRW;
struct S
{
   uint key;
   uint value;
};
AppendStructuredBuffer<S> asb : register(u5);
RWByteAddressBuffer rwbab : register(u1);
RWStructuredBuffer<uint> rwsb : register(u3);
RWTexture1D<float> rwt1d : register(u1);
RWTexture1DArray<uint> rwt1da : register(u4);
RWTexture2D<uint> rwt2d : register(u2);
RWTexture2DArray<uint> rwt2da : register(u6);
RWTexture3D<uint> rwt3d : register(u7); 
 This example illustrates declaring global shader variables.
VertexShader pVS = CompileShader( vs_5_0, VS() );
HullShader pHS = NULL;
DomainShader pDS = NULL;
GeometryShader pGS = ConstructGSWithSO( CompileShader( gs_5_0, VS() ), 
                                        "0:Position.xy; 1:Position.zw; 2:Color.xy", 
                                        "3:Texcoord.xyzw; 3:$SKIP.x;", 
                                        NULL, 
                                        NULL, 
                                        1 );
PixelShader pPS = NULL;
ComputeShader pCS = NULL;
This example illustrates declaring global state block variables.
BlendState myBS[2] < bool IsValid = true; >
{
  {
    BlendEnable[0] = false;
  },
  {
    BlendEnable[0] = true;
    SrcBlendAlpha[0] = Inv_Src_Alpha;
  }
};

RasterizerState myRS
{
      FillMode = Solid;
      CullMode = NONE;
      MultisampleEnable = true;
      DepthClipEnable = false;
};

DepthStencilState myDS
{
    DepthEnable = false;
    DepthWriteMask = Zero;
    DepthFunc = Less;
};
sampler mySS[2] : register(s3) 
{
    {
        Filter = ANISOTROPIC;
        MaxAnisotropy = 3;
    },
    {
        Filter = ANISOTROPIC;
        MaxAnisotropy = 4;
    }
};
  
  

效果格式