D3D HLSL 一段代码注释

// FixedFuncEMU.fx
// Copyright (c) 2005 Microsoft Corporation. All rights reserved.
//

struct VSSceneIn
{
float3 pos : POSITION; //position of the particle
float3 norm : NORMAL; //velocity of the particle
float2 tex : TEXTURE0; //tex coords
};

struct VSSceneOut
{
float4 pos : SV_Position; //position
float2 tex : TEXTURE0; //texture coordinate
float3 wPos : TEXTURE1; //world space pos
float3 wNorm : TEXTURE2; //world space normal
float4 colorD : COLOR0; //color for gouraud and flat shading
float4 colorS : COLOR1; //color for specular
float fogDist : FOGDISTANCE; //distance used for fog calculations
float3 planeDist : SV_ClipDistance0; //clip distance for 3 planes
};

struct PSSceneIn
{
float4 pos : SV_Position; //position
float2 tex : TEXTURE0; //texture coordinate
float3 wPos : TEXTURE1; //world space pos
float3 wNorm : TEXTURE2; //world space normal
float4 colorD : COLOR0; //color for gouraud and flat shading
float4 colorS : COLOR1; //color for specular
float fogDist : FOGDISTANCE; //distance used for fog calculations
};

struct Light
{
float4 Position;
float4 Diffuse;
float4 Specular;
float4 Ambient;
float4 Atten;
};

#define FOGMODE_NONE 0
#define FOGMODE_LINEAR 1
#define FOGMODE_EXP 2
#define FOGMODE_EXP2 3
#define E 2.71828

cbuffer cbLights
{
float4 g_clipplanes[3];
Light g_lights[8];
};

cbuffer cbPerFrame
{
float4x4 g_mWorld;
float4x4 g_mView;
float4x4 g_mProj;
float4x4 g_mInvProj;
float4x4 g_mLightViewProj;
};

cbuffer cbPerTechnique
{
bool g_bEnableLighting = true;
bool g_bEnableClipping = true;
bool g_bPointScaleEnable = false;
float g_pointScaleA;
float g_pointScaleB;
float g_pointScaleC;
float g_pointSize;

//fog params
int g_fogMode = FOGMODE_NONE;
float g_fogStart;
float g_fogEnd;
float g_fogDensity;
float4 g_fogColor;
};

cbuffer cbPerViewChange
{
//viewport params
float g_viewportHeight;
float g_viewportWidth;
float g_nearPlane;
};

cbuffer cbImmutable
{
float3 g_positions[4] =
{
float3( -0.5, 0.5, 0 ),
float3( 0.5, 0.5, 0 ),
float3( -0.5, -0.5, 0 ),
float3( 0.5, -0.5, 0 ),
};
};

Texture2D g_txDiffuse;
Texture2D g_txProjected;
SamplerState g_samLinear
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Clamp;
AddressV = Clamp;
};

DepthStencilState DisableDepth
{
DepthEnable = FALSE;
DepthWriteMask = ZERO;
};

DepthStencilState EnableDepth
{
DepthEnable = TRUE;
DepthWriteMask = ALL;
};

struct ColorsOutput
{
float4 Diffuse;
float4 Specular;
};

ColorsOutput CalcLighting( float3 worldNormal, float3 worldPos, float3 cameraPos )
{
ColorsOutput output = (ColorsOutput)0.0;

for(int i=0; i<8; i++)
{
float3 toLight = g_lights[i].Position.xyz - worldPos;
float lightDist = length( toLight );
float fAtten = 1.0/dot( g_lights[i].Atten, float4(1,lightDist,lightDist*lightDist,0) );
float3 lightDir = normalize( toLight );
float3 halfAngle = normalize( normalize(-cameraPos) + lightDir );

output.Diffuse += max(0,dot( lightDir, worldNormal ) * g_lights[i].Diffuse * fAtten) + g_lights[i].Ambient;
output.Specular += max(0,pow( dot( halfAngle, worldNormal ), 64 ) * g_lights[i].Specular * fAtten );
}

return output;
}

