High Level Shading Language
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2023/02/12 05:06 UTC 版)
コード例
以下にDirect3D 10/11向けHLSLを用いた、単純なHalf Lambert照明モデルおよびPhong照明モデル(Blinn-Phong)の頂点シェーダーおよびピクセルシェーダーのプログラムを示す。なお分岐によって照明モデルを切り替えているが、これはいわゆるウーバーシェーダー(uber-shader)なので、実行速度効率などは考慮していないことに注意されたい。
// Shader Constants.
matrix TrWorldViewProj;
matrix TrWorld;
float4 LightPosition;
float3 EyePosition;
float4 DiffuseColor;
float4 SpecularColor;
float SpecularPower;
bool IsPhongModel;
struct BasicVSOutput
{
float4 Pos : SV_POSITION;
float3 WPos : TEXCOORD1;
float3 WNormal : NORMAL0;
};
typedef BasicVSOutput BasicPSInput;
// Vertex Shader Program.
BasicVSOutput BasicVS(float3 pos : POSITION0, float3 normal : NORMAL0)
{
BasicVSOutput output = (BasicVSOutput)0;
output.Pos = mul(float4(pos, 1), TrWorldViewProj);
output.WPos = mul(float4(pos, 1), TrWorld).xyz;
output.WNormal = mul(normal, (float3x3)TrWorld);
return output;
}
float4 CalcLambert(float3 light, float3 wnormal)
{
// Half Lambert.
float lambert = dot(light, wnormal);
lambert = lambert * 0.5f + 0.5f;
lambert *= lambert;
return lambert * DiffuseColor;
}
float4 BasicLambert(BasicPSInput input)
{
const float3 light = normalize(LightPosition.xyz - input.WPos);
const float3 normal = normalize(input.WNormal);
return CalcLambert(light, normal);
}
float4 BasicPhong(BasicPSInput input)
{
// Phong lighting with specular.
const float3 eye = normalize(EyePosition - input.WPos);
const float3 light = normalize(LightPosition.xyz - input.WPos);
const float3 halfway = normalize(light + eye);
const float3 normal = normalize(input.WNormal);
const float specular = pow(max(dot(normal, halfway), 0.0), SpecularPower);
return CalcLambert(light, normal) + specular * SpecularColor;
}
// Pixel Shader Program.
float4 BasicPS(BasicPSInput input) : SV_TARGET0
{
if (IsPhongModel)
{
return BasicPhong(input);
}
else
{
return BasicLambert(input);
}
}
例のように、ピクセルシェーダーによってピクセル単位の正規化法線ベクトルを求めることにより、Direct3D 7以前の固定機能シェーダーでは実現が難しかったPer-Pixelライティングが容易に実装可能となっている。 もちろん、使用するシェーダーモデルおよび対応するハードウェアによっては、より複雑で長大なアルゴリズムを実装することもできる。リアルタイムグラフィックスゆえにハードウェア性能に応じたトレードオフにはなるが、単純な局所照明(ローカルイルミネーション)だけでなく、より厳密な物理ベースのレンダリング方程式に基づいた、大域照明(グローバルイルミネーション)モデルをHLSLによるプログラマブルシェーダーで実装することで、より現実に近いリアルタイム3Dコンピューターグラフィックスを実現することも可能となる。さらに、Direct3D 11 (DirectCompute) ではコンピュートシェーダーを使って、GPUにグラフィックス用途以外の汎用計算を行なわせるGPGPUプログラムをHLSLで記述することも可能となる。
なお、HLSLソースファイルには通例.hlsl拡張子が付けられ、ヘッダーファイルには.hlsli拡張子が付けられる[12]。
- ^ Programming guide for HLSL - Win32 apps | Microsoft Learn
- ^ High-level shader language (HLSL) - Win32 apps | Microsoft Learn
- ^ Writing HLSL Shaders in Direct3D 9 - Win32 apps | Microsoft Learn
- ^ Interfaces and Classes - Win32 apps | Microsoft Learn
- ^ 上位レベル シェーダ言語 | Microsoft Learn
- ^ HLSL | Microsoft Learn
- ^ Shader Models vs Shader Profiles - MSDN
- ^ [GDC 2016]シェーダモデル6.0がやってくる! Microsoftが語った「次のDirectX」 - 4Gamer.net
- ^ a b HLSL Shader Model 6.0 (Windows)
- ^ Direct3D 9 から Direct3D 10 への移行時の考慮事項 (Direct3D 10)
- ^ 動的リンク
- ^ .hlsli Extension - List of programs that can open .hlsli files
- ^ Direct3D 9 でのシェーダーの使用 | Microsoft Learn
- ^ GitHub - KhronosGroup/glslang: Khronos-reference front end for GLSL/ESSL, partial front end for HLSL, and a SPIR-V generator.
- ^ HLSL, FXC, and D3DCompile | Games for Windows and the DirectX SDK blog
- ^ Unity での HLSL - Unity 2020.3 マニュアル
- ^ Unity 5.0 - Manual: Writing vertex and fragment shaders
- ^ Unity 5.0 - Manual: Compute Shaders
- ^ コンピュートシェーダー - Unity 2017.4 マニュアル
- ^ Microsoft/DirectXShaderCompiler: This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
- ^ ShaderEffect クラス (System.Windows.Media.Effects)
- ^ ShaderEffect クラス (System.Windows.Media.Effects)
- ^ WPF Version 4 の新機能
- ^ Effects (Windows)
- ^ シェーダー モデルとシェーダー プロファイル
- ^ Direct3D 10 でのシェーダーの使用
- ^ 3Dグラフィックス・マニアックス (4) GPUとシェーダ技術の基礎知識(4) | マイナビニュース
- ^ 【4Gamer.net】[特集]ATIとNVIDIAが語る2005&2006年 ATI編
- ^ テッセレーション (Direct3D 9)
- ^ 4Gamer.net AMD,コードネーム「R600」こと「ATI Radeon HD 2000」を発表
- ^ [連載][西川善司の3Dゲームエクスタシー]「ATI Radeon HD 2000」シリーズのGPUアーキテクチャ徹底解説
- ^ Game Developers Conference(GDC) 2010現地レポート - GAME Watch
- ^ "PDC 2008: メインストリーム アプリケーション向け Direct3D10", Microsoft Corporation, 2008年10月
- ^ D3D_FEATURE_LEVEL enumeration
- ^ Direct3D feature levels
- ^ ダウンレベルのハードウェア上の Direct3D 11
- ^ 10Level9 リファレンス
- ^ Shader Model 5.1
- ^ HLSL Shader Model 5.1 Features for Direct3D 12
- ^ Hardware Feature Levels
- ^ a b Shader Model 3.0, Ashu Rege, NVIDIA Developer Technology Group, 2004.
- ^ a b The Direct3D 10 System, David Blythe, Microsoft Corporation, 2006.
- ^ "Shader Model 3.0 Unleashed", Randima (Randy) Fernando, NVIDIA Developer Technology Group
- ^ テクスチャー オブジェクト (DirectX HLSL)
- ^ “西川善司の3DゲームファンのためのE3最新ハードウェア講座” (日本語). Impress Watch (2011年6月11日). 2011年6月11日閲覧。
- ^ “西川善司の3Dゲームファンのための「プレイステーション 4」グラフィックス」講座(後編)” (日本語). Impress Watch (2013年3月8日). 2014年8月29日閲覧。
- High Level Shading Languageのページへのリンク