GLSL-to-HLSL 參考GLSL-to-HLSL reference

當您將圖形架構從 OpenGL ES 2.0 移植到 Direct3D 11 以建立通用 Windows 平台 (UWP) 遊戲時,也必須將 OpenGL 著色器語言 (GLSL) 程式碼移植到 Microsoft 高階著色器語言 (HLSL) 程式碼。You port your OpenGL Shader Language (GLSL) code to Microsoft High Level Shader Language (HLSL) code when you port your graphics architecture from OpenGL ES 2.0 to Direct3D 11 to create a game for Universal Windows Platform (UWP). 這裡所指的 GLSL 是與 OpenGL ES 2.0 相容;HLSL 則與 Direct3D 11 相容。The GLSL that is referred to herein is compatible with OpenGL ES 2.0; the HLSL is compatible with Direct3D 11. 如需 Direct3D 11 與舊版 Direct3D 之間差異的相關資訊,請參閱功能對應For info about the differences between Direct3D 11 and previous versions of Direct3D, see Feature mapping.

比較 OpenGL ES 2.0 與 Direct3D 11Comparing OpenGL ES 2.0 with Direct3D 11

OpenGL ES 2.0 與 Direct3D 11 有許多相似處。OpenGL ES 2.0 and Direct3D 11 have many similarities. 它們都有類似的轉譯管線與圖形功能。They both have similar rendering pipelines and graphics features. 但 Direct3D 11 是轉譯實作與 API,並非規格;OpenGL ES 2.0 則是轉譯規格與 API,而非實作方式。But Direct3D 11 is a rendering implementation and API, not a specification; OpenGL ES 2.0 is a rendering specification and API, not an implementation. 一般來說,Direct3D 11 與 OpenGL ES 2.0 的差異如下:Direct3D 11 and OpenGL ES 2.0 generally differ in these ways:

OpenGL ES 2.0OpenGL ES 2.0 Direct3D 11Direct3D 11
硬體與作業系統無從驗證的規格與廠商提供的實作Hardware and operating system agnostic specification with vendor provided implementations Microsoft 的硬體抽象實作與 Windows 平台上的憑證Microsoft implementation of hardware abstraction and certification on Windows platforms
抽象的硬體多樣性;執行階段管理多數資源Abstracted for hardware diversity, runtime manages most resources 直接存取硬體配置;應用程式可管理資源並進行處理Direct access to hardware layout; app can manage resources and processing
透過協力廠商程式庫 (例如,簡易直接媒體層 (SDL)) 提供較高階的模組Provides higher-level modules via third-party libraries (for example, Simple DirectMedia Layer (SDL)) 在較低階的模組上建置 Direct2D 這類較高階的模組,簡化 Windows 應用程式的開發Higher-level modules, like Direct2D, are built upon lower modules to simplify development for Windows apps
透過擴充功能展現硬體廠商差異性Hardware vendors differentiate via extensions Microsoft 以一般方式將選用功能新增至 API,因此不會專屬於特定硬體廠商Microsoft adds optional features to the API in a generic way so they aren't specific to any particular hardware vendor

 

一般來說,GLSL 與 HLSL 的差異如下:GLSL and HLSL generally differ in these ways:

GLSLGLSL HLSLHLSL
程序性的,注重步驟 (類似 C)Procedural, step-centric (C like) 以物件為導向,注重資料 (類似 C++)Object oriented, data-centric (C++ like)
著色器編譯整合至圖形 APIShader compilation integrated into the graphics API 在 Direct3D 將著色器傳遞到驅動程式之前,HLSL 編譯器會先將著色器編譯至中繼的二進位表示法。The HLSL compiler compiles the shader to an intermediate binary representation before Direct3D passes it to the driver.
注意   此二進位標記法與硬體無關。 Note  This binary representation is hardware independent. 通常會在 app 建置時進行編譯,而不是在 app 執行時進行編譯。It's typically compiled at app build time, rather than at app run time.
 