//
// VS for emulating fixed function pipeline
//
VSSceneOut VSScenemain(VSSceneIn input)
{
VSSceneOut output = (VSSceneOut)0.0;

//output our final position in clipspace
float4 worldPos = mul( float4( input.pos, 1 ), g_mWorld );      //先对齐为float4类型,然后将模型输入的局部坐标经过左乘世界矩阵,转换为世界坐标worldPos
float4 cameraPos = mul( worldPos, g_mView ); //再将世界坐标转换为观察坐标,也是左乘。
output.pos = mul( cameraPos, g_mProj );//进行投影转换

//save world pos for later
output.wPos = worldPos;

//save the fog distance for later
output.fogDist = cameraPos.z;

//find our clipping planes (fixed function clipping is done in world space)
if( g_bEnableClipping )
{
worldPos.w = 1;

//calc the distance from the 3 clipping planes
output.planeDist.x = dot( worldPos, g_clipplanes[0] );
output.planeDist.y = dot( worldPos, g_clipplanes[1] );
output.planeDist.z = dot( worldPos, g_clipplanes[2] );
}
else
{
output.planeDist.x = 1;
output.planeDist.y = 1;
output.planeDist.z = 1;
}

//do gouraud lighting
if( g_bEnableLighting )
{
float3 worldNormal = normalize( mul( input.norm, (float3x3)g_mWorld ) );
output.wNorm = worldNormal;
ColorsOutput cOut = CalcLighting( worldNormal, worldPos, cameraPos );
output.colorD = cOut.Diffuse;
output.colorS = cOut.Specular;
}
else
{
output.colorD = float4(1,1,1,1);
}

//propogate texture coordinate
output.tex = input.tex;

return output;
}

//
// VS for rendering in screen space
//
PSSceneIn VSScreenSpacemain(VSSceneIn input)
{
PSSceneIn output = (PSSceneIn)0.0;

//output our final position
output.pos.x = (input.pos.x / (g_viewportWidth/2.0)) -1;
output.pos.y = -(input.pos.y / (g_viewportHeight/2.0)) +1;
output.pos.z = input.pos.z;
output.pos.w = 1;

//propogate texture coordinate
output.tex = input.tex;
output.colorD = float4(1,1,1,1);

return output;
}

//
// GS for flat shaded rendering
//

[maxvertexcount(3)]
void GSFlatmain( triangle VSSceneOut input[3], inout TriangleStream<VSSceneOut> FlatTriStream )
{
VSSceneOut output;

//
// Calculate the face normal
//
float3 faceEdgeA = input[1].wPos - input[0].wPos;
float3 faceEdgeB = input[2].wPos - input[0].wPos;

//
// Cross product
//
float3 faceNormal = cross(faceEdgeA, faceEdgeB);

//
//calculate the face center
//
float3 faceCenter = (input[0].wPos + input[1].wPos + input[2].wPos)/3.0;

//find world pos and camera pos
float4 worldPos = float4( faceCenter, 1 );
float4 cameraPos = mul( worldPos, g_mView );

//do shading
float3 worldNormal = normalize( faceNormal );
ColorsOutput cOut = CalcLighting( worldNormal, worldPos, cameraPos );

for(int i=0; i<3; i++)
{
output = input[i];
output.colorD = cOut.Diffuse;
output.colorS = cOut.Specular;

FlatTriStream.Append( output );
}
FlatTriStream.RestartStrip();
}

//
// GS for point rendering
//
[maxvertexcount(12)]
void GSPointmain( triangle VSSceneOut input[3], inout TriangleStream<VSSceneOut> PointTriStream )
{
VSSceneOut output;

//
// Calculate the point size
//
//float fSizeX = (g_pointSize/g_viewportWidth)/4.0;
float fSizeY = (g_pointSize/g_viewportHeight)/4.0;
float fSizeX = fSizeY;

for(int i=0; i<3; i++)
{
output = input[i];

//find world pos and camera pos
float4 worldPos = float4(input[i].wPos,1);
float4 cameraPos = mul( worldPos, g_mView );

//find our size
if( g_bPointScaleEnable )
{
float dEye = length( cameraPos.xyz );
fSizeX = fSizeY = g_viewportHeight * g_pointSize *
sqrt( 1.0f/( g_pointScaleA + g_pointScaleB*dEye + g_pointScaleC*(dEye*dEye) ) );
}

//do shading
if(g_bEnableLighting)
{
float3 worldNormal = input[i].wNorm;
ColorsOutput cOut = CalcLighting( worldNormal, worldPos, cameraPos );

output.colorD = cOut.Diffuse;
output.colorS = cOut.Specular;
}
else
{
output.colorD = float4(1,1,1,1);
}

output.tex = input[i].tex;

//
// Emit two new triangles
//
for(int i=0; i<4; i++)
{
float4 outPos = mul( worldPos, g_mView );
output.pos = mul( outPos, g_mProj );
float zoverNear = (outPos.z)/g_nearPlane;
float4 posSize = float4( g_positions[i].x*fSizeX*zoverNear,
g_positions[i].y*fSizeY*zoverNear,
0,
0 );
output.pos += posSize;

PointTriStream.Append(output);
}
PointTriStream.RestartStrip();
}
}

