diff options
Diffstat (limited to 'NvBlast/samples/resources/shaders')
12 files changed, 610 insertions, 0 deletions
diff --git a/NvBlast/samples/resources/shaders/common_buffers.hlsl b/NvBlast/samples/resources/shaders/common_buffers.hlsl new file mode 100644 index 0000000..1aa3386 --- /dev/null +++ b/NvBlast/samples/resources/shaders/common_buffers.hlsl @@ -0,0 +1,28 @@ +#ifndef COMMON_BUFFERS_HLSL +#define COMMON_BUFFERS_HLSL + +cbuffer Camera : register(b0) +{ + row_major matrix viewProjection; + row_major matrix projectionInv; + float3 viewPos; +}; + +cbuffer World : register(b1) +{ + float3 ambientColor; + float3 pointLightPos; + float3 pointLightColor; + float3 dirLightDir; + float specularPower; + float3 dirLightColor; + float specularIntensity; +}; + +cbuffer Object : register(b2) +{ + row_major matrix model; + float4 defaultColor; +}; + +#endif
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/debug_primitive.hlsl b/NvBlast/samples/resources/shaders/debug_primitive.hlsl new file mode 100644 index 0000000..a24ebfb --- /dev/null +++ b/NvBlast/samples/resources/shaders/debug_primitive.hlsl @@ -0,0 +1,31 @@ +#include "common_buffers.hlsl" + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 color : COLOR0; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float3 color : COLOR0; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4 worldSpacePos = mul(float4(iV.position, 1.0f), model); + float4 eyeSpacePos = mul(worldSpacePos, viewProjection); + oV.position = mul(worldSpacePos, viewProjection); + + oV.color = iV.color; + + return oV; +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + return float4(iV.color, 1); +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/lighting.hlsl b/NvBlast/samples/resources/shaders/lighting.hlsl new file mode 100644 index 0000000..2dacc14 --- /dev/null +++ b/NvBlast/samples/resources/shaders/lighting.hlsl @@ -0,0 +1,51 @@ +#include "common_buffers.hlsl" + +static const float att_c = 1.0f; +static const float att_l = 0.014f; +static const float att_q = 0.0007f; + + +float CalcAttenuation(float distance) +{ + return 1 / (att_c + att_l * distance + att_q * distance * distance); +}; + + +float3 CalcLight(float3 textureColor, float3 lightDir, float3 viewDir, float3 normal, float3 lightColor, float specPower, float specIntensity, float attenuation) +{ + normal = normalize(normal); + + // diffuse + float3 dirToLight = normalize(-lightDir); + float diffuseFactor = max(dot(normal, dirToLight), 0.0); + float3 diffuse = lightColor * textureColor * diffuseFactor * attenuation; + + // specular (Blinn-Phong) + float3 halfwayDir = normalize(dirToLight + viewDir); + float specFactor = pow(max(dot(viewDir, halfwayDir), 0.0), specPower); + float3 spec = lightColor * specFactor * attenuation * specIntensity; + + return diffuse + spec; +}; + +float3 CalcPixelLight(float3 diffuseColor, float3 worldPos, float3 normal) +{ + float3 viewDir = normalize(viewPos - worldPos); + + // ambient + float3 ambient = ambientColor * diffuseColor; + + // dir light + float3 dirLight = CalcLight(diffuseColor, dirLightDir, viewDir, normal, dirLightColor, specularPower, specularIntensity, 1); + + // point light + float3 pointLightDir = worldPos - pointLightPos; + float distance = length(pointLightDir); + float attenuation = CalcAttenuation(distance); + float3 pointLight = CalcLight(diffuseColor, pointLightDir, viewDir, normal, pointLightColor, specularPower, specularIntensity, attenuation); + + // hacky hack: ambient attenuates within point light distance + ambient *= attenuation; + + return ambient + dirLight + pointLight; +};
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/model_simple.hlsl b/NvBlast/samples/resources/shaders/model_simple.hlsl new file mode 100644 index 0000000..7d55a72 --- /dev/null +++ b/NvBlast/samples/resources/shaders/model_simple.hlsl @@ -0,0 +1,42 @@ +#include "common_buffers.hlsl" +#include "lighting.hlsl" + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 normal : NORMAL0; + float health : TEXCOORD1; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float4 worldPos : POSITION0; + float3 normal : NORMAL0; + float health : TEXCOORD1; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4 worldSpacePos = mul(float4(iV.position, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + oV.worldPos = worldSpacePos; + + // normals + float3 worldNormal = mul(iV.normal, (float3x3)model); + oV.normal = worldNormal; + + oV.health = iV.health; + + return oV; +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + float3 lightColor = CalcPixelLight(defaultColor.xyz, iV.worldPos.xyz, iV.normal); + lightColor.r = 1.0f - iV.health; // hack for health + return float4(lightColor, 1); +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/model_simple_textured.hlsl b/NvBlast/samples/resources/shaders/model_simple_textured.hlsl new file mode 100644 index 0000000..5167837 --- /dev/null +++ b/NvBlast/samples/resources/shaders/model_simple_textured.hlsl @@ -0,0 +1,76 @@ +#include "common_buffers.hlsl" +#include "lighting.hlsl" + +SamplerState defaultSampler : register(s0); +Texture2D diffuseTexture : register(t0); + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 normal : NORMAL0; + float2 uv : TEXCOORD0; + float health : TEXCOORD1; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float4 worldPos : POSITION0; + float2 uv : TEXCOORD0; + float3 normal : NORMAL0; + float health : TEXCOORD1; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4 worldSpacePos = mul(float4(iV.position, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + oV.worldPos = worldSpacePos; + + // normals + float3 worldNormal = mul(float4(iV.normal, 0.0f), model); + oV.normal = worldNormal; + + oV.uv = iV.uv; + oV.health = iV.health; + + return oV; +} + +float noise2(float2 co) +{ + return frac(sin(dot(co.xy, float2(12.9898,78.233))) * 43758.5453); +} + +float voronoi( float2 x ) +{ + int2 p = floor( x ); + float2 f = frac( x ); + + float res = 8.0; + for( int j=-1; j<=1; j++ ) + for( int i=-1; i<=1; i++ ) + { + int2 b = int2( i, j ); + float2 r = float2( b ) - f + noise2( p + b ); + float d = dot( r, r ); + + res = min( res, d ); + } + return sqrt( res ); +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + float4 textureColor = diffuseTexture.Sample(defaultSampler, iV.uv); + + // health cracks hack + float crack = 1.0f - voronoi(iV.uv * 50.0f); + crack = smoothstep(0.0f, 0.5f, crack); + textureColor = textureColor * lerp(1.0f, crack, (1.0f - iV.health) * 0.7f); + + return float4(CalcPixelLight(textureColor, iV.worldPos, iV.normal), 1); +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/model_skinned.hlsl b/NvBlast/samples/resources/shaders/model_skinned.hlsl new file mode 100644 index 0000000..1e26c95 --- /dev/null +++ b/NvBlast/samples/resources/shaders/model_skinned.hlsl @@ -0,0 +1,54 @@ +#include "common_buffers.hlsl" +#include "lighting.hlsl" + +SamplerState defaultSampler : register(s0); +Texture2D diffuseTexture : register(t0); +Texture2D bonesTexture : register(t1); + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 normal : NORMAL0; + float2 uv : TEXCOORD0; + uint boneIndex : TEXCOORD1; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float4 worldPos : POSITION0; + float2 uv : TEXCOORD0; + float3 normal : NORMAL0; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4x4 boneMatrix; + boneMatrix[0] = bonesTexture.Load(int3(0, iV.boneIndex, 0)); + boneMatrix[1] = bonesTexture.Load(int3(1, iV.boneIndex, 0)); + boneMatrix[2] = bonesTexture.Load(int3(2, iV.boneIndex, 0)); + boneMatrix[3] = bonesTexture.Load(int3(3, iV.boneIndex, 0)); + + float3 skinnedPos = mul(float4(iV.position, 1.0f), boneMatrix); + float4 worldSpacePos = mul(float4(skinnedPos, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + oV.worldPos = worldSpacePos; + + // normals + float3 localNormal = mul(float4(iV.normal, 0.0f), boneMatrix); + float3 worldNormal = mul(float4(localNormal, 0.0f), model); + oV.normal = worldNormal; + + oV.uv = iV.uv; + + return oV; +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + float3 lightColor = CalcPixelLight(defaultColor.xyz, iV.worldPos.xyz, iV.normal); + return float4(lightColor, 1); +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/model_skinned_textured.hlsl b/NvBlast/samples/resources/shaders/model_skinned_textured.hlsl new file mode 100644 index 0000000..2b2bb0e --- /dev/null +++ b/NvBlast/samples/resources/shaders/model_skinned_textured.hlsl @@ -0,0 +1,54 @@ +#include "common_buffers.hlsl" +#include "lighting.hlsl" + +SamplerState defaultSampler : register(s0); +Texture2D diffuseTexture : register(t0); +Texture2D bonesTexture : register(t1); + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 normal : NORMAL0; + float2 uv : TEXCOORD0; + uint boneIndex : TEXCOORD1; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float4 worldPos : POSITION0; + float2 uv : TEXCOORD0; + float3 normal : NORMAL0; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4x4 boneMatrix; + boneMatrix[0] = bonesTexture.Load(int3(0, iV.boneIndex, 0)); + boneMatrix[1] = bonesTexture.Load(int3(1, iV.boneIndex, 0)); + boneMatrix[2] = bonesTexture.Load(int3(2, iV.boneIndex, 0)); + boneMatrix[3] = bonesTexture.Load(int3(3, iV.boneIndex, 0)); + + float3 skinnedPos = mul(float4(iV.position, 1.0f), boneMatrix); + float4 worldSpacePos = mul(float4(skinnedPos, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + oV.worldPos = worldSpacePos; + + // normals + float3 localNormal = mul(float4(iV.normal, 0.0f), boneMatrix); + float3 worldNormal = mul(float4(localNormal, 0.0f), model); + oV.normal = worldNormal; + + oV.uv = iV.uv; + + return oV; +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + float4 textureColor = diffuseTexture.Sample(defaultSampler, iV.uv); + return float4(CalcPixelLight(textureColor, iV.worldPos, iV.normal), 1); +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/physx_primitive.hlsl b/NvBlast/samples/resources/shaders/physx_primitive.hlsl new file mode 100644 index 0000000..03fd137 --- /dev/null +++ b/NvBlast/samples/resources/shaders/physx_primitive.hlsl @@ -0,0 +1,37 @@ +#include "common_buffers.hlsl" +#include "lighting.hlsl" + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 normal : NORMAL0; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float4 worldPos : POSITION0; + float3 normal : NORMAL0; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4 worldSpacePos = mul(float4(iV.position, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + oV.worldPos = worldSpacePos; + + // normals + float3 worldNormal = mul(iV.normal, (float3x3)model); + oV.normal = worldNormal; + + return oV; +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + float3 lightColor = CalcPixelLight(defaultColor.xyz, iV.worldPos.xyz, iV.normal); + return float4(lightColor, 1); +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/physx_primitive_plane.hlsl b/NvBlast/samples/resources/shaders/physx_primitive_plane.hlsl new file mode 100644 index 0000000..3b2ec5d --- /dev/null +++ b/NvBlast/samples/resources/shaders/physx_primitive_plane.hlsl @@ -0,0 +1,64 @@ +#include "common_buffers.hlsl" +#include "lighting.hlsl" + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 normal : NORMAL0; + float2 uv : TEXCOORD0; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float4 worldPos : POSITION0; + float2 uv : TEXCOORD0; + float3 normal : NORMAL0; +}; + +float filterwidth(float2 v) +{ + float2 fw = max(abs(ddx(v)), abs(ddy(v))); + return max(fw.x, fw.y); +} + +float2 bump(float2 x) +{ + return (floor(x/2) + 2.f * max((x/2) - floor(x/2) - .5f, 0.f)); +} + +float checker(float2 uv) +{ + float width = filterwidth(uv); + float2 p0 = uv - 0.5 * width; + float2 p1 = uv + 0.5 * width; + + float2 i = (bump(p1) - bump(p0)) / width; + return i.x * i.y + (1 - i.x) * (1 - i.y); +} + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4 worldSpacePos = mul(float4(iV.position, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + oV.uv = iV.uv; + + oV.worldPos = worldSpacePos; + + // normals + float3 worldNormal = mul(iV.normal, (float3x3)model); + oV.normal = worldNormal; + + return oV; +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + float4 color = defaultColor; + color *= 1.0 - 0.25 * checker(iV.uv); + float3 lightColor = CalcPixelLight(color.xyz, iV.worldPos.xyz, iV.normal); + return float4(lightColor, 1); +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/physx_primitive_transparent.hlsl b/NvBlast/samples/resources/shaders/physx_primitive_transparent.hlsl new file mode 100644 index 0000000..d52392e --- /dev/null +++ b/NvBlast/samples/resources/shaders/physx_primitive_transparent.hlsl @@ -0,0 +1,40 @@ +#include "common_buffers.hlsl" +#include "lighting.hlsl" + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 normal : NORMAL0; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float4 worldPos : POSITION0; + float3 normal : NORMAL0; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4 worldSpacePos = mul(float4(iV.position, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + oV.worldPos = worldSpacePos; + + // normals + float3 worldNormal = mul(iV.normal, (float3x3)model); + oV.normal = worldNormal; + + return oV; +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + float4 color = defaultColor; + float3 viewDir = normalize(viewPos - iV.worldPos.xyz); + float factor = max(0.0f, dot(normalize(iV.normal), viewDir)); + color.a *= factor; + return color; +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/pointsprite.hlsl b/NvBlast/samples/resources/shaders/pointsprite.hlsl new file mode 100644 index 0000000..2bff194 --- /dev/null +++ b/NvBlast/samples/resources/shaders/pointsprite.hlsl @@ -0,0 +1,106 @@ +#include "common_buffers.hlsl" +#include "lighting.hlsl" + +SamplerState linearSampler : register(s0); +SamplerState pointSampler : register(s1); +Texture2D diffuseTexture : register(t0); +Texture2D<float> depthTexture : register(t1); + +static const float POINT_SIZE = 1.00f; +static const float FADE_DISTANCE = 1.0f; + +struct VS_INPUT +{ + float3 position : POSITION0; + float4 color : COLOR0; + float2 scale : TANGENT; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; + float4 color : COLOR0; + float2 uv : TEXCOORD0; + float2 screenPos : TEXCOORD1; + float2 depth : TEXCOORD2; + float2 pointSize : PSIZE; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4 worldSpacePos = mul(float4(iV.position, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + oV.color = iV.color; + oV.uv = float2(0, 0); + + // uncomment to use scale + //oV.pointSize = iV.scale * POINT_SIZE; + oV.pointSize = float2(1, 1) * POINT_SIZE; + + return oV; +} + +static const float4 SPRITE_VERTEX_POSITIONS[4] = +{ + float4( 0.5, -0.5, 0, 0), + float4( 0.5, 0.5, 0, 0), + float4( -0.5, -0.5, 0, 0), + float4( -0.5, 0.5, 0, 0), +}; + +static const float2 SPRITE_VERTEX_TEXCOORDS[4] = +{ + float2(1, 0), + float2(1, 1), + float2(0, 0), + float2(0, 1), +}; + +[maxvertexcount(4)] +void GS( point VS_OUTPUT sprite[1], inout TriangleStream<VS_OUTPUT> triStream ) +{ + VS_OUTPUT v; + + v.color = sprite[0].color; + v.pointSize = sprite[0].pointSize; + + float4 aspectFactor = float4((projection[0][0] / projection[1][1]) * v.pointSize.x, 1.0 * v.pointSize.y, 0, 0); + + [unroll] for(int i = 0; i < 4; ++i) + { + v.position = sprite[0].position + SPRITE_VERTEX_POSITIONS[i] * aspectFactor; + v.screenPos = v.position.xy / v.position.w; + v.depth = v.position.zw; + v.uv = SPRITE_VERTEX_TEXCOORDS[i]; + triStream.Append(v); + } + + triStream.RestartStrip(); +} + +float4 PS(VS_OUTPUT input) : SV_Target0 +{ + // soft particles fade: + float2 screenPos = 0.5*( (input.screenPos) + float2(1,1)); + screenPos.y = 1 - screenPos.y; + + float particleDepth = input.depth.x / input.depth.y; + + float depthSample = depthTexture.Sample(pointSampler, screenPos); + + float4 depthViewSample = mul(float4(input.screenPos, depthSample, 1), projectionInv ); + float4 depthViewParticle = mul(float4(input.screenPos, particleDepth, 1), projectionInv); + + float depthDiff = depthViewSample.z / depthViewSample.w - depthViewParticle.z / depthViewParticle.w; + if( depthDiff < 0 ) + discard; + + float depthFade = saturate( depthDiff / FADE_DISTANCE ); + + float4 textureColor = diffuseTexture.Sample(linearSampler, input.uv) * input.color; + textureColor.a *= depthFade; + return textureColor; +}
\ No newline at end of file diff --git a/NvBlast/samples/resources/shaders/unlit_transparent.hlsl b/NvBlast/samples/resources/shaders/unlit_transparent.hlsl new file mode 100644 index 0000000..0c2158a --- /dev/null +++ b/NvBlast/samples/resources/shaders/unlit_transparent.hlsl @@ -0,0 +1,27 @@ +#include "common_buffers.hlsl" + +struct VS_INPUT +{ + float3 position : POSITION0; + float3 normal : NORMAL0; +}; + +struct VS_OUTPUT +{ + float4 position : SV_POSITION; +}; + +VS_OUTPUT VS(VS_INPUT iV) +{ + VS_OUTPUT oV; + + float4 worldSpacePos = mul(float4(iV.position, 1.0f), model); + oV.position = mul(worldSpacePos, viewProjection); + + return oV; +} + +float4 PS(VS_OUTPUT iV) : SV_Target0 +{ + return defaultColor; +}
\ No newline at end of file |