Variable 儲存區修飾詞Variable storage modifiers 透過輸入配置宣告傳輸常數緩衝區與資料Constant buffers and data transfers via input layout declarations

類型Types

典型的向量類型:vec2/3/4Typical vector type: vec2/3/4

lowp、mediump、highplowp, mediump, highp

典型的向量類型:float2/3/4Typical vector type: float2/3/4

min10float、min16floatmin10float, min16float

texture2D [Function]texture2D [Function] texture.Sample [datatype.Function]texture.Sample [datatype.Function]
sampler2D [datatype]sampler2D [datatype] Texture2D [datatype]Texture2D [datatype]
以列為主的矩陣 (預設值)Row-major matrices (default) 以欄為主的矩陣 (預設值)Column-major matrices (default)
注意   使用row_major類型修飾詞來變更某個變數的版面配置。 Note   Use the row_major type-modifier to change the layout for one variable. 如需詳細資訊,請參閱變數語法For more info, see Variable Syntax. 您也可指定編譯器旗標或 pragma 來變更全域預設值。You can also specify a compiler flag or a pragma to change the global default.
 
片段著色器Fragment shader 像素著色器Pixel shader

 

注意   HLSL 具有材質和取樣器作為兩個不同的物件。Note  HLSL has textures and samplers as two separate objects. 在 GLSL (如 Direct3D 9) 中,紋理繫結為取樣器狀態的一部分。In GLSL, like Direct3D 9, the texture binding is part of the sampler state.

 

在 GLSL 中,您將大部分的 OpenGL 狀態呈現為預先定義的全域變數。In GLSL, you present much of the OpenGL state as pre-defined global variables. 例如,使用 GLSL 時,您可以使用 gl _ 位置 變數來指定頂點位置,並使用 gl _ FragColor 變數來指定片段色彩。For example, with GLSL, you use the gl_Position variable to specify vertex position and the gl_FragColor variable to specify fragment color. 在 HLSL 中,您明確將 Direct3D 狀態從 app 程式碼傳遞至著色器。In HLSL, you pass Direct3D state explicitly from the app code to the shader. 例如,使用 Direct3D 與 HLSL 時,頂點著色器的輸入必須符合頂點緩衝區中的資料格式,而應用程式程式碼中的常數緩衝區結構必須符合著色器程式碼中的常數緩衝區 (cbuffer) 結構。For example, with Direct3D and HLSL, the input to the vertex shader must match the data format in the vertex buffer, and the structure of a constant buffer in the app code must match the structure of a constant buffer (cbuffer) in shader code.

將 GLSL 變數移植到 HLSLPorting GLSL variables to HLSL

在 GLSL 中,您將修飾詞 (限定詞) 套用到全域著色器變數宣告,讓變數在您的著色器中擁有特定的行為。In GLSL, you apply modifiers (qualifiers) to a global shader variable declaration to give that variable a specific behavior in your shaders. 在 HLSL 則不需要這些修飾詞,因為您使用傳遞至著色器並從著色器所傳回的引數來定義著色器流程。In HLSL, you don’t need these modifiers because you define the flow of the shader with the arguments that you pass to your shader and that you return from your shader.

GLSL 變數行為GLSL variable behavior HLSL 對等項目HLSL equivalent

均勻uniform

您將 uniform 變數從 app 程式碼傳遞至頂點與片段著色器內,或是其中之一。You pass a uniform variable from the app code into either or both vertex and fragment shaders. 使用這些著色器繪製任何三角形之前,您必須設定所有 uniform 的值,這樣在繪製三角形網格的整個過程中,它們的值才能維持相同。You must set the values of all uniforms before you draw any triangles with those shaders so their values stay the same throughout the drawing of a triangle mesh. 這些值是 uniform。These values are uniform. 有些 uniform 是針對整個框架所設定,而其他則是特別針對某一組特殊頂點像素著色器而設定。Some uniforms are set for the entire frame and others uniquely to one particular vertex-pixel shader pair.

