private void CreateWorley() { List <float> points = new List <float>(); Random rnd = new Random(); for (int i = 0; i < 240; i++) { points.Add((float)rnd.NextDouble()); } GL.DeleteTexture(owner.model.meshes[0].textures[0].textureID); owner.model.meshes[0].textures[0] = CLFilterAPI.GetInstance().GetWorley(points, 512, 512); }
void JumpTo(int index, bool keepBuffer = false) { //currentArgStack.Push(null); jumpstack.Push(new Tuple <int, MemoryBuffer, Stack <object> >(currentIndex, activeBuffer, currentArgStack)); stepResult.hasJumped = true; int size = (int)activeBuffer.Size; if (!keepBuffer) { activeBuffer = CLFilterAPI.CreateEmpty <byte>(size, MemoryFlag.ReadWrite); } currentIndex = index; currentWord = 1; }
private void cmd_writerandom() { if (currentArgStack.Count == 0) { CLFilterAPI.WriteRandomByte(activeBuffer, channelEnableStates); } while (currentArgStack.Count != 0) { object obj = currentArgStack.Pop(); if (!(obj is MemoryBuffer)) { throw new InvalidOperationException("Argument has the wrong type: " + obj.ToString()); } CLFilterAPI.WriteRandomByte(obj as MemoryBuffer, channelEnableStates); } }
public void Reset(string file, MemoryBuffer input, int width, int height, int depth, int channelCount, bool ignoreDebug = false) { this.Log($"Initializing FilterLang Interpreter", DebugChannel.Log, 8); activeBuffer = input; this.ignoreDebug = ignoreDebug; this.width = width; this.height = height; this.depth = depth; this.channelCount = channelCount; channelEnableStates = new byte[channelCount]; for (int i = 0; i < channelCount; i++) { channelEnableStates[i] = 1; } chEnableStateBuffer = CLFilterAPI.CreateBuffer(channelEnableStates, MemoryFlag.ReadOnly | MemoryFlag.CopyHostPointer); currentArgStack = new Stack <object>(); size = width * height * depth * channelCount; foreach (KeyValuePair <string, MemoryBuffer> memoryBuffer in defines) { memoryBuffer.Value.Dispose(); } jumpstack.Clear(); defines.Clear(); defines.Add("in", input); jumpLocations.Clear(); LoadSource(file); ParseDefines(); ParseJumpLocations(); Reset(); }
private void cmd_setactive() { if (currentArgStack.Count < 1) { throw new InvalidOperationException("Specify the buffer you want to activate"); } byte[] temp = new byte[channelCount]; while (currentArgStack.Count != 1) { object val = currentArgStack.Pop(); if (!(val is decimal)) { throw new InvalidOperationException("Invalid channel Arguments"); } byte channel = (byte)Convert.ChangeType(val, typeof(byte)); if (channel >= channelCount) { this.Log("Script is enabling channels beyond channel count. Ignoring...", DebugChannel.Warning, 10); } else { temp[channel] = 1; } } if (currentArgStack.Peek() == null || (!(currentArgStack.Peek() is MemoryBuffer) && !(currentArgStack.Peek() is decimal))) { throw new InvalidOperationException("Specify the buffer you want to activate"); } if (currentArgStack.Peek() is decimal) { byte channel = (byte)Convert.ChangeType(currentArgStack.Pop(), typeof(byte)); temp[channel] = 1; } else { activeBuffer = (MemoryBuffer)currentArgStack.Pop(); } bool needCopy = false; for (int i = 0; i < channelCount; i++) { if (channelEnableStates[i] != temp[i]) { needCopy = true; break; } } if (needCopy) { this.Log("Updating Channel Buffer", DebugChannel.Log, 10); channelEnableStates = temp; CLFilterAPI.WriteToBuffer(chEnableStateBuffer, channelEnableStates); //temp = CLFilterAPI.ReadBuffer<byte>(chEnableStateBuffer); } }
bool Analyze(string code) { string[] words = code.Split(" ", StringSplitOptions.RemoveEmptyEntries); string function = words.Length == 0 ? "" : words[0]; CLFilter filter = null; if (function == "") { return(false); } bool isBakedFunction = bakedFunctions.ContainsKey(function); bool isDirectExecute = function == "jmp"; if (!isBakedFunction && !CLFilterAPI.TryGetFilter(function, out filter)) { throw new NotImplementedException("Argument Not found in line " + code + "."); } if (leaveStack) //This keeps the stack when returning from a "function" { leaveStack = false; } else { currentArgStack = new Stack <object>(); } bool ret = true; for (; currentWord < words.Length; currentWord++) //loop through the words. start value can be != 0 when returning from a function specified as an argument to a kernel { if (AnalyzeWord(words[currentWord], out object val)) { JumpTo(jumpLocations[words[currentWord]], isDirectExecute); ret = false; //We Jumped to another point in the code. currentArgStack.Push(null); //Push null to signal the interpreter that he returned before assigning the right value. break; } else { currentArgStack.Push(val); //push the value to the stack } } if (currentWord == words.Length && ret) //We finished parsing the line and we didnt jump. { if (isBakedFunction) { bakedFunctions[function](); //Execute baked function } else if (filter == null || words.Length - 1 != filter.parameter.Count - 4) { throw new Exception("Not the right amount of arguments."); } else { //Execute filter for (int i = filter.parameter.Count - 1; i >= 4; i--) { object obj = currentArgStack.Pop(); //Get the arguments and set them to the kernel filter.SetArg(i, obj); } this.Log("Running kernel: " + filter.name, DebugChannel.Log, 10); filter.Run(cq, activeBuffer, new int3(width, height, depth), chEnableStateBuffer, channelCount); //Running the kernel byte[] buf = cq.EnqueueReadBuffer <byte>(activeBuffer, (int)activeBuffer.Size); } } return(ret); }
void ParseDefines() { for (int i = source.Count - 1; i >= 0; i--) { if (source[i].StartsWith(defineKey)) { string[] kvp = source[i].Remove(0, defineKey.Length).Split(':'); if (kvp.Length < 2) { throw new Exception("Invalid Define statement at line " + i); } string varname = kvp[0].Trim(); if (defines.ContainsKey(varname)) { this.Log("Overwriting " + varname, DebugChannel.Warning, 10); defines.Remove(varname); } MemoryFlag flags = MemoryFlag.ReadWrite; string[] flagTest = varname.Split(' '); if (flagTest.Length > 1) { varname = flagTest[1]; if (flagTest[0] == "r") { flags = MemoryFlag.ReadOnly; } else if (flagTest[0] == "w") { flags = MemoryFlag.WriteOnly; } } string[] args = kvp[1].Split(' ', StringSplitOptions.RemoveEmptyEntries); string filename = args[0].Trim(); if (IsSurroundedBy(filename, "\"")) { defines.Add(varname, CLFilterAPI.CreateFromImage((Bitmap)Image.FromFile(filename.Replace("\"", "")), MemoryFlag.CopyHostPointer | flags)); } else if (filename == "random") { defines.Add(varname, CLFilterAPI.CreateRandomByte(size, channelEnableStates, MemoryFlag.CopyHostPointer | flags)); } else if (filename == "empty") { defines.Add(varname, CLFilterAPI.CreateEmpty <byte>(size, MemoryFlag.CopyHostPointer | flags)); } else if (filename == "wfc") { if (args.Length < 10) { throw new Exception("Invalid Define statement at line " + i); } if (!int.TryParse(args[2], out int n)) { throw new Exception("Invalid N argument"); } if (!int.TryParse(args[3], out int width)) { throw new Exception("Invalid width argument"); } if (!int.TryParse(args[4], out int height)) { throw new Exception("Invalid height argument"); } if (!bool.TryParse(args[5], out bool periodicInput)) { throw new Exception("Invalid periodicInput argument"); } if (!bool.TryParse(args[6], out bool periodicOutput)) { throw new Exception("Invalid periodicOutput argument"); } if (!int.TryParse(args[7], out int symetry)) { throw new Exception("Invalid symetry argument"); } if (!int.TryParse(args[8], out int ground)) { throw new Exception("Invalid ground argument"); } if (!int.TryParse(args[9], out int limit)) { throw new Exception("Invalid limit argument"); } WaveFunctionCollapse wfc = new WFCOverlayMode(args[1].Trim().Replace("\"", ""), n, width, height, periodicInput, periodicOutput, symetry, ground); wfc.Run(limit); Bitmap bmp = new Bitmap(wfc.Graphics(), new Size(this.width, this.height)); //Apply scaling defines.Add(varname, CLFilterAPI.CreateFromImage(bmp, MemoryFlag.CopyHostPointer | flags)); } else { throw new InvalidOperationException("Can not resolve symbol: " + varname + " in line " + i); } } } }
public static FilterLangInterpreter CreateWithEmptyInput(string file, int width, int height, int depth, int channelCount, bool ignoreDebug = false) { MemoryBuffer buf = CLFilterAPI.CreateEmpty <byte>(width * height * depth * channelCount, MemoryFlag.CopyHostPointer | MemoryFlag.ReadWrite); return(new FilterLangInterpreter(file, buf, width, height, depth, channelCount, ignoreDebug)); }
private void OnKeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 'r') { GL.DeleteTexture(owner.model.meshes[0].textures[0].textureID); owner.model.meshes[0].textures[0] = GameTexture.Load("runicfloor.png"); isDebuggingInterpreter = false; } else if (e.KeyChar == 'f' || isDebuggingInterpreter) { if (!isDebuggingInterpreter) { string filename = Console.ReadLine(); while (!File.Exists(filename)) { filename = Console.ReadLine(); } fli = FilterLangInterpreter.CreateWithEmptyInput(filename, 512, 512, 1, 4); } FilterLangInterpreter.InterpreterStepResult res = new FilterLangInterpreter.InterpreterStepResult(); if (isDebuggingInterpreter) { this.Log("Continuing Execution", DebugChannel.Log, 10); isDebuggingInterpreter = false; } byte[] buf = null; do { if (res.triggeredDebug) { this.Log("Triggered Debug.", DebugChannel.Log, 10); isDebuggingInterpreter = true; buf = CLFilterAPI.ReadBuffer <byte>(res.debugBuffer); break; } } while (!(res = fli.Step()).terminated); if (!isDebuggingInterpreter) { buf = fli.GetResult(); } else if (buf == null) { throw new InvalidOperationException("F**K"); } GameTexture.Update(owner.model.meshes[0].textures[0], buf, 512, 512); } else if (e.KeyChar == ' ') { CLFilterAPI.GetInstance().RunLightKernel(owner.model.meshes[0].textures[0]); } else if (e.KeyChar == 'o') { CLFilterAPI.GetInstance().RunOverlayKernel(owner.model.meshes[0].textures[0], overlay, 0.5f); } else if (e.KeyChar == 'c') { CLFilterAPI.GetInstance().RunCheckerBoardKernel(owner.model.meshes[0].textures[0], 5f); } else if (e.KeyChar == 'w') { CreateWorley(); } else if (e.KeyChar == 's') { CLFilterAPI.GetInstance().RunSmoothNoise(owner.model.meshes[0].textures[0], 4); } else if (e.KeyChar == 'p') { GL.DeleteTexture(owner.model.meshes[0].textures[0].textureID); owner.model.meshes[0].textures[0] = CLFilterAPI.GetInstance().GetPerlin(512, 512, 6, 0.9f); } else if (e.KeyChar == 't') { string[] filenames = Directory.GetFiles("filter/tests", "*.fl"); this.Log("Running tests...", DebugChannel.Log, 10); foreach (string filename in filenames) { fli = FilterLangInterpreter.CreateWithEmptyInput(filename, 512, 512, 1, 4); this.Log("Running Test File: " + filename, DebugChannel.Log, 10); FilterLangInterpreter.InterpreterStepResult res = new FilterLangInterpreter.InterpreterStepResult(); do { } while (!(res = fli.Step()).terminated); } this.Log("Finished Running Tests", DebugChannel.Log, 10); GL.DeleteTexture(owner.model.meshes[0].textures[0].textureID); owner.model.meshes[0].textures[0] = GameTexture.Load("runicfloor.png"); } }