public void SetTexture(crVTMapEntry vtEntry) { currentEntry = vtEntry; if (vtEntry == null || vtPage == null) { return; } var tiles = vtPage.GetTiles(vtEntry); if (tiles.Count == 0) { return; } mainForm.tileListSource.DataSource = tiles; var uniqueTiles = (from tile in tiles select tile.TDXTile.Texture.Name).Distinct(); Format textureformat = Format.BC3_UNorm; if (tiles[0].TDXTile.Texture == null) { tiles[0].TDXTile.GetTextureFromZAD(); } if (tiles[0].TDXTile.Texture == null) { return; } switch (tiles[0].TDXTile.Texture.Format) { case ToxicRagers.Helpers.D3DFormat.DXT1: textureformat = Format.BC1_UNorm; break; case ToxicRagers.Helpers.D3DFormat.DXT5: textureformat = Format.BC3_UNorm; break; case ToxicRagers.Helpers.D3DFormat.ATI2: textureformat = Format.BC5_UNorm; break; case ToxicRagers.Helpers.D3DFormat.A8R8G8B8: textureformat = Format.B8G8R8A8_UNorm_SRGB; break; case ToxicRagers.Helpers.D3DFormat.A8: textureformat = Format.A8_UNorm; break; } List <ShaderResourceView> resourceViews = new List <ShaderResourceView>(); List <DataRectangle> rects = new List <DataRectangle>(); List <Int32> TileRows = new List <int>(); List <Int32> TileCols = new List <int>(); List <float> IndirectionData = new List <float>(); int divisor = vtPage.GetDivisor(); var cb = new VTConstantBuffer(); cb.Width = vtEntry.Width / vtPage.GetDivisor(); cb.Height = vtEntry.Height / vtPage.GetDivisor(); cb.TilesX = (int)Math.Ceiling(cb.Width / 120.0f) + 1; cb.TilesY = (int)Math.Ceiling(cb.Height / 120.0f) + 1; cb.ViewportWidth = targetControl.ClientSize.Width; cb.ViewportHeight = targetControl.ClientSize.Height; cb.PosOffsetX = vtEntry.Column / divisor - tiles[0].Column * 120; cb.PosOffsetY = vtEntry.Row / divisor - tiles[0].Row * 120; float widthRatio = (float)cb.Height / cb.Width; float heightRatio = (float)cb.Width / cb.Height; float polyWidth = 1f; float polyHeight = 1f; if (cb.ViewportWidth > cb.ViewportHeight) { polyWidth = cb.ViewportWidth / cb.ViewportHeight; } else if (cb.ViewportWidth < cb.ViewportHeight) { polyHeight = cb.ViewportHeight / cb.ViewportWidth; } if (cb.Width > cb.Height) { if (cb.ViewportWidth > cb.Width) { //polyWidth = (float)cb.Width / cb.ViewportWidth; //polyHeight = (float)cb.Height / cb.ViewportHeight; } cb.TextureWidth = cb.ViewportWidth < cb.Width ? cb.ViewportWidth : cb.Width; cb.TextureHeight = (int)(cb.TextureWidth * ((float)cb.Height / cb.Width)); polyHeight = (1f - ((float)cb.TextureHeight / cb.ViewportHeight) * widthRatio) * 2 - 1; polyWidth = ((float)cb.TextureWidth / cb.ViewportWidth) * 2 - 1; } else if (cb.Width < cb.Height) { if (cb.ViewportHeight > cb.Height) { //polyHeight = (float)cb.Height / cb.ViewportHeight; //polyWidth = (float)cb.Height / cb.ViewportHeight; } cb.TextureHeight = cb.ViewportHeight < cb.Height ? cb.ViewportHeight : cb.Height; cb.TextureWidth = (int)(cb.TextureHeight * ((float)cb.Width / cb.Height)); polyHeight = (1f - ((float)cb.TextureHeight / cb.ViewportHeight)) * 2 - 1; polyWidth = ((float)cb.TextureWidth / cb.ViewportWidth) * heightRatio * 2 - 1; } else { if (cb.ViewportWidth < cb.ViewportHeight) { if (cb.ViewportWidth < cb.Width) { cb.TextureWidth = cb.ViewportWidth; cb.TextureHeight = (int)Math.Floor(cb.ViewportWidth * widthRatio); //polyWidth = (float)cb.Width / cb.ViewportWidth; //polyHeight = (float)cb.Height / cb.ViewportHeight; // polyWidth; } } cb.TextureHeight = cb.ViewportHeight < cb.Height ? cb.ViewportHeight : cb.Height; cb.TextureWidth = cb.ViewportWidth < cb.Width ? cb.ViewportWidth : cb.Width; polyHeight = (1f - ((float)cb.TextureHeight / cb.ViewportHeight)) * 2 - 1; polyWidth = ((float)cb.TextureWidth / cb.ViewportWidth) * 2 - 1; } var mappedData = context.MapSubresource(vertexBuffer, 0, MapMode.WriteDiscard, SlimDX.Direct3D11.MapFlags.None); mappedData.Data.Write(new Vector3(-1f, polyHeight, 0.5f)); mappedData.Data.Write(new Vector2(0f, 1f)); mappedData.Data.Write(new Vector3(-1f, 1f, 0.5f)); mappedData.Data.Write(new Vector2(0f, 0f)); mappedData.Data.Write(new Vector3(polyWidth, polyHeight, 0.5f)); mappedData.Data.Write(new Vector2(1f, 1f)); mappedData.Data.Write(new Vector3(polyWidth, 1f, 0.5f)); mappedData.Data.Write(new Vector2(1f, 0f)); context.UnmapSubresource(vertexBuffer, 0); if (cb.PosOffsetX > 0 || cb.PosOffsetY > 0) { Console.Write(""); } //cb.PosX = vtEntry.Column / vtPage.GetDivisor(); //cb.PosY = vtEntry.Row / vtPage.GetDivisor(); for (int y = 0; y <= cb.TilesY; y++) { for (int x = 0; x <= cb.TilesX; x++) { IndirectionData.Add(0); } } int vtDivisor = vtPage.GetDivisor(); foreach (var tile in tiles) { //using (MemoryStream stream = new MemoryStream()) { /*var dds = tile.GetDDS(); * BinaryWriter bw = new BinaryWriter(stream); * dds.Save(bw); * stream.Seek(0, SeekOrigin.Begin); * Texture2D tex = Texture2D.FromStream(device, stream, (int)stream.Length);*/ DataRectangle rect = new DataRectangle(128 * 4, new DataStream(tile.TDXTile.Texture.DecompressToBytes(), false, false)); rects.Add(rect); TileRows.Add(tile.Row); TileCols.Add(tile.Column); int indIndex = (((tile.Row * 120 - (vtEntry.Row / vtDivisor)) / 120) * cb.TilesX) + (tile.Column * 120 - vtEntry.Column / vtDivisor) / 120; IndirectionData[indIndex] = (float)((rects.Count - 1) / tiles.Count); //resourceViews.Add(new ShaderResourceView(device, tex)); //bw.Dispose(); } } Texture2D textureArray = new Texture2D(device, new Texture2DDescription { BindFlags = BindFlags.ShaderResource, ArraySize = rects.Count, Width = 128, Height = 128, Usage = ResourceUsage.Default, CpuAccessFlags = CpuAccessFlags.None, Format = Format.B8G8R8A8_UNorm, SampleDescription = new SampleDescription(1, 0), MipLevels = 1, OptionFlags = ResourceOptionFlags.None } , rects.ToArray()); ShaderResourceView resourceView = new ShaderResourceView(device, textureArray); context.PixelShader.SetShaderResource(resourceView, 0); //cb.TilePosX = TileCols.ToArray(); //cb.TilePosY = TileRows.ToArray(); int sizeOfCB = ((sizeof(Int32) * 10 + 15) / 16) * 16; using (DataStream data = new DataStream(sizeOfCB, true, true)) { data.Write(cb); data.Position = 0; context.PixelShader.SetConstantBuffer(new SlimDX.Direct3D11.Buffer(device, data, new BufferDescription { Usage = ResourceUsage.Default, SizeInBytes = sizeOfCB,// + cb.TilePosX.Length + cb.TilePosY.Length), BindFlags = BindFlags.ConstantBuffer }), 0); } Texture2D inderectionTexture = new Texture2D(device, new Texture2DDescription { BindFlags = BindFlags.ShaderResource, ArraySize = 1, Width = cb.TilesX, Height = cb.TilesY, Usage = ResourceUsage.Default, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R32_Float, SampleDescription = new SampleDescription(1, 0), MipLevels = 1, OptionFlags = ResourceOptionFlags.None } , new DataRectangle(cb.TilesX * 4, new DataStream(IndirectionData.ToArray(), true, true))); ShaderResourceView indresourceview = new ShaderResourceView(device, inderectionTexture); context.PixelShader.SetShaderResource(indresourceview, 1); UpdateRenderer = true; //fx.GetVariableByName("shaderTexture").AsResource().SetResourceArray(resourceViews.ToArray()); /*fx.GetVariableByName("Width").AsScalar().Set(vtEntry.Width); * fx.GetVariableByName("Height").AsScalar().Set(vtEntry.Height); * fx.GetVariableByName("TilesX").AsScalar().Set((int)(vtEntry.Width / 128.0f)); * fx.GetVariableByName("TilesY").AsScalar().Set((int)(vtEntry.Height / 128.0f)); * device.ImmediateContext.PixelShader.SetShaderResource(resourceView, 0);*/ //context.PixelShader.SetShaderResource(resourceView, 0); //context.PixelShader.SetSampler(sampleState, 0); }
public void SetTexture(crVTMapEntry vtEntry) { currentEntry = vtEntry; if (vtEntry == null || vtPage == null) return; var tiles = vtPage.GetTiles(vtEntry); if (tiles.Count == 0) return; mainForm.tileListSource.DataSource = tiles; var uniqueTiles = (from tile in tiles select tile.TDXTile.Texture.Name).Distinct(); Format textureformat = Format.BC3_UNorm; if (tiles[0].TDXTile.Texture == null) tiles[0].TDXTile.GetTextureFromZAD(); if (tiles[0].TDXTile.Texture == null) { return; } switch (tiles[0].TDXTile.Texture.Format) { case ToxicRagers.Helpers.D3DFormat.DXT1: textureformat = Format.BC1_UNorm; break; case ToxicRagers.Helpers.D3DFormat.DXT5: textureformat = Format.BC3_UNorm; break; case ToxicRagers.Helpers.D3DFormat.ATI2: textureformat = Format.BC5_UNorm; break; case ToxicRagers.Helpers.D3DFormat.A8R8G8B8: textureformat = Format.B8G8R8A8_UNorm_SRGB; break; case ToxicRagers.Helpers.D3DFormat.A8: textureformat = Format.A8_UNorm; break; } List<ShaderResourceView> resourceViews = new List<ShaderResourceView>(); List<DataRectangle> rects = new List<DataRectangle>(); List<Int32> TileRows = new List<int>(); List<Int32> TileCols = new List<int>(); List<float> IndirectionData = new List<float>(); int divisor = vtPage.GetDivisor(); var cb = new VTConstantBuffer(); cb.Width = vtEntry.Width / vtPage.GetDivisor(); cb.Height = vtEntry.Height / vtPage.GetDivisor(); cb.TilesX = (int)Math.Ceiling(cb.Width / 120.0f) + 1; cb.TilesY = (int)Math.Ceiling(cb.Height / 120.0f) + 1; cb.ViewportWidth = targetControl.ClientSize.Width; cb.ViewportHeight = targetControl.ClientSize.Height; cb.PosOffsetX = vtEntry.Column / divisor - tiles[0].Column * 120; cb.PosOffsetY = vtEntry.Row / divisor - tiles[0].Row * 120; float widthRatio = (float)cb.Height / cb.Width; float heightRatio = (float)cb.Width / cb.Height; float polyWidth = 1f; float polyHeight = 1f; if (cb.ViewportWidth > cb.ViewportHeight) { polyWidth = cb.ViewportWidth / cb.ViewportHeight; } else if (cb.ViewportWidth < cb.ViewportHeight) { polyHeight = cb.ViewportHeight / cb.ViewportWidth; } if (cb.Width > cb.Height) { if (cb.ViewportWidth > cb.Width) { //polyWidth = (float)cb.Width / cb.ViewportWidth; //polyHeight = (float)cb.Height / cb.ViewportHeight; } cb.TextureWidth = cb.ViewportWidth < cb.Width ? cb.ViewportWidth : cb.Width; cb.TextureHeight = (int)(cb.TextureWidth * ((float)cb.Height / cb.Width)); polyHeight = (1f - ((float)cb.TextureHeight / cb.ViewportHeight) * widthRatio) * 2 - 1; polyWidth = ((float)cb.TextureWidth / cb.ViewportWidth) * 2 - 1; } else if (cb.Width < cb.Height) { if (cb.ViewportHeight > cb.Height) { //polyHeight = (float)cb.Height / cb.ViewportHeight; //polyWidth = (float)cb.Height / cb.ViewportHeight; } cb.TextureHeight = cb.ViewportHeight < cb.Height ? cb.ViewportHeight : cb.Height; cb.TextureWidth = (int)(cb.TextureHeight * ((float)cb.Width / cb.Height)); polyHeight = (1f - ((float)cb.TextureHeight / cb.ViewportHeight)) * 2 - 1; polyWidth = ((float)cb.TextureWidth / cb.ViewportWidth) * heightRatio * 2 - 1; } else { if (cb.ViewportWidth < cb.ViewportHeight) { if (cb.ViewportWidth < cb.Width) { cb.TextureWidth = cb.ViewportWidth; cb.TextureHeight = (int)Math.Floor(cb.ViewportWidth * widthRatio); //polyWidth = (float)cb.Width / cb.ViewportWidth; //polyHeight = (float)cb.Height / cb.ViewportHeight; // polyWidth; } } cb.TextureHeight = cb.ViewportHeight < cb.Height ? cb.ViewportHeight : cb.Height; cb.TextureWidth = cb.ViewportWidth < cb.Width ? cb.ViewportWidth : cb.Width; polyHeight = (1f - ((float)cb.TextureHeight / cb.ViewportHeight)) * 2 - 1; polyWidth = ((float)cb.TextureWidth / cb.ViewportWidth) * 2 - 1; } var mappedData = context.MapSubresource(vertexBuffer, 0, MapMode.WriteDiscard, SlimDX.Direct3D11.MapFlags.None); mappedData.Data.Write(new Vector3(-1f, polyHeight, 0.5f)); mappedData.Data.Write(new Vector2(0f, 1f)); mappedData.Data.Write(new Vector3(-1f, 1f, 0.5f)); mappedData.Data.Write(new Vector2(0f, 0f)); mappedData.Data.Write(new Vector3(polyWidth, polyHeight, 0.5f)); mappedData.Data.Write(new Vector2(1f, 1f)); mappedData.Data.Write(new Vector3(polyWidth, 1f, 0.5f)); mappedData.Data.Write(new Vector2(1f, 0f)); context.UnmapSubresource(vertexBuffer, 0); if (cb.PosOffsetX > 0 || cb.PosOffsetY > 0) { Console.Write(""); } //cb.PosX = vtEntry.Column / vtPage.GetDivisor(); //cb.PosY = vtEntry.Row / vtPage.GetDivisor(); for (int y = 0; y <= cb.TilesY; y++) { for (int x = 0; x <= cb.TilesX; x++) { IndirectionData.Add(0); } } int vtDivisor = vtPage.GetDivisor(); foreach (var tile in tiles) { //using (MemoryStream stream = new MemoryStream()) { /*var dds = tile.GetDDS(); BinaryWriter bw = new BinaryWriter(stream); dds.Save(bw); stream.Seek(0, SeekOrigin.Begin); Texture2D tex = Texture2D.FromStream(device, stream, (int)stream.Length);*/ DataRectangle rect = new DataRectangle(128 * 4, new DataStream(tile.TDXTile.Texture.DecompressToBytes(), false, false)); rects.Add(rect); TileRows.Add(tile.Row); TileCols.Add(tile.Column); int indIndex = (((tile.Row * 120 - (vtEntry.Row / vtDivisor)) / 120) * cb.TilesX) + (tile.Column * 120 - vtEntry.Column / vtDivisor) / 120; IndirectionData[indIndex] = (float)((rects.Count - 1) / tiles.Count); //resourceViews.Add(new ShaderResourceView(device, tex)); //bw.Dispose(); } } Texture2D textureArray = new Texture2D(device, new Texture2DDescription { BindFlags = BindFlags.ShaderResource, ArraySize = rects.Count, Width = 128, Height = 128, Usage = ResourceUsage.Default, CpuAccessFlags = CpuAccessFlags.None, Format = Format.B8G8R8A8_UNorm, SampleDescription = new SampleDescription(1, 0), MipLevels = 1, OptionFlags = ResourceOptionFlags.None } , rects.ToArray()); ShaderResourceView resourceView = new ShaderResourceView(device, textureArray); context.PixelShader.SetShaderResource(resourceView, 0); //cb.TilePosX = TileCols.ToArray(); //cb.TilePosY = TileRows.ToArray(); int sizeOfCB = ((sizeof(Int32) * 10 + 15) / 16) * 16; using (DataStream data = new DataStream(sizeOfCB, true, true)) { data.Write(cb); data.Position = 0; context.PixelShader.SetConstantBuffer(new SlimDX.Direct3D11.Buffer(device, data, new BufferDescription { Usage = ResourceUsage.Default, SizeInBytes = sizeOfCB,// + cb.TilePosX.Length + cb.TilePosY.Length), BindFlags = BindFlags.ConstantBuffer }), 0); } Texture2D inderectionTexture = new Texture2D(device, new Texture2DDescription { BindFlags = BindFlags.ShaderResource, ArraySize = 1, Width = cb.TilesX, Height = cb.TilesY, Usage = ResourceUsage.Default, CpuAccessFlags = CpuAccessFlags.None, Format = Format.R32_Float, SampleDescription = new SampleDescription(1, 0), MipLevels = 1, OptionFlags = ResourceOptionFlags.None } , new DataRectangle(cb.TilesX * 4, new DataStream(IndirectionData.ToArray(), true, true))); ShaderResourceView indresourceview = new ShaderResourceView(device, inderectionTexture); context.PixelShader.SetShaderResource(indresourceview, 1); UpdateRenderer = true; //fx.GetVariableByName("shaderTexture").AsResource().SetResourceArray(resourceViews.ToArray()); /*fx.GetVariableByName("Width").AsScalar().Set(vtEntry.Width); fx.GetVariableByName("Height").AsScalar().Set(vtEntry.Height); fx.GetVariableByName("TilesX").AsScalar().Set((int)(vtEntry.Width / 128.0f)); fx.GetVariableByName("TilesY").AsScalar().Set((int)(vtEntry.Height / 128.0f)); device.ImmediateContext.PixelShader.SetShaderResource(resourceView, 0);*/ //context.PixelShader.SetShaderResource(resourceView, 0); //context.PixelShader.SetSampler(sampleState, 0); }