From 6b001cf861f31ad51c71274ff332d5a6b84f0d89 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Thu, 18 Feb 2016 21:44:21 +0000 Subject: [PATCH] add support for HLSL/FX Add support for DirectX HLSL / FX files. The FX files are just HLSL files with some additional syntax to set render-states and define multiple shader stages in one file. Samples are either written by me, or taken from Chromium. --- lib/linguist/languages.yml | 10 ++ samples/HLSL/accelerated_surface_win.hlsl | 27 +++++ samples/HLSL/corridor.fx | 105 +++++++++++++++++++ samples/HLSL/jellyfish.fx | 119 ++++++++++++++++++++++ samples/HLSL/noise.fx | 41 ++++++++ 5 files changed, 302 insertions(+) create mode 100644 samples/HLSL/accelerated_surface_win.hlsl create mode 100644 samples/HLSL/corridor.fx create mode 100644 samples/HLSL/jellyfish.fx create mode 100644 samples/HLSL/noise.fx diff --git a/lib/linguist/languages.yml b/lib/linguist/languages.yml index 14519a61..7ee17bb0 100644 --- a/lib/linguist/languages.yml +++ b/lib/linguist/languages.yml @@ -1323,6 +1323,16 @@ HCL: ace_mode: ruby tm_scope: source.ruby +HLSL: + type: programming + extensions: + - .fx + - .fxh + - .hlsl + - .hlsli + ace_mode: text + tm_scope: none + HTML: type: markup tm_scope: text.html.basic diff --git a/samples/HLSL/accelerated_surface_win.hlsl b/samples/HLSL/accelerated_surface_win.hlsl new file mode 100644 index 00000000..74d28b8f --- /dev/null +++ b/samples/HLSL/accelerated_surface_win.hlsl @@ -0,0 +1,27 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// To compile these two shaders: +// fxc /E pixelMain /T ps_2_0 accelerated_surface_win.hlsl +// fxc /E vertexMain /T vs_2_0 accelerated_surface_win.hlsl +// +// fxc is in the DirectX SDK. + +struct Vertex { + float4 position : POSITION; + float2 texCoord : TEXCOORD0; +}; + +texture t; +sampler s; + +// Passes a position and texture coordinate to the pixel shader. +Vertex vertexMain(Vertex input) { + return input; +}; + +// Samples a texture at the given texture coordinate and returns the result. +float4 pixelMain(float2 texCoord : TEXCOORD0) : COLOR0 { + return tex2D(s, texCoord); +}; diff --git a/samples/HLSL/corridor.fx b/samples/HLSL/corridor.fx new file mode 100644 index 00000000..476ea4cc --- /dev/null +++ b/samples/HLSL/corridor.fx @@ -0,0 +1,105 @@ +float4x4 matWorldView : WORLDVIEW; +float4x4 matWorldViewProjection : WORLDVIEWPROJECTION; + +struct VS_INPUT { + float4 Position : POSITION0; + float3 Normal : NORMAL; + float3 Tangent : TANGENT; + float3 Binormal : BINORMAL; + float2 TexCoord0 : TEXCOORD0; + float2 TexCoord1 : TEXCOORD1; +}; + +struct VS_OUTPUT { + float4 Position : POSITION0; + float2 TexCoord0 : TEXCOORD0; + float2 TexCoord1 : TEXCOORD1; + float3x3 TangentToView : TEXCOORD2; +}; + +VS_OUTPUT vs_main(VS_INPUT input) +{ + VS_OUTPUT output; + output.Position = mul(input.Position, matWorldViewProjection); + output.TexCoord0 = input.TexCoord0 * 5; + output.TexCoord1 = input.TexCoord1; + output.TangentToView[0] = mul(float4(input.Tangent, 0), matWorldView).xyz; + output.TangentToView[1] = mul(float4(input.Binormal, 0), matWorldView).xyz; + output.TangentToView[2] = mul(float4(input.Normal, 0), matWorldView).xyz; + return output; +} + +struct PS_OUTPUT { + float4 gbuffer0 : COLOR0; + float4 gbuffer1 : COLOR1; +}; + +texture albedo_tex; +sampler albedo_samp = sampler_state { + Texture = (albedo_tex); + MipFilter = Linear; + MinFilter = Linear; + MagFilter = Linear; + AddressU = Wrap; + AddressV = Wrap; + sRGBTexture = True; +}; + +texture normal_tex; +sampler normal_samp = sampler_state { + Texture = (normal_tex); + MipFilter = Linear; + MinFilter = Linear; + MagFilter = Linear; + AddressU = Wrap; + AddressV = Wrap; + sRGBTexture = False; +}; + +texture specular_tex; +sampler specular_samp = sampler_state { + Texture = (specular_tex); + MipFilter = Linear; + MinFilter = Linear; + MagFilter = Linear; + AddressU = Wrap; + AddressV = Wrap; + sRGBTexture = True; +}; + +texture ao_tex; +sampler ao_samp = sampler_state { + Texture = (ao_tex); + MipFilter = Linear; + MinFilter = Linear; + MagFilter = Linear; + AddressU = Wrap; + AddressV = Wrap; + sRGBTexture = True; +}; + +PS_OUTPUT ps_main(VS_OUTPUT Input) +{ + PS_OUTPUT o; + + float3 tangentNormal = normalize(tex2D(normal_samp, Input.TexCoord0).xyz * 2 - 1); + float3 eyeNormal = normalize(mul(tangentNormal, Input.TangentToView)); + + float3 albedo = tex2D(albedo_samp, Input.TexCoord0).rgb; + float ao = tex2D(ao_samp, Input.TexCoord1).r * 0.75; + float spec = tex2D(specular_samp, Input.TexCoord0).r; + + o.gbuffer0 = float4(eyeNormal, spec * ao); + o.gbuffer1 = float4(albedo, 1 - ao); + return o; +} + +technique mesh { + pass Geometry { + VertexShader = compile vs_3_0 vs_main(); + PixelShader = compile ps_3_0 ps_main(); + + AlphaBlendEnable = False; + ZWriteEnable = True; + } +} diff --git a/samples/HLSL/jellyfish.fx b/samples/HLSL/jellyfish.fx new file mode 100644 index 00000000..c1c8359e --- /dev/null +++ b/samples/HLSL/jellyfish.fx @@ -0,0 +1,119 @@ +float4x4 matWorldViewProjection : WORLDVIEWPROJECTION; +float4x4 matWorldView : WORLDVIEW; +float4x4 matWorld : WORLD; +float4x4 matView : VIEW; + +uniform float4 vViewPosition; + +struct VS_INPUT +{ + float3 Pos: POSITION; + float3 Normal: NORMAL; + float3 Tangent: TANGENT; + float3 Binormal: BINORMAL; +}; + +struct VS_OUTPUT +{ + float4 Pos : POSITION; + float3 reflection : TEXCOORD1; + float3 refraction : TEXCOORD2; + float fresnel : TEXCOORD3; +}; + +uniform float3 amt; +uniform float3 scale; +uniform float3 phase; + +float3 deform(float3 p) +{ + float s = 3; + float3 p2 = p * scale + phase; + s += sin(p2.x) * amt.x; + s += sin(p2.y) * amt.y; + s += sin(p2.z) * amt.z; + return p * s / 3; +} + +VS_OUTPUT vs_main( VS_INPUT In ) +{ + VS_OUTPUT Out; + + float3 pos = In.Pos; + float3 norm = In.Normal; + + float3 p1 = pos + In.Tangent * 0.05; + float3 p2 = pos + In.Binormal * 0.05; + pos = deform(pos); + p1 = deform(p1); + p2 = deform(p2); + + p1 -= pos; + p2 -= pos; + norm = normalize(cross(p1, p2)); + + float3 view = normalize(pos - vViewPosition.xyz); + + Out.Pos = mul(float4(pos, 1.0), matWorldViewProjection); + Out.reflection = reflect(view, norm); + Out.refraction = reflect(view, norm * 0.4f); /* fake, but who cares? */ + Out.fresnel = dot(view, norm); + norm = mul(float4(norm, 0.0), matWorldViewProjection); + + return Out; +} + +#define PS_INPUT VS_OUTPUT + +#if 0 +textureCUBE reflectionMap; +samplerCUBE reflectionMapSampler = sampler_state +{ + Texture = (reflectionMap); + MipFilter = LINEAR; + MinFilter = LINEAR; + MagFilter = LINEAR; +}; +#else +// textures +texture reflectionMap +< + string type = "CUBE"; + string name = "test_cube.dds"; +>; + +samplerCUBE reflectionMapSampler = sampler_state +{ + Texture = (reflectionMap); + MipFilter = LINEAR; + MinFilter = LINEAR; + MagFilter = LINEAR; +}; +#endif + +struct PS_OUTPUT +{ + float4 color : COLOR0; +}; + +PS_OUTPUT ps_main( PS_INPUT In ) +{ + PS_OUTPUT Out; + + float4 reflection = texCUBE(reflectionMapSampler, normalize(In.reflection)) * 1.5; + float4 refraction = texCUBE(reflectionMapSampler, normalize(In.refraction)); + float fresnel = In.fresnel; +// float fresnel = abs(normalize(In.normal).z); + Out.color = lerp(reflection, refraction, fresnel) * pow(1.0 - fresnel * 0.75, 1.0); + + return Out; +} + +technique blur_ps_vs_2_0 +{ + pass P0 + { + VertexShader = compile vs_2_0 vs_main(); + PixelShader = compile ps_2_0 ps_main(); + } +} diff --git a/samples/HLSL/noise.fx b/samples/HLSL/noise.fx new file mode 100644 index 00000000..be0303c1 --- /dev/null +++ b/samples/HLSL/noise.fx @@ -0,0 +1,41 @@ +float alpha = 1.f; + +texture tex; +sampler tex_sampler = sampler_state +{ + Texture = (tex); + MipFilter = LINEAR; + MinFilter = LINEAR; + MagFilter = LINEAR; + + AddressU = WRAP; + AddressV = WRAP; +}; + +struct VS_OUTPUT +{ + float4 pos : POSITION; + float2 tex : TEXCOORD1; +}; + +VS_OUTPUT vertex(float4 ipos : POSITION, float2 tex : TEXCOORD0) +{ + VS_OUTPUT Out; + Out.pos = ipos; + Out.tex = tex * 2; + return Out; +} + +float4 pixel(VS_OUTPUT In) : COLOR +{ + return tex2D(tex_sampler, In.tex) * alpha; +} + +technique blur_ps_vs_2_0 +{ + pass P0 + { + VertexShader = compile vs_2_0 vertex(); + PixelShader = compile ps_2_0 pixel(); + } +}