//
// Calculates fog factor based upon distance
//
float CalcFogFactor( float d )
{
float fogCoeff = 1.0;

if( FOGMODE_LINEAR == g_fogMode )
{
fogCoeff = (g_fogEnd - d)/(g_fogEnd - g_fogStart);
}
else if( FOGMODE_EXP == g_fogMode )
{
fogCoeff = 1.0 / pow( E, d*g_fogDensity );
}
else if( FOGMODE_EXP2 == g_fogMode )
{
fogCoeff = 1.0 / pow( E, d*d*g_fogDensity*g_fogDensity );
}

return clamp( fogCoeff, 0, 1 );
}

//
// PS for rendering with clip planes
//
float4 PSScenemain(PSSceneIn input) : SV_Target
{
//calculate the fog factor
float fog = CalcFogFactor( input.fogDist );

//calculate the color based off of the normal, textures, etc
float4 normalColor = g_txDiffuse.Sample( g_samLinear, input.tex ) * input.colorD + input.colorS;

//calculate the color from the projected texture
float4 cookieCoord = mul( float4(input.wPos,1), g_mLightViewProj );
//since we don‘t have texldp, we must perform the w divide ourselves befor the texture lookup
cookieCoord.xy = 0.5 * cookieCoord.xy / cookieCoord.w + float2( 0.5, 0.5 );
float4 cookieColor = float4(0,0,0,0);
if( cookieCoord.z > 0 )
cookieColor = g_txProjected.Sample( g_samLinear, cookieCoord.xy );

//for standard light-modulating effects just multiply normalcolor and coookiecolor
normalColor += cookieColor;

return fog * normalColor + (1.0 - fog)*g_fogColor;
}

//
// PS for rendering with alpha test
//
float4 PSAlphaTestmain(PSSceneIn input) : SV_Target
{
float4 color = g_txDiffuse.Sample( g_samLinear, input.tex ) * input.colorD;
if( color.a < 0.5 )
discard;
return color;
}

//
// RenderSceneGouraud - renders gouraud-shaded primitives
//
technique10 RenderSceneGouraud
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );

SetDepthStencilState( EnableDepth, 0 );
}
}

//
// RenderSceneFlat - renders flat-shaded primitives
//
technique10 RenderSceneFlat
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
SetGeometryShader( CompileShader( gs_4_0, GSFlatmain() ) );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );

SetDepthStencilState( EnableDepth, 0 );
}
}

//
// RenderScenePoint - replaces d3dfill_point
//
technique10 RenderScenePoint
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
SetGeometryShader( CompileShader( gs_4_0, GSPointmain() ) );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );

SetDepthStencilState( EnableDepth, 0 );
}
}

//
// RenderScreneSpace - shows how to render something in screenspace
//
technique10 RenderScreenSpaceAlphaTest
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScreenSpacemain() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PSAlphaTestmain() ) );

SetDepthStencilState( DisableDepth, 0 );
}
}

//
// RenderScreneSpace - shows how to render something in screenspace
//
technique10 RenderTextureOnly
{
pass p0
{
SetVertexShader( CompileShader( vs_4_0, VSScenemain() ) );
SetGeometryShader( NULL );
SetPixelShader( CompileShader( ps_4_0, PSScenemain() ) );

SetDepthStencilState( EnableDepth, 0 );
}
}

D3D HLSL 一段代码注释

时间: 2024-09-27 16:15:04

D3D HLSL 一段代码注释的相关文章

书中一段代码的注释

取自<Focus On 3D Terrain Programming>中的一段: //-------------------------------------------------------------- // Name: CTERRAIN::FilterHeightBand - private // Description: Apply the erosion filter to an individual // band of height values // Arguments:

词法分析器:代码注释