uniform 變數是基於多邊形的變數。Uniform variables are per-polygon variables.

使用常數緩衝區。Use constant buffer.

請參閱使用方法:建立常數緩衝區著色器常數See How to: Create a Constant Buffer and Shader Constants.

不同varying

您在頂點著色器內初始化 varying 變數,並將它傳遞至片段著色器中擁有相同名稱的 varying 變數。You initialize a varying variable inside the vertex shader and pass it through to an identically named varying variable in the fragment shader. 因為頂點著色器只設定每個頂點的 varying 變數值,因此點陣化會插補這些值 (以透視修正方式) 來產生每個片段值,以傳遞至片段著色器。Because the vertex shader only sets the value of the varying variables at each vertex, the rasterizer interpolates those values (in a perspective-correct manner) to generate per fragment values to pass into the fragment shader. 這些變數在每個三角形都不同。These variables vary across each triangle.

使用從頂點著色器傳回的結構做為像素著色器的輸入。Use the structure that you return from your vertex shader as the input to your pixel shader. 請確定語意值相符。Make sure the semantic values match.

屬性attribute

attribute 是您單獨從 app 程式碼傳遞至頂點著色器之頂點描述的一部分。An attribute is a part of the description of a vertex that you pass from the app code to the vertex shader alone. 與 uniform 不同,您要為每個頂點設定各個 attribute 的值,然後每個頂點便有不同的值。Unlike a uniform, you set each attribute’s value for each vertex, which, in turn, allows each vertex to have a different value. Attribute 變數是基於頂點的變數。Attribute variables are per-vertex variables.

在 Direct3D 應用程式程式碼中定義頂點緩衝區,並使它與頂點著色器中定義的頂點輸入相符。Define a vertex buffer in your Direct3D app code and match it to the vertex input defined in the vertex shader. 您可選擇是否定義索引緩衝區。Optionally, define an index buffer. 請參閱使用方法:建立頂點緩衝區使用方法:建立索引緩衝區See How to: Create a Vertex Buffer and How to: Create an Index Buffer.

在 Direct3D 應用程式程式碼中建立輸入配置,並使語意值與頂點輸入中的語意值相符。Create an input layout in your Direct3D app code and match semantic values with those in the vertex input. 請參閱建立輸入配置See Create the input layout.

constconst

編譯至著色器且永不會變更的常數。Constants that are compiled into the shader and never change.

請使用 static constUse a static const. static 表示此值未針對常數緩衝區公開,const 表示著色器無法變更此值。static means the value isn't exposed to constant buffers, const means the shader can't change the value. 因此,根據值的初始設定式,會於編譯時識別它。So, the value is known at compile time based on its initializer.

 

在 GLSL,沒有修飾詞的變數只是每個著色器私有的一般全域變數。In GLSL, variables without modifiers are just ordinary global variables that are private to each shader.

當您將資料傳遞至紋理 (HLSL 中的 Texture2D) 以及其關聯的取樣器 (HLSL 中的 SamplerState) 時,通常會將它們宣告為像素著色器中的全域變數。When you pass data to textures (Texture2D in HLSL) and their associated samplers (SamplerState in HLSL), you typically declare them as global variables in the pixel shader.

將 GLSL 類型移植到 HLSLPorting GLSL types to HLSL

使用此表格將 GLSL 類型移植到 HLSL。Use this table to port your GLSL types to HLSL.

GLSL 類型GLSL type HLSL 類型HLSL type
純量類型:float、int、boolscalar types: float, int, bool

純量類型:float、int、boolscalar types: float, int, bool

還有 uint、doublealso, uint, double

如需詳細資訊,請參閱純量類型For more info, see Scalar Types.

向量類型vector type

  • 浮點向量:vec2、vec3、vec4floating-point vector: vec2, vec3, vec4
  • 布林值向量:bvec2、bvec3、bvec4Boolean vector: bvec2, bvec3, bvec4
  • 帶正負號的整數向量:ivec2、ivec3、ivec4signed integer vector: ivec2, ivec3, ivec4

