Skip to content

Instantly share code, notes, and snippets.

View jackmott's full-sized avatar

Jack Mott jackmott

  • Olo
  • Texas,USA
View GitHub Profile
@jackmott
jackmott / ShaderManager.cs
Created March 13, 2017 19:21
Hot Swappable Shaders for MonoGame
/*
HotSwap shader sytem for MonoGame
HotSwap code only exists for debug builds
Edit paths to match your project
Construct in your Initialize method
Add shaders in LoadContent (or whenever)
Call CheckForChanges in Update() or periodically however you like
mgcb.exe usually located in C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools
*/
@jackmott
jackmott / json.fs
Created March 16, 2017 11:26
makes json.net handle options and DUs the way one would expect
namespace AlphaFront
module JsonCustomConverters =
open Newtonsoft.Json
open Microsoft.FSharp.Reflection
open System
open System.IO
type DuConverter() =
inherit JsonConverter()
@jackmott
jackmott / p2d.cpp
Created March 21, 2017 17:17
perlin2dsimd
#include "FastNoise.h"
inline int fastFloor(float x) {
int xi = (int)x;
return x < xi ? xi - 1 : xi;
}
__m128 onef = SetOne(1.0);
__m128i one = SetOnei(1);
@jackmott
jackmott / example.cs
Created March 23, 2017 13:51
c# 7 pattern matching
switch (SomeClass)
{
case DerivedClassA a:
a.dostuff();
break;
case DerivedClassB b:
b.dostuff();
break;
}
@jackmott
jackmott / minby.cs
Last active March 24, 2017 02:32
Handy way to do minby / maxby extension methods in C# 7
public static (T obj, float value) MinBy<T>(this T[] array,Func<T,float> lambda)
{
float minValue = float.MaxValue;
T minT = default(T);
//note if you implement this for List<T>, don't use foreach, it is slower. Use a for loop.
foreach (var t in array)
{
var value = lambda.Invoke(t);
if (value < minValue)
{
@jackmott
jackmott / sprites.cs
Created March 24, 2017 19:23
composing sprite sheets with monogame
// Compose a bunch of sprite sheets to make a uniqe npc as they
// come on screen. The sheets contain all the animation frames.
Texture2D shirtSheet = content.Load<Texture2D>(RandUtil.Index(shirtPaths));
Texture2D pantSheet = content.Load<Texture2D>(RandUtil.Index(pantsPaths));
Texture2D shoeSheet = content.Load<Texture2D>(RandUtil.Index(shoePaths));
var renderTarget = new RenderTarget2D(PRPGame.graphics,baseSheet.Width,baseSheet.Height,false,SurfaceFormat.Color,DepthFormat.None,0,RenderTargetUsage.DiscardContents);
PRPGame.graphics.SetRenderTarget(renderTarget);
PRPGame.graphics.Clear(Color.Transparent);
@jackmott
jackmott / fastforeach.cs
Created March 26, 2017 03:18
fast foreach
public static void ForEach<T>(this T[] array, Action<T> lambda)
{
foreach (var x in array)
{
lambda.Invoke(x);
}
}
//use like
@jackmott
jackmott / testmap.js
Last active March 28, 2017 15:08
javascript map vs imperative testing
<script>
//All tests run repeatedly until numbers stabilize (jit warmup)
//Node 7.7.4 NODE_ENV = production - 42ms
//Firefox 52.0.1 32bit 250 to 280ms
//Firefox 52.0.1 64bit 210 to 250 ms
//Chrome 57.0.2987.110 (64-bit) 45ms
function testImperative(values)
{
@jackmott
jackmott / simdandjits.txt
Last active March 28, 2017 16:19
SIMD and jits
With no JIT that I am aware of (not jvm, not ryujit, not v8) can you make say, _mm256_sll_epi32 happen, at all.
With C++ I can call that with an intrinsic: `_mm_and_ps(x,y);` Now is it a pain, because to hit the various SIMD targets, I have
to write the SSE2 and AVX512 equivalents and detect which to use at runtime? Yes, it is a pain, though libraries like bSIMD exist
to remove that pain.
Also, no JIT that I am aware of can autovectorize something simple like a for loop adding up floats. The JVM doesn't do it, .NET doesn't
auto vectorize any user code ever. There isn't time to figure those out at runtime.
GCC, MSVCC, and LLVM, and Intel AOT compilers can all auto-vectorize simple cases like that, and Intel can sometimes do rather
amazing autovectorization.
@jackmott
jackmott / map.fs
Created April 3, 2017 22:52
map benchmark
open System.Collections.Concurrent
open System.Threading.Tasks
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Running
open BenchmarkDotNet.Jobs
open System
// Learn more about F# at http://fsharp.org
// See the 'F# Tutorial' project for more help.