前沿:词法分析器是将一段程序的代码按照类别分开.一般来说是将关键字, 变量名  , 常数 运算符( + _ * / )和界符分类词法分析算是编译的基础把今天上编译原理的实验课, 看了看  老师给的代码 添加了一些注释大致的流程是这样的:规定关键字的符号是10数字的符号是数字本身+ - *  = 这些符号代码中的case里面有(分别是13 14 ...),可以看懂的首先, 把程序存到制定的内存区域, 这里是划出了一个连续的空间(放到字符数组);然后再按字节读取里面的内容 , 当读到空格(" &qu

代码注释主要有哪几种

注释(annotation) 注释就是对代码的解释和说明.目的是为了让别人和自己很容易看懂.为了让别人一看就知道这段代码是做什么用的对书籍或文章的语汇.内容.背景.引文作介绍.评议的文字. 最基本的注释 单行注释 // 注释的内容 多行注释 /* 注释 内容 */ 程序代码 1.HTML语言 使用<!--     --> 来注释,把要注释的话写在中间 程序代码 和 程序代码 < *> 3.vbscript 语言 使用英文单引号 ' 后面写要注释的内容 程序代码 4.asp 语言使用

当程序员说“这代码写的可真烂”,他们的意思是“这烂代码不是我写的”。而当他们说这段代码有些“小问题”时,很可能这代码是他们自己写的

英文原文:What Programmers Say vs. What They Mean 你是否听到过同事说“这段代码不言自明”?你的同事的这句话的实际意思是这段代码不需要写注释. 你也许注意到了,很多时候,程序员所说的话的字面意思和其真实的意思是完全不同的.不用惊异,下面你将很快知道这些暧昧的短语和其深层次的意思都是什么. 最近 Imgur 上出现了一张图片,里面列举的程序员的一些专业术语和其含义,它能很好的帮助你理解这些话的真实意思.这里是对其中的精华进行的总结. 典型的程序员之间的对话 当

那些令人喷饭的代码注释:仅以此代码献给...

程序源代码中的注释经常是一个卧虎藏龙的地方,有人就很喜欢写幽默搞笑的注释内容.解释代码含义的同时,也带给人轻松神经的机会,确实是很有意思的风格 ,来看看这一辑国外某公司产品中的注释. 注意:看的时候严禁喝水或进食. 1 .亲爱的代码维护人员: 当您尝试优化这段代码但发现这是一个极端错误的决定的时候,请修改下面的计时器,以便警示后人. 总计浪费在这段代码的时间 = 16小时 2 .真的很有问题 3 .谨以此代码献给我的妻子达琳,感谢她一直支持我,还有我三个孩子和一只狗. 4 .神奇代码,请勿改动

了解HTML的代码注释

什么是代码注释?代码注释的作用是帮助程序员标注代码的用途,过一段时间后再看你所编写的代码,就能很快想起这段代码的用途. 代码注释不仅方便程序员自己回忆起以前代码的用途,还可以帮助其他程序员很快的读懂你的程序的功能,方便多人合作开发网页代码. 语法: <!--注释文字 --> 示例: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="

8,了解HTML的代码注释

了解HTML的代码注释 什么是代码注释?代码注释的作用是帮助程序员标注代码的用途,过一段时间后再看你所编写的代码,就能很快想起这段代码的用途.代码注释不仅方便程序员自己回忆起以前代码的用途,还可以帮助其他程序员很快的读懂你的程序的功能,方便多人合作开发网页代码. 语法: <!--注释文字 --> 如:右边编辑器的代码的第 8.12 行都是,但是你会发现注释代码是不会在结果窗口中显示出来的.

js/javascript代码注释规范与示例

注释在代码编写过程中的重要性,写代码超过半年的就能深深的体会到.没有注释的代码都不是好代码.为了别人学习,同时为了自己以后对代码进行‘升级’,看看js/javascript代码注释规范与示例.来自:http://www.56.com/style/-doc-/v1/tpl/js_dev_spec/spec-comment.html 文件注释 文件注释位于文件的最前面,应包括文件的以下信息:概要说明及版本(必须)项目地址(开源组件必须)版权声明(必须)开源协议(开源组件必须)版本号(必须)修改时间(

关于异步回调的一段代码及相关总结

昨天遇到了一个问题,就是想在外部js里实现页面加载完了就执行js,类似于jquery中$或者window.onload,当然和window.onload还有所不同,因为window.onload是在图片啊其他的资源加载完了才开始执行的,而我想在尽可能快的执行,问题可以用一段代码来描叙: 1 <html> 2 <head> 3 <script src="test.js“></script> 4 5 </head> 6 <body&g