向量類型vector type

  • float2、float3、float4 與 float1float2, float3, float4, and float1
  • bool2、bool3、bool4 與 bool1bool2, bool3, bool4, and bool1
  • int2、int3、int4 與 int1int2, int3, int4, and int1
  • 這些類型也有類似於 float、bool 與 int 的向量擴充:These types also have vector expansions similar to float, bool, and int:

    • uintuint
    • min10float、min16floatmin10float, min16float
    • min12int、min16intmin12int, min16int
    • min16uintmin16uint

如需詳細資訊,請參閱向量類型關鍵字For more info, see Vector Type and Keywords.

向量也是定義為 float4 (typedef vector <float, 4> vector;) 的類型。vector is also type defined as float4 (typedef vector <float, 4> vector;). 如需詳細資訊,請參閱使用者定義的類型For more info, see User-Defined Type.

矩陣類型matrix type

  • mat2:2x2 浮點數矩陣mat2: 2x2 float matrix
  • mat3:3x3 浮點數矩陣mat3: 3x3 float matrix
  • mat4:4x4 浮點數矩陣mat4: 4x4 float matrix

矩陣類型matrix type

  • float2x2float2x2
  • float3x3float3x3
  • float4x4float4x4
  • 還有 float1x1、float1x2、float1x3、float1x4、float2x1、float2x3、float2x4、float3x1、float3x2、float3x4、float4x1、float4x2、float4x3also, float1x1, float1x2, float1x3, float1x4, float2x1, float2x3, float2x4, float3x1, float3x2, float3x4, float4x1, float4x2, float4x3
  • 這些類型也有類似於 float 的矩陣擴充:These types also have matrix expansions similar to float:

    • int、uint、boolint, uint, bool
    • min10float、min16floatmin10float, min16float
    • min12int、min16intmin12int, min16int
    • min16uintmin16uint

您也可使用矩陣類型來定義矩陣。You can also use the matrix type to define a matrix.

例如,matrix <float, 2, 2> fMatrix = {0.0f, 0.1, 2.1f, 2.2f};For example: matrix <float, 2, 2> fMatrix = {0.0f, 0.1, 2.1f, 2.2f};

矩陣也是定義為 float4x4 (typedef matrix <float, 4, 4> matrix;) 的類型。matrix is also type defined as float4x4 (typedef matrix <float, 4, 4> matrix;). 如需詳細資訊,請參閱使用者定義的類型For more info, see User-Defined Type.

適用於 float、int、取樣器的精確度限定詞precision qualifiers for float, int, sampler

  • highphighp

    此限定詞提供的最小精確度需求大於 min16float 所提供的需求,且小於完整 32 位元浮點數。This qualifier provides minimum precision requirements that are greater than that provided by min16float and less than a full 32-bit float. HLSL 中的對等項目為:Equivalent in HLSL is:

    highp float -> floathighp float -> float

    highp int -> inthighp int -> int

  • mediumpmediump

    套用到 float 與 int 的這個限定詞相當於 HLSL 中的 min16float 與 min12int。This qualifier applied to float and int is equivalent to min16float and min12int in HLSL. 最小 10 位元的 mantissa,不像 min10float。Minimum 10 bits of mantissa, not like min10float.

  • lowplowp

    套用至 float 的這個限定詞提供範圍從 -2 到 2 的浮點。This qualifier applied to float provides a floating point range of -2 to 2. 相當於 HLSL 中的 min10float。Equivalent to min10float in HLSL.

