From 41713d7719c547278948eae451172100ce4c2c7d Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Thu, 18 Feb 2016 21:41:38 +0000 Subject: [PATCH 1/2] add a sample of FLUX code These samples were taken from the paper "Flux: A Language for Programming High-Performance Servers", by Burns et al and Flux V0.02 which can be found here: https://plasma.cs.umass.edu/emery/flux.1.html --- samples/FLUX/gameserver.fx | 54 +++++++++++++ samples/FLUX/imageserver.fx | 44 +++++++++++ samples/FLUX/mbittorrent.fx | 151 ++++++++++++++++++++++++++++++++++++ samples/FLUX/test.fx | 38 +++++++++ 4 files changed, 287 insertions(+) create mode 100644 samples/FLUX/gameserver.fx create mode 100644 samples/FLUX/imageserver.fx create mode 100644 samples/FLUX/mbittorrent.fx create mode 100644 samples/FLUX/test.fx diff --git a/samples/FLUX/gameserver.fx b/samples/FLUX/gameserver.fx new file mode 100644 index 00000000..9c40d5f1 --- /dev/null +++ b/samples/FLUX/gameserver.fx @@ -0,0 +1,54 @@ +typedef engine isEngineMessage; +typedef turn isTurnMessage; +typedef connect isConnectMessage; +typedef disconnect isDisconnectMessage; + +ClientMessage(char* data) => (); +ParseMessage(char* data) => (int type, int client, char* data); +ReadMessage(int type, int client, char* data) => (); + +ParseEngine(int type, int client, char* data) => (int client, int direction); +DoEngine(int client, int direction) => (); + +ParseTurn(int type, int client, char* data) => (int client, int direction); +DoTurn(int client, int direction) => (); + +ParseConnect(int type, int client, char* data) + => (int client, char* host, int port); +DoConnect(int client, char* host, int port) => (); + +ParseDisconnect(int type, int client, char* data) => (int client); +DoDisconnect(int client) => (); + +UpdateBoard(ClientList clients) => (ClientList clients); +SendData(ClientList clients) => (); + +DoUpdate(ClientList clients) => (); + +DataTimer() => (ClientList clients); + +GetClients() => (ClientList clients); + +Wait(ClientList clients) => (ClientList clients); + +Listen () => (char* data); + +source Listen => ClientMessage; +source DataTimer => DoUpdate; + +DataTimer = GetClients -> Wait; + +DoUpdate = UpdateBoard -> SendData; + +ClientMessage=ParseMessage -> ReadMessage; + +ReadMessage:[engine, _, _] = ParseEngine -> DoEngine; +ReadMessage:[turn, _, _] = ParseTurn -> DoTurn; +ReadMessage:[connect, _, _] = ParseConnect -> DoConnect; +ReadMessage:[disconnect, _, _] = ParseDisconnect -> DoDisconnect; + +atomic GetClients:{client_lock}; +atomic DoConnect:{client_lock}; +atomic DoDisconnect:{client_lock}; + + diff --git a/samples/FLUX/imageserver.fx b/samples/FLUX/imageserver.fx new file mode 100644 index 00000000..effbfaed --- /dev/null +++ b/samples/FLUX/imageserver.fx @@ -0,0 +1,44 @@ +typedef xml TestXML; +typedef html TestHTML; + +typedef inCache TestInCache; + +Page (int socket) => (); + +ReadRequest (int socket) => (int socket, bool close, image_tag *request); + +CheckCache (int socket, bool close, image_tag *request) + => (int socket, bool close, image_tag *request); + +Handler (int socket, bool close, image_tag *request) + => (int socket, bool close, image_tag *request); + +Complete (int socket, bool close, image_tag *request) => (); + +ReadInFromDisk (int socket, bool close, image_tag *request) + => (int socket, bool close, image_tag *request, __u8 *rgb_data); + +Write (int socket, bool close, image_tag *request) + => (int socket, bool close, image_tag *request); + +Compress(int socket, bool close, image_tag *request, __u8 *rgb_data) + => (int socket, bool close, image_tag *request); + +StoreInCache(int socket, bool close, image_tag *request) + => (int socket, bool close, image_tag *request); + +Listen () + => (int socket); + +source Listen => Page; + +Handler:[_, _, inCache]=; +Handler:[_, _, _]=ReadInFromDisk -> Compress -> StoreInCache; + +Page = ReadRequest -> CheckCache-> Handler -> Write -> Complete; + +atomic CheckCache:{cache}; +atomic StoreInCache:{cache}; +atomic Complete:{cache}; + +handle error ReadInFromDisk => FourOhFor; diff --git a/samples/FLUX/mbittorrent.fx b/samples/FLUX/mbittorrent.fx new file mode 100644 index 00000000..620bcd54 --- /dev/null +++ b/samples/FLUX/mbittorrent.fx @@ -0,0 +1,151 @@ +typedef choke TestChoke; +typedef unchoke TestUnchoke; +typedef interested TestInterested; +typedef uninterested TestUninterested; +typedef request TestRequest; +typedef cancel TestCancel; +typedef piece TestPiece; +typedef bitfield TestBitfield; +typedef have TestHave; +typedef piececomplete TestPieceComplete; + +CheckinWithTracker (torrent_data_t *tdata) + => (); + +SendRequestToTracker (torrent_data_t *tdata) + => (torrent_data_t *tdata, int socket); + +GetTrackerResponse (torrent_data_t *tdata, int socket) + => (); + +UpdateChokeList (torrent_data_t *tdata) + => (); + +PickChoked (torrent_data_t *tdata) + => (torrent_data_t *tdata, chokelist_t clist); + +SendChokeUnchoke (torrent_data_t *tdata, chokelist_t clist) + => (); + +SetupConnection (torrent_data_t *tdata, int socket) + => (); + +Handshake (torrent_data_t *tdata, int socket) + => (torrent_data_t *tdata, client_data_t *client); + +SendBitfield (torrent_data_t *tdata, client_data_t *client) + => (); + +Message (torrent_data_t *tdata, client_data_t *client) + => (); + +ReadMessage (torrent_data_t *tdata, client_data_t *client) + => (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload); + +HandleMessage (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (client_data_t *client); + +MessageDone (client_data_t *client) + => (); + +CompletePiece (torrent_data_t *tdata, client_data_t *client, int piece) + => (torrent_data_t *tdata, client_data_t *client); + +VerifyPiece (torrent_data_t *tdata, client_data_t *client, int piece) + => (torrent_data_t *tdata, client_data_t *client, int piece); + +SendHave (torrent_data_t *tdata, client_data_t *client, int piece) + => (torrent_data_t *tdata, client_data_t *client); + +SendUninterested (torrent_data_t *tdata, client_data_t *client) + => (torrent_data_t *tdata, client_data_t *client); + +Choke (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (client_data_t *client); + +Cancel (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (client_data_t *client); + +Interested (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (client_data_t *client); + +Uninterested (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (client_data_t *client); + +Bitfield (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (client_data_t *client); + +Unchoke (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (torrent_data_t *tdata, client_data_t *client); + +SendRequest (torrent_data_t *tdata, client_data_t *client) + => (client_data_t *client); + +Have (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (torrent_data_t *tdata, client_data_t *client); + +Piece (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (torrent_data_t *tdata, client_data_t *client, int piece); + +Request (torrent_data_t *tdata, client_data_t *client, int type, int length, char *payload) + => (client_data_t *client); + +SendKeepAlives (torrent_data_t *tdata) + => (); + +GetClients () + => (int maxfd, fd_set *fds); + +SelectSockets (int maxfd, fd_set *fds) + => (fd_set *fds); + +CheckSockets (fd_set *fds) + => (torrent_data_t *tdata, client_data_t *client); + +TrackerTimer () + => (torrent_data_t *tdata); + +ChokeTimer () + => (torrent_data_t *tdata); + +Connect () + => (torrent_data_t *tdata, int socket); + +KeepAliveTimer () + => (torrent_data_t *tdata); + +Listen () + => (torrent_data_t *tdata, client_data_t *client); + +source TrackerTimer => CheckinWithTracker; +source ChokeTimer => UpdateChokeList; +source Connect => SetupConnection; +source Listen => Message; +source KeepAliveTimer => SendKeepAlives; + +Listen = GetClients -> SelectSockets -> CheckSockets; +CheckinWithTracker = SendRequestToTracker -> GetTrackerResponse; +UpdateChokeList = PickChoked -> SendChokeUnchoke; +SetupConnection = Handshake -> SendBitfield; +Message = ReadMessage -> HandleMessage -> MessageDone; + +CompletePiece:[_, _, piececomplete] = VerifyPiece -> SendHave -> SendUninterested; + +HandleMessage:[_, _, choke, _, _] = Choke; +HandleMessage:[_, _, unchoke, _, _] = Unchoke -> SendRequest; +HandleMessage:[_, _, interested, _, _] = Interested; + +HandleMessage:[_, _, uninterested, _, _] = Uninterested; +HandleMessage:[_, _, request, _, _] = Request; +HandleMessage:[_, _, cancel, _, _] = Cancel; +HandleMessage:[_, _, piece, _, _] = Piece -> CompletePiece -> SendRequest; +HandleMessage:[_, _, bitfield, _, _] = Bitfield; +HandleMessage:[_, _, have, _, _] = Have -> SendRequest; + +atomic GetClients:{BigLock}; +atomic CheckSockets:{BigLock}; +atomic Message:{BigLock}; +atomic CheckinWithTracker:{BigLock}; +atomic UpdateChokeList:{BigLock}; +atomic SetupConnection:{BigLock}; +atomic SendKeepAlives:{BigLock}; diff --git a/samples/FLUX/test.fx b/samples/FLUX/test.fx new file mode 100644 index 00000000..bb81fa9a --- /dev/null +++ b/samples/FLUX/test.fx @@ -0,0 +1,38 @@ +// concrete node signatures +Listen () + => (int socket); + +ReadRequest (int socket) + => (int socket, bool close, image_tag *request); + +CheckCache (int socket, bool close, image_tag *request) + => (int socket, bool close, image_tag *request); + +// omitted for space: +// ReadInFromDisk, StoreInCache +Compress (int socket, bool close, image_tag *request, __u8 *rgb_data) + => (int socket, bool close, image_tag *request); +Write (int socket, bool close, image_tag *request) + => (int socket, bool close, image_tag *request); +Complete (int socket, bool close, image_tag *request) => (); + +// source node +source Listen => Image; + +// abstract node +Image = ReadRequest -> CheckCache -> Handler -> Write -> Complete; + +// predicate type & dispatch +typedef hit TestInCache; +Handler:[_, _, hit] = ; +Handler:[_, _, _] = +ReadInFromDisk -> Compress -> StoreInCache; + +// error handler +handle error ReadInFromDisk => FourOhFor; + +// atomicity constraints +atomic CheckCache:{cache}; +atomic StoreInCache:{cache}; +atomic Complete:{cache}; + From 6b001cf861f31ad51c71274ff332d5a6b84f0d89 Mon Sep 17 00:00:00 2001 From: Erik Faye-Lund Date: Thu, 18 Feb 2016 21:44:21 +0000 Subject: [PATCH 2/2] 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(); + } +}