private static BigInteger GetPrime(int bits, Ibasa.Numerics.Random.Generator generator) { BigInteger prime; do { prime = Primes.GetPrime(bits, generator); } while (prime % 4 != 3); return prime; }
static void ProgramCallback(Ibasa.OpenCL.Program program, object user_data) { Console.WriteLine("Program built."); var device = program.Devices[0]; Console.WriteLine(device.Name); var info = program.GetBuildInfo(device); Console.WriteLine(info.BuildOptions); Console.WriteLine(info.BuildStatus); Console.WriteLine(info.Log); }
public static bool IsProbablyPrime(this BigInteger prime, int certainty, Ibasa.Numerics.Random.Generator random) { if (prime == 2 || prime == 3) return true; if (prime < 2 || prime % 2 == 0) return false; if (prime <= long.MaxValue) return IsProbablyPrime((long)prime, certainty, random); int s = 0; var d = prime - 1; while (d % 2 == 0) { d /= 2; s += 1; } byte[] bytes = new byte[prime.ToByteArray().Length]; for (int i = 0; i < certainty; ++i) { BigInteger a; do { random.NextBytes(bytes); a = new BigInteger(bytes); } while (a < 2 || a >= prime - 2); BigInteger x = BigInteger.ModPow(a, d, prime); if (x != 1 && x != prime - 1) { for (int r = 1; r < s; ++r) { x = BigInteger.ModPow(x, 2, prime); if (x == 1) return false; if (x == prime - 1) break; } if (x != prime - 1) return false; } } return true; }
public static BigInteger GetPrime(int bits, Ibasa.Numerics.Random.Generator random) { if (bits < 2) throw new ArgumentException("bits is less than two.", "bits"); BigInteger pi = 2; if (bits > 4) pi = pi * 3; if (bits > 6) pi = pi * 5 * 7; if (bits > 8) pi = pi * 11 * 13; if (bits > 10) pi = pi * 17 * 19 * 23 * 29; byte[] bytes = new byte[((bits + 7) / 8) + 1]; var bytemask = (byte)(uint.MaxValue >> (32 - (bits % 8))); var intmask = BigInteger.Pow(2, bits) - 1; BigInteger prime; do { do { random.NextBytes(bytes); bytes[bytes.Length - 1] = 0; bytes[bytes.Length - 2] &= bytemask; prime = new BigInteger(bytes); } while (prime.IsEven); } while (BigInteger.GreatestCommonDivisor(prime, pi) != 1); while (!prime.IsProbablyPrime(100, random)) { prime = (prime + pi) & intmask; } return prime; }
public BlumBlumShub(int bits, Ibasa.Numerics.Random.Generator generator) { if (bits < 6) throw new ArgumentException("bits is less than 6.", "bits"); var p = GetPrime(bits / 2, generator); var q = GetPrime(bits / 2, generator); while (q == p) { q = GetPrime(bits / 2, generator); } M = p * q; do { var bytes = new byte[((bits + 7) / 8) + 1]; generator.NextBytes(bytes); bytes[bytes.Length - 1] = 0; State = new BigInteger(bytes); } while (State == 0 || State == 1 || State % p == 0 || State % q == 0); State = BigInteger.ModPow(State, 2, M); }
ImmutableArray<Mesh> LoadMeshes(Ibasa.IO.BinaryReader reader, int count) { long offset = reader.BaseStream.Position; Mesh[] meshes = new Mesh[count]; for (int i = 0; i < count; ++i) { reader.BaseStream.Position = offset; int material = reader.ReadInt32(); int modelIndex = reader.ReadInt32(); int vertexCount = reader.ReadInt32(); int vertexOffset = reader.ReadInt32(); int flexCount = reader.ReadInt32(); long flexOffset = offset + reader.ReadInt32(); int materialType = reader.ReadInt32(); int materialParam = reader.ReadInt32(); int meshId = reader.ReadInt32(); Vector3f center = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); int modelvertexdata = reader.ReadInt32(); int[] numLODVertexes = new int[8]; for (int l = 0; l < 8; ++l) { numLODVertexes[l] = reader.ReadInt32(); } ImmutableArray<int> lods = new ImmutableArray<int>(numLODVertexes); reader.Seek(8 * 4, SeekOrigin.Current); //skip 8 ints offset = reader.BaseStream.Position; reader.BaseStream.Position = flexOffset; ImmutableArray<Flex> flexes = LoadFlexes(reader, flexCount); meshes[i] = new Mesh(material, vertexCount, vertexOffset, flexes, materialType, materialParam, meshId, center, lods); } return new ImmutableArray<Mesh>(meshes); }
public static void SetUniform(int location, Ibasa.Interop.UnmanagedArray<int> value) { unsafe { Gl.Uniform1iv(location, value.Count, (int*)value.Pointer.ToPointer()); GlHelper.GetError(); } }
private void WriteDx10Header(Ibasa.IO.BinaryWriter writer) { if (!IsDX10Mode) return; writer.Write((int)DxgiFormat); writer.Write((int)ResourceDimension); writer.Write((int)MiscFlags); writer.Write(ArraySize); writer.Write(new byte[4]); //write padding }
ImmutableArray<Eyeball> LoadEyeballs(Ibasa.IO.BinaryReader reader, int count) { long offset = reader.BaseStream.Position; Eyeball[] eyeballs = new Eyeball[count]; for (int i = 0; i < count; ++i) { offset = reader.BaseStream.Position; reader.BaseStream.Position = offset; } return new ImmutableArray<Eyeball>(eyeballs); }
private void ReadHeader(Ibasa.IO.BinaryReader reader) { HeaderFlags = (HeaderFlagsEnum)reader.ReadInt32(); Height = reader.ReadInt32(); Width = reader.ReadInt32(); Pitch = reader.ReadInt32(); Depth = reader.ReadInt32(); MipmapCount = reader.ReadInt32(); reader.ReadBytes(44); //skip padding int pixelSize = reader.ReadInt32(); if (pixelSize != 32) throw new InvalidDataException(string.Format("Pixel format size {0} does not match 32.", pixelSize)); PixelFlags = (PixelFlagsEnum)reader.ReadInt32(); FourCC = new FourCC(reader.ReadInt32()); RGBBitCount = reader.ReadInt32(); RBitMask = reader.ReadInt32(); GBitMask = reader.ReadInt32(); BBitMask = reader.ReadInt32(); ABitMask = reader.ReadInt32(); SurfaceFlags = (SurfaceFlagsEnum)reader.ReadInt32(); CubemapFlags = (CubemapFlagsEnum)reader.ReadInt32(); reader.ReadBytes(12); //skip padding }
private void ReadData(Ibasa.IO.BinaryReader reader) { int depth = (HeaderFlags.HasFlag(HeaderFlagsEnum.Depth) ? Depth : 1); int mipmapCount = (HeaderFlags.HasFlag(HeaderFlagsEnum.MipmapCount) ? MipmapCount : 1); if (IsDX10Mode) { int arraySize = (CubemapFlags.HasFlag(CubemapFlagsEnum.Cubemap) ? 6 : 1) * ArraySize; Image = new Resource(new Size3i(Width, Height, depth), mipmapCount, arraySize, SelectLoadFormat()); for (int arraySlice = 0; arraySlice < Image.ArraySize; ++arraySlice) { for (int mipSlice = 0; mipSlice < Image.MipLevels; ++mipSlice) { byte[] subresource = Image[mipSlice, arraySlice]; reader.Read(subresource, 0, subresource.Length); } } } else if (PixelFlags.HasFlag(PixelFlagsEnum.FourCC)) { int arraySize = (CubemapFlags.HasFlag(CubemapFlagsEnum.Cubemap) ? 6 : 1); Image = new Resource(new Size3i(Width, Height, depth), mipmapCount, arraySize, SelectLoadFormat()); for (int arraySlice = 0; arraySlice < Image.ArraySize; ++arraySlice) { for (int mipSlice = 0; mipSlice < Image.MipLevels; ++mipSlice) { byte[] subresource = Image[mipSlice, arraySlice]; reader.Read(subresource, 0, subresource.Length); } } } }
private void ReadHeader73(Ibasa.IO.BinaryReader reader) { if (VersionMinor >= 3) { reader.ReadBytes(3); //skip padding int resourceCount = reader.ReadInt32(); reader.ReadBytes(8); //skip padding var resources = new Resource[resourceCount]; for (int res = 0; res < resourceCount; ++res) { int type = reader.ReadInt32(); resources[res] = new Resource( (ResourceType)type & ResourceType.Mask, (ResourceFlags)type & ResourceFlags.Mask, reader.ReadInt32()); } Resources = new ImmutableArray<Resource>(resources); } else if(VersionMinor == 2) { //skip padding reader.ReadBytes(15); //skip padding Resources = new ImmutableArray<Resource>(0); } }
protected override void FixedUpdate(Ibasa.Game.GameTime time) { Console.WriteLine(time); }
private void ReadHeader70(Ibasa.IO.BinaryReader reader) { Width = reader.ReadUInt16(); Height = reader.ReadUInt16(); Flags = (TextureFlags)reader.ReadInt32(); Frames = reader.ReadUInt16(); StartFrame = reader.ReadUInt16(); reader.ReadBytes(4); //skip padding Reflectivity = new Vector3f(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); reader.ReadBytes(4); //skip padding BumpmapScale = reader.ReadSingle(); ImageFormat = (ImageFormat)reader.ReadInt32(); MipmapCount = reader.ReadByte(); LowResImageFormat = (ImageFormat)reader.ReadInt32(); LowResImageWidth = reader.ReadByte(); LowResImageHeight = reader.ReadByte(); }
public static bool IsProbablyPrime(this long prime, int certainty, Ibasa.Numerics.Random.Generator random) { if (prime == 2 || prime == 3) return true; if (prime < 2 || prime % 2 == 0) return false; int s = 0; var d = prime - 1; while (d % 2 == 0) { d /= 2; s += 1; } for (int i = 0; i < certainty; ++i) { long a = random.Next(2, prime - 2); long x = Ibasa.Numerics.Functions.ModPow(a, d, prime); if (x != 1 && x != prime - 1) { for (int r = 1; r < s; ++r) { x = Ibasa.Numerics.Functions.ModPow(x, 2, prime); if (x == 1) return false; if (x == prime - 1) break; } if (x != prime - 1) return false; } } return true; }
ImmutableArray<Flex> LoadFlexes(Ibasa.IO.BinaryReader reader, int count) { return null; }
ImmutableArray<string> LoadStringTable(Ibasa.IO.BinaryReader reader, int count, long baseOffset) { long[] offsets = new long[count]; for (int i = 0; i < count; ++i) { offsets[i] = baseOffset + reader.ReadInt32(); } string[] strings = new string[count]; for (int i = 0; i < count; ++i) { reader.BaseStream.Position = offsets[i]; strings[i] = LoadString(reader); } return new ImmutableArray<string>(strings); }
string LoadString(Ibasa.IO.BinaryReader reader) { StringBuilder builder = new StringBuilder(); char c; while ((c = reader.ReadChar()) != '\0') { builder.Append(c); } return builder.ToString(); }
private void ReadHeader72(Ibasa.IO.BinaryReader reader) { if (VersionMinor >= 2) { Depth = reader.ReadUInt16(); Depth = Depth == 0 ? 1 : Depth; //map 0 to 1 } else { Depth = 1; reader.ReadBytes(1); //skip padding } }
public static void SetUniform(int location, Ibasa.Interop.UnmanagedArray<Ibasa.Numerics.Vector4ui> value) { unsafe { Gl.Uniform4uiv(location, value.Count, (uint*)value.Pointer.ToPointer()); GlHelper.GetError(); } }
private void ReadData(Ibasa.IO.BinaryReader reader) { LowResImage = new SharpIL.Resource( new Size3i(LowResImageWidth, LowResImageHeight, 1), 1, 1, EncodingFromFormat(LowResImageFormat)); HighResImage = new SharpIL.Resource( new Size3i(Width, Height, Depth), MipmapCount, Frames * Faces, EncodingFromFormat(ImageFormat)); if (VersionMinor < 3) { reader.Read(LowResImage[0, 0], 0, LowResImage[0, 0].Length); for (int mip = MipmapCount-1; mip >=0 ; --mip) { Size3f size = HighResImage.ComputeMipSliceSize(mip); for (int frame = 0; frame < Frames; ++frame) { for (int face = 0; face < Faces; ++face) { int array = face + (Faces * frame); reader.Read( HighResImage[mip, array], 0, HighResImage[mip, array].Length); } } } } else { //low res int lowResOffset = Resources.Find((resource) => resource.Type == ResourceType.LowResImage).Data; reader.Seek((uint)lowResOffset, SeekOrigin.Begin); reader.Read(LowResImage[0, 0], 0, LowResImage[0, 0].Length); //high res int highResOffset = Resources.Find((resource) => resource.Type == ResourceType.Image).Data; reader.Seek((uint)highResOffset, SeekOrigin.Begin); for (int mip = MipmapCount - 1; mip >= 0; --mip) { Size3f size = HighResImage.ComputeMipSliceSize(mip); for (int frame = 0; frame < Frames; ++frame) { for (int face = 0; face < Faces; ++face) { byte[] data = HighResImage[mip, face + (Faces * frame)]; reader.Read(data, 0, data.Length); } } } } }
public static void SetUniform(int location, Ibasa.Numerics.Vector2f value) { Gl.Uniform2f(location, value.X, value.Y); GlHelper.GetError(); }
protected override void Update(Ibasa.Game.GameTime time) { System.Threading.Thread.Sleep(16); }
public static void SetUniform(int location, Ibasa.Interop.UnmanagedArray<Ibasa.Numerics.Matrix4x3f> value) { unsafe { Gl.UniformMatrix4x3fv(location, value.Count, 0, (float*)value.Pointer.ToPointer()); GlHelper.GetError(); } }
private void ReadDx10Header(Ibasa.IO.BinaryReader reader) { if (!IsDX10Mode) return; DxgiFormat = (DxgiFormat)reader.ReadInt32(); ResourceDimension = (ResourceDimensionEnum)reader.ReadInt32(); MiscFlags = (MiscFlagsEnum)reader.ReadInt32(); ArraySize = reader.ReadInt32(); reader.ReadBytes(4); //skip padding }
public static void SetUniform(int location, Ibasa.Numerics.Matrix4x3f value) { unsafe { Gl.UniformMatrix4x3fv(location, 1, 0, (float*)(&value)); GlHelper.GetError(); } }
private void WriteHeader(Ibasa.IO.BinaryWriter writer) { writer.Write((int)HeaderFlags); writer.Write(Height); writer.Write(Width); writer.Write(Pitch); writer.Write(Depth); writer.Write(MipmapCount); writer.Write(new byte[44]); //write padding writer.Write(32); writer.Write((int)PixelFlags); writer.Write((int)FourCC); writer.Write(RGBBitCount); writer.Write(RBitMask); writer.Write(GBitMask); writer.Write(BBitMask); writer.Write(ABitMask); writer.Write((int)SurfaceFlags); writer.Write((int)CubemapFlags); writer.Write(new byte[12]); //write padding }
public static void SetUniform(int location, Ibasa.Numerics.Vector4i value) { Gl.Uniform4i(location, value.X, value.Y, value.Z, value.W); GlHelper.GetError(); }
private void WriteData(Ibasa.IO.BinaryWriter writer) { if (IsDX10Mode) { for (int arraySlice = 0; arraySlice < Image.ArraySize; ++arraySlice) { for (int mipSlice = 0; mipSlice < Image.MipLevels; ++mipSlice) { byte[] subresource = Image[mipSlice, arraySlice]; writer.Write(subresource); } } } else if (PixelFlags.HasFlag(PixelFlagsEnum.FourCC)) { for (int arraySlice = 0; arraySlice < Image.ArraySize; ++arraySlice) { for (int mipSlice = 0; mipSlice < Image.MipLevels; ++mipSlice) { byte[] subresource = Image[mipSlice, arraySlice]; writer.Write(subresource); } } } else { for (int arraySlice = 0; arraySlice < Image.ArraySize; ++arraySlice) { for (int mipSlice = 0; mipSlice < Image.MipLevels; ++mipSlice) { byte[] subresource = Image[mipSlice, arraySlice]; writer.Write(subresource); } } } }
ImmutableArray<Model> LoadModels(Ibasa.IO.BinaryReader reader, int count) { long offset = reader.BaseStream.Position; Model[] models = new Model[count]; for (int i = 0; i < count; ++i) { reader.BaseStream.Position = offset; string name = Encoding.ASCII.GetString(reader.ReadBytes(64)).Trim('\0'); int type = reader.ReadInt32(); float boundingRadius = reader.ReadSingle(); int meshCount = reader.ReadInt32(); long meshOffset = offset + reader.ReadInt32(); int vertexCount = reader.ReadInt32(); int vertexIndex = reader.ReadInt32(); int tangentIndex = reader.ReadInt32(); int numattachments = reader.ReadInt32(); int attachmentindex = reader.ReadInt32(); int eyeballCount = reader.ReadInt32(); long eyeballOffset = offset + reader.ReadInt32(); int vertexData = reader.ReadInt32(); int indexData = reader.ReadInt32(); reader.Seek(8 * 4, SeekOrigin.Current); //skip 8 ints offset = reader.BaseStream.Position; reader.BaseStream.Position = meshOffset; ImmutableArray<Mesh> meshes = LoadMeshes(reader, meshCount); reader.BaseStream.Position = meshOffset; ImmutableArray<Eyeball> eyeballs = LoadEyeballs(reader, meshCount); models[i] = new Model(name, type, boundingRadius, meshes, vertexCount, vertexIndex, tangentIndex, eyeballs); } return new ImmutableArray<Model>(models); }