精確度類型precision types

  • min16float:最小 16 位元浮點值min16float: minimum 16-bit floating point value
  • min10floatmin10float

    最小固定點帶正負號 2.8 位元值 (2 位元的整數與 8 位元的分數元件)。Minimum fixed-point signed 2.8 bit value (2 bits of whole number and 8 bits fractional component). 8 位元的分數元件可包含 1 而不排除,讓範圍完整包含從 -2 到 2。The 8-bit fractional component can be inclusive of 1 instead of exclusive to give it the full inclusive range of -2 to 2.

  • min16int:最小 16 位元帶正負號的整數min16int: minimum 16-bit signed integer
  • min12int:最小 12 位元帶正負號的整數min12int: minimum 12-bit signed integer

    此類型適用於 10Level9 (9_x 功能層級),其中整數由浮點數表示。This type is for 10Level9 (9_x feature levels) in which integers are represented by floating point numbers. 這會是您在使用 16 位元浮點數列舉整數時所得到的精確度。This is the precision you can get when you emulate an integer with a 16-bit floating point number.

  • min16uint:最小 16 位元不帶正負號的整數min16uint: minimum 16-bit unsigned integer

如需詳細資訊,請參閱純量類型使用 HLSL 最小精確度For more info, see Scalar Types and Using HLSL minimum precision.

sampler2Dsampler2D Texture2DTexture2D
samplerCubesamplerCube TextureCubeTextureCube

 

將 GLSL 預先定義的全域變數移植到 HLSLPorting GLSL pre-defined global variables to HLSL

使用此表格將 GLSL 預先定義的全域變數移植到 HLSL。Use this table to port GLSL pre-defined global variables to HLSL.

GLSL 預先定義的全域變數GLSL pre-defined global variable HLSL 語意HLSL semantics

gl_Positiongl_Position

此變數為 vec4 類型。This variable is type vec4.

頂點位置Vertex position

例如 - gl_Position = position;for example - gl_Position = position;

SV_PositionSV_Position

Direct3D 9 中的 POSITIONPOSITION in Direct3D 9

此語意為 float4 類型。This semantic is type float4.

頂點著色器輸出Vertex shader output

頂點位置Vertex position

例如 - float4 vPosition : SV_Position;for example - float4 vPosition : SV_Position;

gl_PointSizegl_PointSize

此變數為 float 類型。This variable is type float.

點大小Point size

PSIZEPSIZE

除非目標設為 Direct3D 9,否則無意義No meaning unless you target Direct3D 9

此語意為 float 類型。This semantic is type float.

頂點著色器輸出Vertex shader output

點大小Point size

gl_FragColorgl_FragColor

此變數為 vec4 類型。This variable is type vec4.

片段色彩Fragment color

例如 - gl_FragColor = vec4(colorVarying, 1.0);for example - gl_FragColor = vec4(colorVarying, 1.0);

SV_TargetSV_Target

Direct3D 9 中的 COLORCOLOR in Direct3D 9

此語意為 float4 類型。This semantic is type float4.

像素著色器輸出Pixel shader output

像素色彩Pixel color

例如 - float4 Color[4] : SV_Target;for example - float4 Color[4] : SV_Target;

gl_FragData [n]gl_FragData[n]

此變數為 vec4 類型。This variable is type vec4.

色彩附件 n 的片段色彩Fragment color for color attachment n

SV_Target[n]SV_Target[n]

此語意為 float4 類型。This semantic is type float4.

儲存在 n 轉譯目標的像素著色器輸出值,其中 0 <= n <= 7。Pixel shader output value that is stored in n render target, where 0 <= n <= 7.

gl_FragCoordgl_FragCoord

此變數為 vec4 類型。This variable is type vec4.

框架緩衝區內的片段位置Fragment position within frame buffer

SV_PositionSV_Position

Direct3D 9 中無法使用Not available in Direct3D 9

此語意為 float4 類型。This semantic is type float4.

像素著色器輸入Pixel shader input

螢幕空間座標Screen space coordinates

例如 - float4 screenSpace : SV_Positionfor example - float4 screenSpace : SV_Position

gl_FrontFacinggl_FrontFacing

此變數為 bool 類型。This variable is type bool.

決定片段是否屬於面向前方的基本型別。Determines whether fragment belongs to a front-facing primitive.

SV_IsFrontFaceSV_IsFrontFace

Direct3D 9 中的 VFACEVFACE in Direct3D 9

SV_IsFrontFace 為 bool 類型。SV_IsFrontFace is type bool.

VFACE 為 float 類型。VFACE is type float.

像素著色器輸入Pixel shader input

基本型別面向Primitive facing

gl_PointCoordgl_PointCoord

此變數為 vec2 類型。This variable is type vec2.

點內的片段位置 (僅限點陣化)Fragment position within a point (point rasterization only)

SV_PositionSV_Position

Direct3D 9 中的 VPOSVPOS in Direct3D 9

SV_Position 為 float4 類型。SV_Position is type float4.

VPOS 為 float2 類型。VPOS is type float2.

像素著色器輸入Pixel shader input

螢幕空間中的像素或範本位置The pixel or sample position in screen space

例如 - float4 pos : SV_Positionfor example - float4 pos : SV_Position

gl_FragDepthgl_FragDepth

此變數為 float 類型。This variable is type float.

深度緩衝區資料Depth buffer data

SV_DepthSV_Depth

Direct3D 9 中的 DEPTHDEPTH in Direct3D 9

SV_Depth 為 float 類型。SV_Depth is type float.

像素著色器輸出Pixel shader output

深度緩衝區資料Depth buffer data

 

您可使用語意來指定頂點著色器輸入與像素著色器輸入的位置、色彩等等。You use semantics to specify position, color, and so on for vertex shader input and pixel shader input. 您必須使輸入配置中的語意值與頂點著色器輸入相符。You must match the semantics values in the input layout with the vertex shader input. 如需範例,請參閱將 GLSL 變數移植到 HLSL 的範例For examples, see Examples of porting GLSL variables to HLSL. 如需 HLSL 語意的詳細資訊,請參閱語意For more info about the HLSL semantics, see Semantics.

將 GLSL 變數移植到 HLSL 的範例Examples of porting GLSL variables to HLSL

這裡我們示範在 OpenGL/GLSL 程式碼中使用 GLSL 變數的範例,以及在 Direct3D/HLSL 程式碼中的相等範例。Here we show examples of using GLSL variables in OpenGL/GLSL code and then the equivalent example in Direct3D/HLSL code.

GLSL 中的 uniform、attribute 與 varyingUniform, attribute, and varying in GLSL

OpenGL 應用程式程式碼OpenGL app code

// Uniform values can be set in app code and then processed in the shader code.
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

// Incoming position of vertex
attribute vec4 position;
 
// Incoming color for the vertex
attribute vec3 color;
 
// The varying variable tells the shader pipeline to pass it  
// on to the fragment shader.
varying vec3 colorVarying;

GLSL 頂點著色器程式碼GLSL vertex shader code

//The shader entry point is the main method.
void main()
{
colorVarying = color; //Use the varying variable to pass the color to the fragment shader
gl_Position = position; //Copy the position to the gl_Position pre-defined global variable
}

GLSL 片段著色器程式碼GLSL fragment shader code

void main()
{
//Pad the colorVarying vec3 with a 1.0 for alpha to create a vec4 color
//and assign that color to the gl_FragColor pre-defined global variable
//This color then becomes the fragment's color.
gl_FragColor = vec4(colorVarying, 1.0);
}

HLSL 中的常數緩衝區與資料傳輸Constant buffers and data transfers in HLSL

以下範例說明如何將資料傳遞至 HLSL 頂點著色器,然後流向像素著色器。Here is an example of how you pass data to the HLSL vertex shader that then flows through to the pixel shader. 在您的應用程式程式碼中,定義頂點與常數緩衝區。In your app code, define a vertex and a constant buffer. 然後,在頂點著色器程式碼中,將常數緩衝區定義為 cbuffer,並儲存每一頂點資料與像素著色器輸入資料。Then, in your vertex shader code, define the constant buffer as a cbuffer and store the per-vertex data and the pixel shader input data. 這裡我們使用名為 VertexShaderInputPixelShaderInput 的結構。Here we use structures called VertexShaderInput and PixelShaderInput.

Direct3D app 程式碼Direct3D app code

struct ConstantBuffer
{
    XMFLOAT4X4 model;
    XMFLOAT4X4 view;
    XMFLOAT4X4 projection;
};
struct SimpleCubeVertex
{
    XMFLOAT3 pos;   // position
    XMFLOAT3 color; // color
};

 // Create an input layout that matches the layout defined in the vertex shader code.
 const D3D11_INPUT_ELEMENT_DESC basicVertexLayoutDesc[] =
 {
     { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,  0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
     { "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
 };

// Create vertex and index buffers that define a geometry.

HLSL 頂點著色器程式碼HLSL vertex shader code

cbuffer ModelViewProjectionCB : register( b0 )
{
    matrix model; 
    matrix view;
    matrix projection;
};
// The POSITION and COLOR semantics must match the semantics in the input layout Direct3D app code.
struct VertexShaderInput
{
    float3 pos : POSITION; // Incoming position of vertex 
    float3 color : COLOR; // Incoming color for the vertex
};

struct PixelShaderInput
{
    float4 pos : SV_Position; // Copy the vertex position.
    float4 color : COLOR; // Pass the color to the pixel shader.
};

PixelShaderInput main(VertexShaderInput input)
{
    PixelShaderInput vertexShaderOutput;

    // shader source code

    return vertexShaderOutput;
}

HLSL 像素著色器程式碼HLSL pixel shader code

// Collect input from the vertex shader. 
// The COLOR semantic must match the semantic in the vertex shader code.
struct PixelShaderInput
{
    float4 pos : SV_Position;
    float4 color : COLOR; // Color for the pixel
};

// Set the pixel color value for the renter target. 
float4 main(PixelShaderInput input) : SV_Target
{
    return input.color;
}

將 OpenGL 轉譯程式碼移植到 Direct3D 的範例Examples of porting OpenGL rendering code to Direct3D

這裡我們示範在 OpenGL ES 2.0 程式碼中轉譯的範例,以及在 Direct3D 11 程式碼中的相等範例。Here we show an example of rendering in OpenGL ES 2.0 code and then the equivalent example in Direct3D 11 code.

OpenGL 轉譯程式碼OpenGL rendering code

// Bind shaders to the pipeline. 
// Both vertex shader and fragment shader are in a program.
glUseProgram(m_shader->getProgram());
 
// Input asssembly 
// Get the position and color attributes of the vertex.

m_positionLocation = glGetAttribLocation(m_shader->getProgram(), "position");
glEnableVertexAttribArray(m_positionLocation);

m_colorLocation = glGetAttribColor(m_shader->getProgram(), "color");
glEnableVertexAttribArray(m_colorLocation);
 
// Bind the vertex buffer object to the input assembler.
glBindBuffer(GL_ARRAY_BUFFER, m_geometryBuffer);
glVertexAttribPointer(m_positionLocation, 4, GL_FLOAT, GL_FALSE, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, m_colorBuffer);
glVertexAttribPointer(m_colorLocation, 3, GL_FLOAT, GL_FALSE, 0, NULL);
 
// Draw a triangle with 3 vertices.
glDrawArray(GL_TRIANGLES, 0, 3);

Direct3D 轉譯程式碼Direct3D rendering code

// Bind the vertex shader and pixel shader to the pipeline.
m_d3dDeviceContext->VSSetShader(vertexShader.Get(),nullptr,0);
m_d3dDeviceContext->PSSetShader(pixelShader.Get(),nullptr,0);
 
// Declare the inputs that the shaders expect.
m_d3dDeviceContext->IASetInputLayout(inputLayout.Get());
m_d3dDeviceContext->IASetVertexBuffers(0, 1, vertexBuffer.GetAddressOf(), &stride, &offset);

// Set the primitive's topology.
m_d3dDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

// Draw a triangle with 3 vertices. triangleVertices is an array of 3 vertices.
m_d3dDeviceContext->Draw(ARRAYSIZE(triangleVertices),0);